This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
tracer.js
60 lines (49 loc) · 1.71 KB
/
tracer.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
const promClient = require('prom-client')
const { initTracer, PrometheusMetricsFactory } = require('jaeger-client')
const Logger = require('./logger')
class MetricsFactory {
constructor (serviceName, options) {
options = options || {}
this.namespace = this.sanitizeName(serviceName)
this.factory = options.factory || new PrometheusMetricsFactory(promClient, this.namespace)
}
sanitizeName (name) {
return name.replace(/[-:]/g, '_')
}
createCounter (name, tags) {
name = this.sanitizeName(name)
return this.factory.createCounter(name, tags)
}
createGauge (name, tags) {
name = this.sanitizeName(name)
return this.factory.createGauge(name, tags)
}
createTimer (name, tags) {
name = this.sanitizeName(name)
return this.factory.createTimer(name, tags)
}
}
function createTracer (config, options) {
options = options || {}
options.logger = options.logger || new Logger('Tracer')
options.metrics = options.metrics || new MetricsFactory(config.serviceName)
// https://github.com/jaegertracing/jaeger-client-node/blob/master/src/configuration.js
const tracerConfig = {
serviceName: config.serviceName,
sampler: {
type: 'const',
param: 1
},
reporter: {
agentHost: config.jaegerAgentHost,
agentPort: config.jaegerAgentPort,
logSpans: config.jaegerLogSpans
}
}
const tracerOptions = {
logger: options.logger, // https://github.com/jaegertracing/jaeger-client-node/blob/master/src/_flow/logger.js
metrics: options.metrics // https://github.com/jaegertracing/jaeger-client-node/blob/master/src/_flow/metrics.js
}
return initTracer(tracerConfig, tracerOptions)
}
module.exports = { MetricsFactory, createTracer }