forked from open-telemetry/opentelemetry-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (98 loc) · 3.46 KB
/
index.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
'use strict'
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
const { callbackify, promisify } = require('util')
const grpc = require('@grpc/grpc-js')
const protoLoader = require('@grpc/proto-loader')
const health = require('grpc-js-health-check')
const opentelemetry = require('@opentelemetry/api')
const charge = require('./charge')
const logger = require('./logger')
const otelDemoPackage = grpc.loadPackageDefinition(
protoLoader.loadSync('demo.proto')
)
const featureFlagServiceClient = initFeatureFlagClient()
const getPaymentServiceSimulateSlowness = featureFlagServiceClient
? promisify(featureFlagServiceClient.getRangeFeatureFlag.bind(featureFlagServiceClient))
: null
async function chargeServiceHandler(call) {
const span = opentelemetry.trace.getActiveSpan()
try {
const amount = call.request.amount
span.setAttributes({
'app.payment.amount': parseFloat(`${amount.units}.${amount.nanos}`)
})
logger.info({ request: call.request }, 'Charge request received.')
await simulateSlowness(span);
return charge.charge(call.request)
} catch (err) {
logger.warn({ err })
span.recordException(err)
span.setStatus({ code: opentelemetry.SpanStatusCode.ERROR })
throw err
}
}
async function simulateSlowness(span) {
if (getPaymentServiceSimulateSlowness) {
try {
const simulateSlownessResponse = await getPaymentServiceSimulateSlowness({
name: 'paymentServiceSimulateSlowness',
nameLowerBound: 'paymentServiceSimulateSlownessLowerBound',
nameUpperBound: 'paymentServiceSimulateSlownessUpperBound'
})
if (simulateSlownessResponse.enabled) {
const minimumDelayResponse = simulateSlownessResponse.lowerBound || 0
const maximumDelayResponse = simulateSlownessResponse.upperBound || 500
const delayMillis =
minimumDelayResponse +
Math.floor(Math.random() * (maximumDelayResponse - minimumDelayResponse))
logger.info('Simulating payment service slowness, waiting %d.', delayMillis)
span.setAttributes({ 'app.payment.simulatedSlowness': delayMillis })
await new Promise((resolve) => setTimeout(resolve, delayMillis))
}
} catch (err) {
logger.warn({err: err})
}
}
}
async function closeGracefully(signal) {
featureFlagServiceClient.close()
server.forceShutdown()
process.kill(process.pid, signal)
}
const server = new grpc.Server()
server.addService(
health.service,
new health.Implementation({
'': health.servingStatus.SERVING,
})
)
server.addService(otelDemoPackage.oteldemo.PaymentService.service, {
charge: callbackify(chargeServiceHandler),
})
server.bindAsync(
`0.0.0.0:${process.env['PAYMENT_SERVICE_PORT']}`,
grpc.ServerCredentials.createInsecure(),
(err, port) => {
if (err) {
return logger.error({ err })
}
logger.info(`PaymentService gRPC server started on port ${port}`)
server.start()
}
)
function initFeatureFlagClient() {
const featureFlagServiceAddress = process.env.FEATURE_FLAG_GRPC_SERVICE_ADDR
if (!featureFlagServiceAddress) {
logger.warn(
'The feature flag service address is not set (FEATURE_FLAG_GRPC_SERVICE_ADDR). No artificial request duration variance will be introduced.'
)
return null
}
return new otelDemoPackage.oteldemo.FeatureFlagService(
featureFlagServiceAddress,
grpc.credentials.createInsecure()
)
}
process.once('SIGINT', closeGracefully)
process.once('SIGTERM', closeGracefully)