-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdk.ts
149 lines (130 loc) · 4.69 KB
/
sdk.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { DiagConsoleLogger, type TextMapPropagator, diag, metrics, Attributes, DiagLogger } from "./opentelemetry/api.js";
import { logs } from "./opentelemetry/api-logs.js";
import { OTLPMetricExporterBase } from "./opentelemetry/exporter-metrics-otlp-http.js";
import { type InstrumentationOption, registerInstrumentations } from "./opentelemetry/instrumentation.js";
import {
type DetectorSync, Resource,
detectResourcesSync,
envDetectorSync,
hostDetectorSync,
osDetectorSync,
} from "./opentelemetry/resources.js";
// The SDKs for each signal
import { BasicTracerProvider, BatchSpanProcessor, SpanExporter, type IdGenerator, type Sampler } from "./opentelemetry/sdk-trace-base.js";
import { MeterProvider, PeriodicExportingMetricReader, type View } from "./opentelemetry/sdk-metrics.js";
import { BatchLogRecordProcessor, LogRecordExporter, LoggerProvider } from "./opentelemetry/sdk-logs.js";
// Our Deno-specific implementations
import {
DenoDeployDetector,
DenoProcessDetector,
DenoRuntimeDetector,
} from "./otel-platform/detectors.ts";
import {
DenoAsyncHooksContextManager,
} from "./otel-platform/context-manager.ts";
import {
OTLPTracesExporter,
OTLPMetricsExporter,
OTLPLogsExporter,
} from "./otel-platform/otlp-json-exporters.ts";
import { getEnv } from "./opentelemetry/core.js";
import { getDenoAutoInstrumentations } from "./instrumentation/auto.ts";
/**
* A one-stop shop to provide a tracer, a meter, and a logger.
* Transmits all signals by OTLP.
*/
export class DenoTelemetrySdk {
public readonly resource: Resource;
public readonly tracer: BasicTracerProvider;
public readonly meter: MeterProvider;
public readonly logger: LoggerProvider;
constructor(props?: {
diagLogger?: DiagLogger;
detectors?: DetectorSync[];
resource?: Resource;
resourceAttrs?: Attributes;
instrumentations?: InstrumentationOption[];
propagator?: TextMapPropagator;
idGenerator?: IdGenerator;
sampler?: Sampler;
metricsExportIntervalMillis?: number;
metricsViews?: View[];
otlpEndpointBase?: string;
tracesExporter?: SpanExporter;
// metricsExporter?: ;
logsExporter?: LogRecordExporter;
}) {
// if (env.OTEL_SDK_DISABLED) {
// return this; // TODO: better?
// }
const env = getEnv();
diag.setLogger(props?.diagLogger ?? new DiagConsoleLogger(), env.OTEL_LOG_LEVEL);
this.resource = detectResourcesSync({
detectors: props?.detectors ?? getDefaultDetectors(),
});
if (props?.resource) {
this.resource = this.resource.merge(props.resource);
}
if (props?.resourceAttrs) {
this.resource = this.resource.merge(new Resource(props.resourceAttrs));
}
this.tracer = new BasicTracerProvider({
resource: this.resource,
idGenerator: props?.idGenerator,
sampler: props?.sampler,
});
this.tracer.register({
contextManager: new DenoAsyncHooksContextManager().enable(),
propagator: props?.propagator,
});
this.tracer.addSpanProcessor(new BatchSpanProcessor(props?.tracesExporter
?? new OTLPTracesExporter({
resourceBase: props?.otlpEndpointBase,
})));
this.meter = new MeterProvider({
resource: this.resource,
views: props?.metricsViews,
// Metrics export on a fixed timer, so make the user opt-in to them
readers: ((props?.metricsExportIntervalMillis ?? 0) > 0) ? [
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporterBase(new OTLPMetricsExporter({
resourceBase: props?.otlpEndpointBase,
})),
exportIntervalMillis: props?.metricsExportIntervalMillis,
})
] : [],
});
metrics.setGlobalMeterProvider(this.meter);
this.logger = new LoggerProvider({
resource: this.resource,
});
logs.setGlobalLoggerProvider(this.logger);
this.logger.addLogRecordProcessor(new BatchLogRecordProcessor(props?.logsExporter
?? new OTLPLogsExporter({
resourceBase: props?.otlpEndpointBase,
})));
registerInstrumentations({
tracerProvider: this.tracer,
meterProvider: this.meter,
loggerProvider: this.logger,
instrumentations: props?.instrumentations ?? getDenoAutoInstrumentations(),
});
}
}
function getDefaultDetectors(): DetectorSync[] {
// We first check for Deno Deploy then decide what we want to detect based on that
const denoDeployDetector = new DenoDeployDetector();
const runtimeDetectors =
Object.keys(denoDeployDetector.detect().attributes).length
? [denoDeployDetector]
: [
new DenoProcessDetector(),
hostDetectorSync,
osDetectorSync,
];
return [
new DenoRuntimeDetector(),
...runtimeDetectors,
envDetectorSync,
];
}