-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrace.ts
More file actions
58 lines (52 loc) · 1.74 KB
/
trace.ts
File metadata and controls
58 lines (52 loc) · 1.74 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
import { IOtlpExportDelegate } from "./node_modules/@opentelemetry/otlp-exporter-base/build/src/otlp-export-delegate";
import { ReadableSpan, SpanExporter } from "@opentelemetry/sdk-trace-base";
import {
OTLPExporterBase,
OTLPExporterConfigBase,
} from "@opentelemetry/otlp-exporter-base";
import { ExportResult, ExportResultCode } from "@opentelemetry/core";
import { JsonTraceSerializer } from "@opentelemetry/otlp-transformer";
import { name, version } from "./package.json";
// https://opentelemetry.io/docs/specs/otel/trace/sdk/#span-exporter
export class FetchExportDelegate
implements IOtlpExportDelegate<ReadableSpan[]>
{
private spans: ReadableSpan[] = [];
private config: OTLPExporterConfigBase;
constructor(config: OTLPExporterConfigBase) {
this.config = config;
}
async export(
spans: ReadableSpan[],
callback: (result: ExportResult) => void,
) {
// Batch up and rely on forceFlush()
this.spans = this.spans.concat(spans);
callback({ code: ExportResultCode.SUCCESS });
}
async forceFlush(): Promise<void> {
const response = await fetch(`${this.config.url}/v1/traces`, {
method: "POST",
headers: Object.assign(
{
"User-Agent": `${name} v${version}`,
"Content-Type": "application/json",
},
this.config.headers,
),
body: JsonTraceSerializer.serializeRequest(this.spans),
});
console.log(response.status);
console.log([...response.headers.entries()]);
console.log(await response.json());
}
async shutdown(): Promise<void> {}
}
export class SpanExporterFetch
extends OTLPExporterBase<ReadableSpan[]>
implements SpanExporter
{
constructor(config?: OTLPExporterConfigBase) {
super(new FetchExportDelegate(config));
}
}