This repository was archived by the owner on Aug 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathOpenTelemetryService.node.ts
More file actions
92 lines (75 loc) · 3.51 KB
/
Copy pathOpenTelemetryService.node.ts
File metadata and controls
92 lines (75 loc) · 3.51 KB
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
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { Resource } from '@opentelemetry/resources'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import {
type ConfigurationWithAccessToken,
FeatureFlag,
featureFlagProvider,
} from '@sourcegraph/cody-shared'
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { version } from '../../version'
import { CodyTraceExporter } from './CodyTraceExport'
import { ConsoleBatchSpanExporter } from './console-batch-span-exporter'
export type OpenTelemetryServiceConfig = Pick<
ConfigurationWithAccessToken,
'serverEndpoint' | 'experimentalTracing' | 'debugVerbose'
>
export class OpenTelemetryService {
private tracerProvider?: NodeTracerProvider
private unloadInstrumentations?: () => void
private isTracingEnabled = false
private lastTraceUrl: string | undefined
// We use a single promise object that we chain on to, to avoid multiple reconfigure calls to
// be run in parallel
private reconfigurePromiseMutex: Promise<void> = Promise.resolve()
constructor(protected config: OpenTelemetryServiceConfig) {
this.reconfigurePromiseMutex = this.reconfigurePromiseMutex.then(() => this.reconfigure())
}
public onConfigurationChange(newConfig: OpenTelemetryServiceConfig): void {
this.config = newConfig
this.reconfigurePromiseMutex = this.reconfigurePromiseMutex.then(() => this.reconfigure())
}
private async reconfigure(): Promise<void> {
this.isTracingEnabled =
this.config.experimentalTracing ||
(await featureFlagProvider.evaluateFeatureFlag(FeatureFlag.CodyAutocompleteTracing))
const traceUrl = new URL('/-/debug/otlp/v1/traces', this.config.serverEndpoint).toString()
if (this.lastTraceUrl === traceUrl) {
return
}
this.lastTraceUrl = traceUrl
const logLevel = this.config.debugVerbose ? DiagLogLevel.INFO : DiagLogLevel.ERROR
diag.setLogger(new DiagConsoleLogger(), logLevel)
await this.reset()
this.unloadInstrumentations = registerInstrumentations({
instrumentations: [new HttpInstrumentation()],
})
this.configureTracerProvider(traceUrl)
}
public configureTracerProvider(traceUrl: string): void {
this.tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'cody-client',
[SemanticResourceAttributes.SERVICE_VERSION]: version,
}),
})
// Add the default tracer exporter used in production.
this.tracerProvider.addSpanProcessor(
new BatchSpanProcessor(
new CodyTraceExporter({ traceUrl, isTracingEnabled: this.isTracingEnabled })
)
)
// Add the console exporter used in development for verbose logging and debugging.
if (process.env.NODE_ENV === 'development' || this.config.debugVerbose) {
this.tracerProvider.addSpanProcessor(new BatchSpanProcessor(new ConsoleBatchSpanExporter()))
}
this.tracerProvider.register()
}
public async reset(): Promise<void> {
await this.tracerProvider?.shutdown()
this.unloadInstrumentations?.()
}
}