-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer.js
84 lines (76 loc) · 2.5 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
76
77
78
79
80
81
82
83
84
const opentelemetry = require("@opentelemetry/api");
const {
TraceExporter,
} = require("@google-cloud/opentelemetry-cloud-trace-exporter");
const { NodeTracerProvider } = require("@opentelemetry/node");
const { BatchSpanProcessor } = require("@opentelemetry/tracing");
module.exports = class Tracer {
constructor(config) {
this.projectId = config.projectId;
this.keyPath = config.keyPath;
this.plugins = config.plugins;
this.api = opentelemetry;
}
/**
* Register this tracer for use with the OpenTelemetry API.
* Undefined values may be replaced with defaults
*/
createTracer() {
// Create a provider for activating and tracking spans
const tracerProvider = new NodeTracerProvider({
plugins: {
http: {
enabled: this.plugins.http === true ? true : false,
path: "@opentelemetry/plugin-http",
ignoreOutgoingUrls: [/traces:batchWrite/],
},
https: {
enabled: this.plugins.https === true ? true : false,
path: "@opentelemetry/plugin-https",
ignoreOutgoingUrls: [/traces:batchWrite/],
},
express: {
enabled: this.plugins.express === true ? true : false,
path: "@opentelemetry/plugin-express",
},
mysql: {
enabled: this.plugins.mysql === true ? true : false,
path: "@opentelemetry/plugin-mysql",
},
"@grpc/grpc-js": {
enabled: this.plugins.grpc === true ? true : false,
path: "@opentelemetry/plugin-grpc-js",
},
dns: {
enabled: this.plugins.dns === true ? true : false,
path: "@opentelemetry/plugin-dns",
},
graphql: {
enabled: this.plugins.graphql === true ? true : false,
path: "@opentelemetry/instrumentation-graphql",
},
},
});
// Register the tracer
tracerProvider.register();
opentelemetry.trace.setGlobalTracerProvider(tracerProvider);
// Create an exporter for sending spans data
// Initialize the exporter
const exporter = new TraceExporter({
projectId: this.projectId,
keyFilename: this.keyPath,
});
// Configure a span processor for the tracer
tracerProvider.addSpanProcessor(new BatchSpanProcessor(exporter));
const name = this.projectId;
const version = "0.1.0";
const tracer = opentelemetry.trace.getTracer(name, version);
return tracer;
}
/**
* Returns the opentelemetry construct as api
*/
getApi() {
return opentelemetry;
}
};