From 11a6d5160719ccd47257ab07a429e81f79949a99 Mon Sep 17 00:00:00 2001 From: Valery Bugakov Date: Tue, 27 Feb 2024 15:25:03 +0800 Subject: [PATCH] Autocomplete: fix OpenTelemetry exporter --- pnpm-lock.yaml | 6 +++ vscode/package.json | 2 + .../OpenTelemetryService.node.ts | 46 +++++++++++++------ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a69074993b28..8eab55f5893f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -269,6 +269,9 @@ importers: '@opentelemetry/exporter-trace-otlp-http': specifier: ^0.45.1 version: 0.45.1(@opentelemetry/api@1.7.0) + '@opentelemetry/instrumentation': + specifier: ^0.45.1 + version: 0.45.1(@opentelemetry/api@1.7.0) '@opentelemetry/instrumentation-http': specifier: ^0.45.1 version: 0.45.1(@opentelemetry/api@1.7.0) @@ -281,6 +284,9 @@ importers: '@opentelemetry/sdk-trace-base': specifier: ^1.18.1 version: 1.18.1(@opentelemetry/api@1.7.0) + '@opentelemetry/sdk-trace-node': + specifier: ^1.18.1 + version: 1.18.1(@opentelemetry/api@1.7.0) '@opentelemetry/semantic-conventions': specifier: ^1.18.1 version: 1.18.1 diff --git a/vscode/package.json b/vscode/package.json index ab017b76b3e9..1f97ef0027e8 100644 --- a/vscode/package.json +++ b/vscode/package.json @@ -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", diff --git a/vscode/src/services/open-telemetry/OpenTelemetryService.node.ts b/vscode/src/services/open-telemetry/OpenTelemetryService.node.ts index 7bdd6fc7fdd6..0937341a1f0c 100644 --- a/vscode/src/services/open-telemetry/OpenTelemetryService.node.ts +++ b/vscode/src/services/open-telemetry/OpenTelemetryService.node.ts @@ -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 { @@ -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 @@ -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 { + await this.tracerProvider?.shutdown() + this.unloadInstrumentations?.() } }