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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
import type { Context, Span, Tracer } from "@opentelemetry/api";
import {
context,
propagation,
SpanKind,
SpanStatusCode,
trace,
} from "@opentelemetry/api";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { resourceFromAttributes } from "@opentelemetry/resources";
import {
BatchSpanProcessor,
ConsoleSpanExporter,
ParentBasedSampler,
SimpleSpanProcessor,
TraceIdRatioBasedSampler,
} from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import {
ATTR_SERVICE_NAME,
ATTR_SERVICE_VERSION,
} from "@opentelemetry/semantic-conventions";
import serverConfig from "@karakeep/shared/config";
import logger from "@karakeep/shared/logger";
import type { TracingAttributes } from "./tracingTypes";
export type { TracingAttributeKey, TracingAttributes } from "./tracingTypes";
let tracerProvider: NodeTracerProvider | null = null;
let isInitialized = false;
/**
* Initialize the OpenTelemetry tracing infrastructure.
* Should be called once at application startup.
*/
export function initTracing(serviceSuffix?: string): void {
if (isInitialized) {
logger.debug("Tracing already initialized, skipping");
return;
}
if (!serverConfig.tracing.enabled) {
logger.info("Tracing is disabled");
isInitialized = true;
return;
}
const serviceName = serviceSuffix
? `${serverConfig.tracing.serviceName}-${serviceSuffix}`
: serverConfig.tracing.serviceName;
logger.info(`Initializing OpenTelemetry tracing for service: ${serviceName}`);
const resource = resourceFromAttributes({
[ATTR_SERVICE_NAME]: serviceName,
[ATTR_SERVICE_VERSION]: serverConfig.serverVersion ?? "unknown",
});
// Configure span processors
const spanProcessors = [];
if (serverConfig.tracing.otlpEndpoint) {
// OTLP exporter (Jaeger, Zipkin, etc.)
const otlpExporter = new OTLPTraceExporter({
url: serverConfig.tracing.otlpEndpoint,
});
spanProcessors.push(new BatchSpanProcessor(otlpExporter));
logger.info(
`OTLP exporter configured: ${serverConfig.tracing.otlpEndpoint}`,
);
} else {
// Fallback to console exporter for development
spanProcessors.push(new SimpleSpanProcessor(new ConsoleSpanExporter()));
logger.info("Console span exporter configured (no OTLP endpoint set)");
}
tracerProvider = new NodeTracerProvider({
resource,
sampler: new ParentBasedSampler({
root: new TraceIdRatioBasedSampler(serverConfig.tracing.sampleRate),
}),
spanProcessors,
});
// Register the provider globally
tracerProvider.register();
isInitialized = true;
logger.info("OpenTelemetry tracing initialized successfully");
}
/**
* Shutdown the tracing infrastructure gracefully.
* Should be called on application shutdown.
*/
export async function shutdownTracing(): Promise<void> {
if (tracerProvider) {
await tracerProvider.shutdown();
logger.info("OpenTelemetry tracing shut down");
}
}
/**
* Get a tracer instance for creating spans.
* @param name - The name of the tracer (typically the module/component name)
*/
export function getTracer(name: string): Tracer {
return trace.getTracer(name);
}
/**
* Get the currently active span, if any.
*/
export function getActiveSpan(): Span | undefined {
return trace.getActiveSpan();
}
/**
* Get the current trace context.
*/
export function getActiveContext(): Context {
return context.active();
}
/**
* Execute a function within a new span.
* Automatically handles error recording and span status.
*/
export async function withSpan<T>(
tracer: Tracer,
spanName: string,
options: {
kind?: SpanKind;
attributes?: TracingAttributes;
},
fn: (span: Span) => Promise<T>,
): Promise<T> {
return tracer.startActiveSpan(
spanName,
{
kind: options.kind ?? SpanKind.INTERNAL,
attributes: options.attributes,
},
async (span) => {
try {
const result = await fn(span);
span.setStatus({ code: SpanStatusCode.OK });
return result;
} catch (error) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: error instanceof Error ? error.message : String(error),
});
span.recordException(
error instanceof Error ? error : new Error(String(error)),
);
throw error;
} finally {
span.end();
}
},
);
}
/**
* Execute a synchronous function within a new span.
*/
export function withSpanSync<T>(
tracer: Tracer,
spanName: string,
options: {
kind?: SpanKind;
attributes?: TracingAttributes;
},
fn: (span: Span) => T,
): T {
const span = tracer.startSpan(spanName, {
kind: options.kind ?? SpanKind.INTERNAL,
attributes: options.attributes,
});
try {
const result = context.with(trace.setSpan(context.active(), span), () =>
fn(span),
);
span.setStatus({ code: SpanStatusCode.OK });
return result;
} catch (error) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: error instanceof Error ? error.message : String(error),
});
span.recordException(
error instanceof Error ? error : new Error(String(error)),
);
throw error;
} finally {
span.end();
}
}
/**
* Add an event to the current active span.
*/
export function addSpanEvent(
name: string,
attributes?: Record<string, string | number | boolean>,
): void {
const span = getActiveSpan();
if (span) {
span.addEvent(name, attributes);
}
}
/**
* Set attributes on the current active span.
*/
export function setSpanAttributes(attributes: TracingAttributes): void {
const span = getActiveSpan();
if (span) {
span.setAttributes(attributes);
}
}
/**
* Record an error on the current active span.
*/
export function recordSpanError(error: Error): void {
const span = getActiveSpan();
if (span) {
span.recordException(error);
span.setStatus({
code: SpanStatusCode.ERROR,
message: error.message,
});
}
}
/**
* Extract trace context from HTTP headers (for distributed tracing).
*/
export function extractTraceContext(
headers: Record<string, string | string[] | undefined>,
): Context {
const normalizedHeaders: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
if (value) {
normalizedHeaders[key] = Array.isArray(value) ? value[0] : value;
}
}
return propagation.extract(context.active(), normalizedHeaders);
}
/**
* Inject trace context into HTTP headers (for distributed tracing).
*/
export function injectTraceContext(
headers: Record<string, string>,
): Record<string, string> {
propagation.inject(context.active(), headers);
return headers;
}
/**
* Run a function within a specific context.
*/
export function runWithContext<T>(ctx: Context, fn: () => T): T {
return context.with(ctx, fn);
}
// Re-export commonly used types and constants
export { SpanKind, SpanStatusCode } from "@opentelemetry/api";
|