1
>
/*---------------------------------------------------------------------------------------------
outboundForwarder.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import type * as http from 'http';
7
>
import type * as https from 'https';
8
>
import { Queue } from '../../../../base/common/async.js';
9
>
import { Disposable, toDisposable } from '../../../../base/common/lifecycle.js';
10
>
import { URL } from 'url';
11
>
import { promises as fs } from 'fs';
12
>
import { ILogService } from '../../../log/common/log.js';
13
>
import { ICompletedSpanData } from '../../common/spanData.js';
14
>
import { IDecodeResult } from './otlpJsonDecode.js';
15
>
16
>
/**
17
>
* Fan-out target for span data the loopback receiver collects. Each
18
>
* implementation isolates failures so that the receiver's response to the SDK
19
>
* is never blocked by an upstream problem.
20
>
*/
21
>
export interface IOutboundForwarder {
22
>
/** Forward raw OTLP/HTTP request bytes unchanged (for OTLP exporters). */
23
>
forwardRaw?(body: Buffer, contentType: string): void;
24
>
/** Forward decoded spans (for console / file / structured sinks). */
25
>
forwardSpans?(result: IDecodeResult): void;
26
>
/**
27
>
* Drain any in-flight work. Best-effort; never throws. Callers should
28
>
* await this on shutdown so file/network writes have a chance to complete.
29
>
*/
30
>
flush(): Promise<void>;
31
>
dispose(): void;
32
>
}
33
>
34
>
// ---------------------------------------------------------------------------
35
>
// OTLP/HTTP forwarder: re-POST the raw body to a remote OTLP collector.
36
>
// ---------------------------------------------------------------------------
37
>
38
>
export interface IOtlpHttpForwarderOptions {
39
>
/**
40
>
* Target URL. May be either:
41
>
*
42
>
* - A full signal-specific URL (`http://host:4318/v1/traces`) — used verbatim.
43
>
* - A bare base URL (`http://host:4318` or `http://host:4318/`) — `/v1/traces`
44
>
* is auto-appended, matching the `OTEL_EXPORTER_OTLP_ENDPOINT` convention
45
>
* used by OTLP exporters in the official OpenTelemetry SDKs.
46
>
*/
47
>
readonly endpoint: string;
48
>
/** Extra headers (e.g. authorization). */
49
>
readonly headers?: Readonly<Record<string, string>>;
50
>
/** Per-request timeout in ms. Defaults to 10s. */
51
>
readonly timeoutMs?: number;
52
>
}
53
>
54
>
/**
55
>
* Resolve an OTLP/HTTP traces endpoint, matching the path-handling rules used
56
>
* by the OpenTelemetry SDKs: a bare base URL (no path or just `/`) gets
57
>
* `/v1/traces` appended; any other path is used verbatim.
58
>
*
59
>
* Returns the input string when it cannot be parsed as a URL so the caller's
60
>
* error path remains in charge.
61
>
*/
62
>
export function resolveOtlpTracesEndpoint(endpoint: string): string {
63
try {
64
const url = new URL(endpoint);