Skip to content

Commit

Permalink
Autocomplete: fix OpenTelemetry exporter with multiple processors (#…
Browse files Browse the repository at this point in the history
…3270)

Currently, `NodeSDK` does not support multiple-span processors and exporters. This functionality [was added](open-telemetry/opentelemetry-js#4454) three weeks ago and will be included in [the following experimental release](open-telemetry/opentelemetry-js#4504). To fix the problem without waiting for the release, I migrated this functionality to lower-level primitives, where we have complete control over the number of exporters.
  • Loading branch information
valerybugakov authored and steveyegge committed Mar 13, 2024
1 parent e6143f7 commit 61d261d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1062,10 +1062,12 @@
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/core": "^1.18.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.45.1",
"@opentelemetry/instrumentation": "^0.45.1",
"@opentelemetry/instrumentation-http": "^0.45.1",
"@opentelemetry/resources": "^1.18.1",
"@opentelemetry/sdk-node": "^0.45.1",
"@opentelemetry/sdk-trace-base": "^1.18.1",
"@opentelemetry/sdk-trace-node": "^1.18.1",
"@opentelemetry/semantic-conventions": "^1.18.1",
"@sentry/browser": "^7.66.0",
"@sentry/core": "^7.66.0",
Expand Down
46 changes: 31 additions & 15 deletions vscode/src/services/open-telemetry/OpenTelemetryService.node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { Resource } from '@opentelemetry/resources'
import { NodeSDK } from '@opentelemetry/sdk-node'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'

import {
Expand All @@ -21,7 +22,9 @@ export type OpenTelemetryServiceConfig = Pick<
>

export class OpenTelemetryService {
private sdk: NodeSDK | undefined
private tracerProvider?: NodeTracerProvider
private unloadInstrumentations?: () => void

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
Expand Down Expand Up @@ -51,27 +54,40 @@ export class OpenTelemetryService {
}
this.lastTraceUrl = traceUrl

const logLevel = this.config.debugVerbose ? DiagLogLevel.DEBUG : DiagLogLevel.ERROR
const logLevel = this.config.debugVerbose ? DiagLogLevel.INFO : DiagLogLevel.ERROR
diag.setLogger(new DiagConsoleLogger(), logLevel)

await this.sdk?.shutdown()
this.sdk = undefined
await this.reset()

this.sdk = new NodeSDK({
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,
}),
instrumentations: [new HttpInstrumentation()],
traceExporter: new OTLPTraceExporter({ url: traceUrl }),
})

// Disable default process logging. We do not care about the VS Code extension process
autoDetectResources: false,
// Add the default tracer exporter used in production.
this.tracerProvider.addSpanProcessor(
new BatchSpanProcessor(new OTLPTraceExporter({ url: traceUrl }))
)

...((process.env.NODE_ENV === 'development' || this.config.debugVerbose) && {
spanProcessor: new BatchSpanProcessor(new ConsoleBatchSpanExporter()),
}),
})
this.sdk.start()
// 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?.()
}
}

0 comments on commit 61d261d

Please sign in to comment.