-
Notifications
You must be signed in to change notification settings - Fork 1
/
casa-account-server.js
158 lines (136 loc) · 4.23 KB
/
casa-account-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// very bad nodejs code below...sorry...
// google tracer works, opencensus-exporter-stackdriver is
// doesn't work when deployed in GKE, not sure why
const useStackdriver = (() => {
let env = process.env.USE_STACKDRIVER;
return env && (env === "yes" || env === "true");
})();
if (useStackdriver) {
require("@google-cloud/trace-agent").start({
samplingRate: 1000, // max that GCP allows
logLevel: 3, // info
});
console.log("google cloud trace agent initialized");
}
const fs = require("fs");
const path = require("path");
const _ = require("lodash");
// gRPC stuff
const grpc = require("grpc");
const protoLoader = require("@grpc/proto-loader");
const health = require("grpc-health-check");
function initZipkinTracing() {
// opencensus stuff
const {logger} = require("@opencensus/core");
const tracing = require("@opencensus/nodejs");
const {plugin} = require("@opencensus/instrumentation-grpc");
const {ZipkinTraceExporter} = require("@opencensus/exporter-zipkin");
zipkinUrl = process.env.ZIPKIN_COLLECTOR_URL;
if (!zipkinUrl || zipkinUrl === "off") {
console.log("jaeger exporter not initialized");
return undefined;
}
const exporter = new ZipkinTraceExporter({
url: zipkinUrl,
serviceName: "casa-account-v2",
tags: [
{
key: "runtime",
value: "nodejs " + process.versions.node,
},
{
key: "service",
value: "casa-account-v2",
},
],
bufferTimeout: 10, // time in milliseconds
logger: logger.logger("info"),
});
tracing.registerExporter(exporter).start({
samplingRate: 1,
logLevel: 1,
});
const basedir = path.dirname(require.resolve("grpc"));
const version = require(path.join(basedir, "package.json")).version;
// Enables GRPC plugin: Method that enables the instrumentation patch.
plugin.enable(
grpc,
tracing.tracer,
version,
/** plugin options */ {},
basedir
);
console.log("jaeger tracing initialized");
}
// dynamically load proto files and generates structure
// in memory. not using code generated by protoc here
let PROTO_PATH = __dirname + "/demo_bank.proto";
let packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
let api = grpc.loadPackageDefinition(packageDefinition).demobank.api;
function dummyCasaAccount(call, callback) {
let lastUpdate = {
seconds: Math.floor(new Date(2020, 4, 31, 18, 19, 20, 0) / 1000 + 3600 * 8),
nanos: 0,
};
let dummy = Object.assign(
{
account_id: "00000000",
nickname: "dummy-v2",
prod_code: "1111",
prod_name: "Bottomless CASA",
currency: "SGD",
status: "DORMANT",
status_last_updated: lastUpdate,
balances: [
{
amount: 10.0,
type: "CURRENT",
credit_flag: true,
last_updated: lastUpdate,
},
{
amount: 10.0,
type: "AVAILABLE",
credit_flag: true,
last_updated: lastUpdate,
},
],
},
api.CasaAccount
);
console.log("returning dummy account data");
callback(null, dummy);
}
function getServer() {
let server = new grpc.Server();
server.addService(
health.service,
new health.Implementation({
"": proto.grpc.health.v1.HealthCheckResponse.ServingStatus.SERVING,
})
);
server.addService(api.CasaAccountService.service, {
GetAccount: dummyCasaAccount,
});
return server;
}
if (require.main === module) {
if (!useStackdriver) {
initZipkinTracing();
}
let addr = process.env.GRPC_LISTEN_ADDR;
if (!addr) {
addr = "0.0.0.0:50051";
}
console.log("starting server listening on ", addr);
let server = getServer();
server.bind(addr, grpc.ServerCredentials.createInsecure());
server.start();
}
exports.getServer = getServer;