forked from microservices-demo/front-end
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.js
75 lines (62 loc) · 2.17 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const opentelemetry = require('@opentelemetry/api');
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { ConsoleSpanExporter } = require('@opentelemetry/tracing');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { CollectorTraceExporter } = require('@opentelemetry/exporter-collector-grpc');
const ExpressLayerType = require('@opentelemetry/plugin-express')
const { Context } = require('@opentelemetry/api');
const { ReadableSpan } = require( '@opentelemetry/tracing');
const { Span } = require( '@opentelemetry/tracing');
// const { SpanProcessor } = require( '@opentelemetry/tracing');
var os = require("os");
var hostname = os.hostname();
class AttributeSpanProcessor {
onStart(span, _context) {
span.setAttribute("host.name", hostname);
}
onEnd(_span) {}
shutdown() {
return Promise.resolve();
}
forceFlush() {
return Promise.resolve();
}
}
module.exports = (serviceName, url) => {
var logStdout = false
if (url == "stdout") {
logStdout = true
}
const collectorOptions = {
serviceName: serviceName,
url: url // url is optional and can be omitted - default is localhost:4317
};
console.log("serviceName : " + serviceName + ", url : " + url)
const exporter = new CollectorTraceExporter(collectorOptions);
const provider = new NodeTracerProvider({
plugins: {
http: {
enabled: true,
path: '@opentelemetry/plugin-http',
ignoreIncomingPaths: [new RegExp('.*(png|html)')],
},
express: {
enabled: true,
// You may use a package name or absolute path to the file.
path: '@opentelemetry/plugin-express',
ignoreLayers: [new RegExp('middleware.*')],
ignoreLayersType: [ExpressLayerType.MIDDLEWARE],
},
}
});
if (!logStdout) {
provider.addSpanProcessor(new AttributeSpanProcessor())
provider.addSpanProcessor(SimpleSpanProcessor(exporter));
} else {
provider.addSpanProcessor(new AttributeSpanProcessor())
var stdexporter = new ConsoleSpanExporter();
provider.addSpanProcessor(new SimpleSpanProcessor(stdexporter));
}
provider.register();
return opentelemetry.trace.getTracer("front-end");
};