This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracing.ts
46 lines (39 loc) · 1.81 KB
/
tracing.ts
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
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { SimpleSpanProcessor, BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { trace, Tracer } from "@opentelemetry/api";
import { JaegerExporter } from "@opentelemetry/exporter-jaeger";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { PrismaInstrumentation } from '@prisma/instrumentation';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-base'
export default function initializeTracing(serviceName: string): Tracer {
const traceRatio = process.env.NODE_ENV === 'production' ? 0.1 : 1.0;
const provider = new NodeTracerProvider({
sampler: new TraceIdRatioBasedSampler(traceRatio),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}),
});
const jaegerExporter = new JaegerExporter({
endpoint: "http://localhost:14268/api/traces",
});
if (process.env.NODE_ENV === 'production') {
provider.addSpanProcessor(new BatchSpanProcessor(jaegerExporter))
} else {
provider.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter))
}
provider.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));
registerInstrumentations({
instrumentations: [
new PrismaInstrumentation(),
new HttpInstrumentation(),
new ExpressInstrumentation(),
],
tracerProvider: provider,
});
provider.register();
return trace.getTracer(serviceName);
};