Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Client Platform and agentVersion to Exported Traces in OpenTelemetry Service #6889

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion vscode/src/services/open-telemetry/CodyTraceExport.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { ExportResult } from '@opentelemetry/core'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'
import { CodyIDE } from '@sourcegraph/cody-shared/src/configuration'
import type { OpenTelemetryServiceConfig } from './OpenTelemetryService.node'

const MAX_TRACE_RETAIN_MS = 60 * 1000

export class CodyTraceExporter extends OTLPTraceExporter {
private queuedSpans: Map<string, { span: ReadableSpan; enqueuedAt: number }> = new Map()

private clientPlatform: CodyIDE
private agentVersion?: string
constructor(private configAccessor: () => OpenTelemetryServiceConfig | null) {
super({
url: configAccessor()?.traceUrl,
Expand All @@ -18,6 +20,8 @@ export class CodyTraceExporter extends OTLPTraceExporter {
},
httpAgentOptions: { rejectUnauthorized: false },
})
this.clientPlatform = configAccessor()?.clientPlatform ?? CodyIDE.VSCode
this.agentVersion = configAccessor()?.agentVersion
}

export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void {
Expand All @@ -43,6 +47,8 @@ export class CodyTraceExporter extends OTLPTraceExporter {
const spanMap = new Map<string, ReadableSpan>()
for (const span of spans) {
spanMap.set(span.spanContext().spanId, span)
span.attributes.clientPlatform = this.clientPlatform
span.attributes.agentVersion = this.agentVersion
}

const spansToExport: ReadableSpan[] = []
Expand Down
10 changes: 7 additions & 3 deletions vscode/src/services/open-telemetry/OpenTelemetryService.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'

import {
type CodyIDE,
FeatureFlag,
type Unsubscribable,
clientCapabilities,
combineLatest,
featureFlagProvider,
resolvedConfig,
Expand All @@ -15,7 +17,6 @@ import {
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { isEqual } from 'lodash'
import { version } from '../../version'
import { CodyTraceExporter } from './CodyTraceExport'
import { ConsoleBatchSpanExporter } from './console-batch-span-exporter'

Expand All @@ -24,6 +25,8 @@ export interface OpenTelemetryServiceConfig {
traceUrl: string
accessToken: string | null
debugVerbose: boolean
clientPlatform: CodyIDE
agentVersion?: string
}
export class OpenTelemetryService {
private tracerProvider?: NodeTracerProvider
Expand All @@ -45,7 +48,7 @@ export class OpenTelemetryService {
this.tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'cody-client',
[SemanticResourceAttributes.SERVICE_VERSION]: version,
[SemanticResourceAttributes.SERVICE_VERSION]: clientCapabilities().agentExtensionVersion,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the same value so I wanted to maintain consistency in the usage of agentExtensionVersion

}),
})
// Register once at startup
Expand All @@ -67,8 +70,9 @@ export class OpenTelemetryService {
traceUrl: traceUrl,
debugVerbose: configuration.debugVerbose,
accessToken: auth.accessToken,
clientPlatform: clientCapabilities().agentIDE,
agentVersion: clientCapabilities().agentExtensionVersion,
}

if (isEqual(this.lastConfig, newConfig)) {
return
}
Expand Down
4 changes: 2 additions & 2 deletions vscode/webviews/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ export const App: React.FunctionComponent<{ vscodeAPI: VSCodeWrapper }> = ({ vsc
webviewTelemetryService.configure({
isTracingEnabled: true,
debugVerbose: true,
agentIDE: config.clientCapabilities.agentIDE,
extensionAgentVersion: config.clientCapabilities.agentExtensionVersion,
clientPlatform: config.clientCapabilities.agentIDE,
agentVersion: config.clientCapabilities.agentExtensionVersion,
})
}
}, [config, webviewTelemetryService])
Expand Down
22 changes: 11 additions & 11 deletions vscode/webviews/utils/webviewOpenTelemetryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api'
import { Resource } from '@opentelemetry/resources'
import { BatchSpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import type { CodyIDE } from '@sourcegraph/cody-shared/src/configuration'
import { CodyIDE } from '@sourcegraph/cody-shared/src/configuration'
import { CodyTraceExporterWeb } from '../../src/services/open-telemetry/CodyTraceExportWeb'

// This class is used to initialize and manage the OpenTelemetry service for the webview.
Expand All @@ -14,8 +14,8 @@ export class WebviewOpenTelemetryService {
private unloadInstrumentations?: () => void
private isTracingEnabled = false
private isInitialized = false
private agentIDE?: CodyIDE
private extensionAgentVersion?: string
private clientPlatform?: CodyIDE
private agentVersion?: string
constructor() {
if (!WebviewOpenTelemetryService.instance) {
WebviewOpenTelemetryService.instance = this
Expand All @@ -26,8 +26,8 @@ export class WebviewOpenTelemetryService {
public configure(options?: {
isTracingEnabled?: boolean
debugVerbose?: boolean
agentIDE?: CodyIDE
extensionAgentVersion?: string
clientPlatform?: CodyIDE
agentVersion?: string
}): void {
// If the service is already initialized or if it is not the instance that is being used, return
if (this.isInitialized || WebviewOpenTelemetryService.instance !== this) {
Expand All @@ -37,12 +37,12 @@ export class WebviewOpenTelemetryService {
const {
isTracingEnabled = true,
debugVerbose = false,
agentIDE,
extensionAgentVersion,
clientPlatform,
agentVersion,
} = options || {}
this.isTracingEnabled = isTracingEnabled
this.agentIDE = agentIDE
this.extensionAgentVersion = extensionAgentVersion
this.clientPlatform = clientPlatform
this.agentVersion = agentVersion
const logLevel = debugVerbose ? DiagLogLevel.INFO : DiagLogLevel.ERROR
diag.setLogger(new DiagConsoleLogger(), logLevel)

Expand All @@ -58,8 +58,8 @@ export class WebviewOpenTelemetryService {
new BatchSpanProcessor(
new CodyTraceExporterWeb({
isTracingEnabled: true,
clientPlatform: this.agentIDE ?? ('defaultIDE' as CodyIDE),
agentVersion: this.extensionAgentVersion,
clientPlatform: this.clientPlatform ?? CodyIDE.VSCode,
agentVersion: this.agentVersion,
})
)
)
Expand Down
Loading