-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from pullflow/epic/telemetry
Epic: Telemetry for extension
- Loading branch information
Showing
17 changed files
with
323 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* eslint-disable no-unused-vars */ | ||
import { Span, Tracer, SpanKind, Attributes } from '@opentelemetry/api' | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http' | ||
import { Resource } from '@opentelemetry/resources' | ||
import { | ||
BasicTracerProvider, | ||
BatchSpanProcessor, | ||
} from '@opentelemetry/sdk-trace-base' | ||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' | ||
import { ExtensionContext, extensions } from 'vscode' | ||
import { TraceAttributes } from './types' | ||
import { Store } from './store' | ||
import { AppConfig } from './appConfig' | ||
import { log } from './logger' | ||
|
||
const extensionInfo = extensions.getExtension('Pullflow.pullflow')?.packageJSON | ||
|
||
type FakeTracer = {} | ||
type FakeBasicTracerProvider = {} | ||
type FakeAttribute = {} | ||
|
||
export function instantiatePullflowTracer(context: ExtensionContext) { | ||
const { isTelemetryEnabled } = Store.get(context) | ||
if (isTelemetryEnabled) { | ||
return new Trace(context) | ||
} else { | ||
return new FakeTrace(context) | ||
} | ||
} | ||
class FakeTrace { | ||
tracer: FakeTracer | ||
provider: FakeBasicTracerProvider | ||
defaultAttributes: FakeAttribute | ||
|
||
constructor(_context: ExtensionContext) { | ||
this.provider = {} | ||
this.tracer = {} | ||
this.defaultAttributes = {} | ||
} | ||
|
||
dispose(): void {} | ||
start({}: { name: string; attributes?: TraceAttributes }) {} | ||
end({}: { attributes?: TraceAttributes }) {} | ||
} | ||
class Trace { | ||
tracer: Tracer | ||
provider: BasicTracerProvider | ||
defaultAttributes: Attributes | ||
span: Span | undefined | ||
|
||
constructor(context: ExtensionContext) { | ||
this.provider = new BasicTracerProvider({ | ||
resource: new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: extensionInfo.name, | ||
[SemanticResourceAttributes.SERVICE_VERSION]: extensionInfo.version, | ||
}), | ||
}) | ||
|
||
const exporter = new OTLPTraceExporter({ | ||
url: AppConfig.pullflow.telemetryUrl + '/v1/traces', | ||
compression: 'gzip' as any, | ||
}) | ||
this.provider.addSpanProcessor(new BatchSpanProcessor(exporter)) | ||
this.tracer = this.provider.getTracer(extensionInfo.name) | ||
const { user } = Store.get(context) | ||
this.defaultAttributes = user || {} | ||
this.span = undefined | ||
} | ||
|
||
dispose(): void { | ||
void this.provider.shutdown() | ||
} | ||
|
||
start({ name, attributes }: { name: string; attributes?: TraceAttributes }) { | ||
this.span = this.tracer.startSpan(name, { | ||
kind: SpanKind.INTERNAL, | ||
startTime: Date.now(), | ||
}) | ||
if (attributes) | ||
this.span.setAttributes({ | ||
...attributes, | ||
...this.defaultAttributes, | ||
}) | ||
} | ||
|
||
end({ attributes }: { attributes?: TraceAttributes }): void { | ||
if (this.span === undefined) { | ||
log.warn('span is undefined', 'trace.ts') | ||
return | ||
} | ||
if (attributes) | ||
this.span.setAttributes({ | ||
...attributes, | ||
...this.defaultAttributes, | ||
}) | ||
this.span.end(Date.now()) | ||
this.span = undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
import { QuickPickItem, window } from 'vscode' | ||
import { ExtensionContext, QuickPickItem, window } from 'vscode' | ||
import { instantiatePullflowTracer } from '../../utils/trace' | ||
|
||
export const QuickPick = { | ||
create: <Type extends QuickPickItem>({ | ||
context, | ||
items, | ||
title, | ||
placeholder, | ||
onDidChangeSelection, | ||
}: { | ||
context: ExtensionContext | ||
items: Type[] | ||
title: string | ||
placeholder: string | ||
onDidChangeSelection: (selection: readonly Type[]) => void | ||
}) => { | ||
const quickPick = window.createQuickPick<Type>() | ||
|
||
const trace = instantiatePullflowTracer(context) | ||
trace.start({ | ||
name: title, | ||
}) | ||
|
||
quickPick.items = items | ||
quickPick.title = title | ||
quickPick.placeholder = placeholder | ||
quickPick.onDidChangeSelection(onDidChangeSelection) | ||
quickPick.onDidHide(() => quickPick.dispose()) | ||
quickPick.onDidAccept(() => quickPick.dispose()) | ||
|
||
quickPick.onDidHide(() => { | ||
trace.end({ | ||
attributes: { | ||
title, | ||
}, | ||
}) | ||
quickPick.dispose() | ||
}) | ||
|
||
quickPick.onDidAccept(() => { | ||
trace.end({ | ||
attributes: { | ||
title, | ||
selectedItem: quickPick.selectedItems[0]?.label, | ||
}, | ||
}) | ||
quickPick.dispose() | ||
}) | ||
|
||
quickPick.show() | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.