@vymalo/opencode-core-otel 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/config.d.ts +39 -0
- package/dist/config.js +237 -0
- package/dist/config.js.map +1 -0
- package/dist/deferred.d.ts +22 -0
- package/dist/deferred.js +29 -0
- package/dist/deferred.js.map +1 -0
- package/dist/export-logging.d.ts +28 -0
- package/dist/export-logging.js +74 -0
- package/dist/export-logging.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/instruments.d.ts +29 -0
- package/dist/instruments.js +103 -0
- package/dist/instruments.js.map +1 -0
- package/dist/lib.d.ts +11 -0
- package/dist/lib.js +12 -0
- package/dist/lib.js.map +1 -0
- package/dist/logging.d.ts +16 -0
- package/dist/logging.js +69 -0
- package/dist/logging.js.map +1 -0
- package/dist/propagation.d.ts +22 -0
- package/dist/propagation.js +57 -0
- package/dist/propagation.js.map +1 -0
- package/dist/providers.d.ts +66 -0
- package/dist/providers.js +242 -0
- package/dist/providers.js.map +1 -0
- package/dist/recorder.d.ts +166 -0
- package/dist/recorder.js +795 -0
- package/dist/recorder.js.map +1 -0
- package/dist/token-source.d.ts +59 -0
- package/dist/token-source.js +125 -0
- package/dist/token-source.js.map +1 -0
- package/dist/types.d.ts +104 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/vcs.d.ts +63 -0
- package/dist/vcs.js +188 -0
- package/dist/vcs.js.map +1 -0
- package/package.json +79 -0
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { type EnvSource, parseCommand, parseKeyValueList, resolveOtelConfig, signalUrl, SIGNALS } from "./config.js";
|
|
2
|
+
export { buildResource, createProviders, describeError, type ExporterFactories, type TelemetryProviders } from "./providers.js";
|
|
3
|
+
export { createInstruments, detectLanguage, type Instruments } from "./instruments.js";
|
|
4
|
+
export { type RecorderDeps, TelemetryRecorder } from "./recorder.js";
|
|
5
|
+
export { createTokenSource, DEFAULT_REFRESH_MS, EXPIRY_SKEW_MS, readJwtExpiry, type CommandRunner, type TokenSource, type TokenSourceOptions } from "./token-source.js";
|
|
6
|
+
export { type ExporterLike, withFailureLogging } from "./export-logging.js";
|
|
7
|
+
export { describeRemote, type FileReader, parseRemoteFromConfig, readVcsInfo, resolveGitDirs, sanitizeRemoteUrl, type VcsInfo } from "./vcs.js";
|
|
8
|
+
export { DEFAULT_DEFERRED_TIMEOUT_MS, deferredAttribute, type DeferredAttribute } from "./deferred.js";
|
|
9
|
+
export { installTracePropagation, type PropagationConfigInput, type ProviderConfigLike } from "./propagation.js";
|
|
10
|
+
export { createJsonConsoleLogger, DEFAULT_LOG_LEVEL, fromOpenCodeLogLevel, type LogFields, type Logger, type LogLevel, LOG_LEVEL_PRIORITY } from "./logging.js";
|
|
11
|
+
export type { ExporterKind, MetricTemporality, OtelPluginOptions, ResolvedOtelConfig, SignalName } from "./types.js";
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { parseCommand, parseKeyValueList, resolveOtelConfig, signalUrl, SIGNALS } from "./config.js";
|
|
2
|
+
export { buildResource, createProviders, describeError } from "./providers.js";
|
|
3
|
+
export { createInstruments, detectLanguage } from "./instruments.js";
|
|
4
|
+
export { TelemetryRecorder } from "./recorder.js";
|
|
5
|
+
export { createTokenSource, DEFAULT_REFRESH_MS, EXPIRY_SKEW_MS, readJwtExpiry } from "./token-source.js";
|
|
6
|
+
export { withFailureLogging } from "./export-logging.js";
|
|
7
|
+
export { describeRemote, parseRemoteFromConfig, readVcsInfo, resolveGitDirs, sanitizeRemoteUrl } from "./vcs.js";
|
|
8
|
+
export { DEFAULT_DEFERRED_TIMEOUT_MS, deferredAttribute } from "./deferred.js";
|
|
9
|
+
export { installTracePropagation } from "./propagation.js";
|
|
10
|
+
export { createJsonConsoleLogger, DEFAULT_LOG_LEVEL, fromOpenCodeLogLevel, LOG_LEVEL_PRIORITY } from "./logging.js";
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=lib.js.map
|
package/dist/lib.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA,SAEE,cACA,mBACA,mBACA,WACA,eACK;AAEP,SACE,eACA,iBACA,qBAGK;AAEP,SAAS,mBAAmB,sBAAwC;AAEpE,SAA4B,yBAAyB;AAErD,SACE,mBACA,oBACA,gBACA,qBAIK;AAEP,SAA4B,0BAA0B;AAEtD,SACE,gBAEA,uBACA,aACA,gBACA,yBAEK;AAEP,SACE,6BACA,yBAEK;AAEP,SACE,+BAGK;AAEP,SACE,yBACA,mBACA,sBAIA,0BACK","names":[],"sources":["../src/lib.ts"],"version":3,"file":"lib.js","sourceRoot":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { LogLevel } from "./types.js";
|
|
2
|
+
export type { LogLevel } from "./types.js";
|
|
3
|
+
export declare const LOG_LEVEL_PRIORITY: Record<LogLevel, number>;
|
|
4
|
+
export declare const DEFAULT_LOG_LEVEL: LogLevel;
|
|
5
|
+
export interface LogFields {
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface Logger {
|
|
9
|
+
trace(event: string, fields?: LogFields): void;
|
|
10
|
+
debug(event: string, fields?: LogFields): void;
|
|
11
|
+
info(event: string, fields?: LogFields): void;
|
|
12
|
+
warn(event: string, fields?: LogFields): void;
|
|
13
|
+
error(event: string, fields?: LogFields): void;
|
|
14
|
+
}
|
|
15
|
+
export declare function createJsonConsoleLogger(minLevel?: LogLevel): Logger;
|
|
16
|
+
export declare function fromOpenCodeLogLevel(value: unknown): LogLevel | undefined;
|
package/dist/logging.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export const LOG_LEVEL_PRIORITY = {
|
|
2
|
+
trace: 5,
|
|
3
|
+
debug: 10,
|
|
4
|
+
info: 20,
|
|
5
|
+
warn: 30,
|
|
6
|
+
error: 40
|
|
7
|
+
};
|
|
8
|
+
export const DEFAULT_LOG_LEVEL = "info";
|
|
9
|
+
function redactFields(fields) {
|
|
10
|
+
if (!fields) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const redacted = {};
|
|
14
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
15
|
+
if (/token|secret|password|authorization/i.test(key)) {
|
|
16
|
+
redacted[key] = "[redacted]";
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
redacted[key] = value;
|
|
20
|
+
}
|
|
21
|
+
return redacted;
|
|
22
|
+
}
|
|
23
|
+
export function createJsonConsoleLogger(minLevel = DEFAULT_LOG_LEVEL) {
|
|
24
|
+
const minPriority = LOG_LEVEL_PRIORITY[minLevel];
|
|
25
|
+
const write = (level, event, fields) => {
|
|
26
|
+
if (LOG_LEVEL_PRIORITY[level] < minPriority) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const payload = {
|
|
30
|
+
ts: new Date().toISOString(),
|
|
31
|
+
level,
|
|
32
|
+
event,
|
|
33
|
+
...redactFields(fields) ?? {}
|
|
34
|
+
};
|
|
35
|
+
const line = JSON.stringify(payload);
|
|
36
|
+
if (level === "error") {
|
|
37
|
+
console.error(line);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (level === "warn") {
|
|
41
|
+
console.warn(line);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
console.log(line);
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
trace: (event, fields) => write("trace", event, fields),
|
|
48
|
+
debug: (event, fields) => write("debug", event, fields),
|
|
49
|
+
info: (event, fields) => write("info", event, fields),
|
|
50
|
+
warn: (event, fields) => write("warn", event, fields),
|
|
51
|
+
error: (event, fields) => write("error", event, fields)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function fromOpenCodeLogLevel(value) {
|
|
55
|
+
if (typeof value !== "string") {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
switch (value.toUpperCase()) {
|
|
59
|
+
case "DEBUG":
|
|
60
|
+
// Host DEBUG unlocks our most-verbose internal tier (trace).
|
|
61
|
+
return "trace";
|
|
62
|
+
case "INFO": return "info";
|
|
63
|
+
case "WARN": return "warn";
|
|
64
|
+
case "ERROR": return "error";
|
|
65
|
+
default: return undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=logging.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAIA,OAAO,MAAM,qBAA+C;CAC1D,OAAO;CACP,OAAO;CACP,MAAM;CACN,MAAM;CACN,OAAO;AACT;AAEA,OAAO,MAAM,oBAA8B;AAc3C,SAAS,aAAa,QAA2C;CAC/D,IAAI,CAAC,QAAQ;EACX,OAAO;CACT;CACA,MAAM,WAAsB,CAAC;CAC7B,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,GAAG;EACjD,IAAI,uCAAuC,KAAK,GAAG,GAAG;GACpD,SAAS,OAAO;GAChB;EACF;EACA,SAAS,OAAO;CAClB;CACA,OAAO;AACT;AAEA,OAAO,SAAS,wBAAwB,WAAqB,mBAA2B;CACtF,MAAM,cAAc,mBAAmB;CAEvC,MAAM,SAAS,OAAiB,OAAe,WAA6B;EAC1E,IAAI,mBAAmB,SAAS,aAAa;GAC3C;EACF;EACA,MAAM,UAAU;GACd,IAAI,IAAI,KAAK,CAAC,CAAC,YAAY;GAC3B;GACA;GACA,GAAI,aAAa,MAAM,KAAK,CAAC;EAC/B;EACA,MAAM,OAAO,KAAK,UAAU,OAAO;EACnC,IAAI,UAAU,SAAS;GACrB,QAAQ,MAAM,IAAI;GAClB;EACF;EACA,IAAI,UAAU,QAAQ;GACpB,QAAQ,KAAK,IAAI;GACjB;EACF;EACA,QAAQ,IAAI,IAAI;CAClB;CAEA,OAAO;EACL,QAAQ,OAAO,WAAW,MAAM,SAAS,OAAO,MAAM;EACtD,QAAQ,OAAO,WAAW,MAAM,SAAS,OAAO,MAAM;EACtD,OAAO,OAAO,WAAW,MAAM,QAAQ,OAAO,MAAM;EACpD,OAAO,OAAO,WAAW,MAAM,QAAQ,OAAO,MAAM;EACpD,QAAQ,OAAO,WAAW,MAAM,SAAS,OAAO,MAAM;CACxD;AACF;AAEA,OAAO,SAAS,qBAAqB,OAAsC;CACzE,IAAI,OAAO,UAAU,UAAU;EAC7B,OAAO;CACT;CACA,QAAQ,MAAM,YAAY,GAA1B;EACE,KAAK;;EAEH,OAAO;EACT,KAAK,QACH,OAAO;EACT,KAAK,QACH,OAAO;EACT,KAAK,SACH,OAAO;EACT,SACE,OAAO;CACX;AACF","names":[],"sources":["../src/logging.ts"],"version":3,"file":"logging.js","sourceRoot":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Context } from "@opentelemetry/api";
|
|
2
|
+
import type { Logger } from "./logging.js";
|
|
3
|
+
export interface ProviderConfigLike {
|
|
4
|
+
options?: Record<string, unknown>;
|
|
5
|
+
}
|
|
6
|
+
export interface PropagationConfigInput {
|
|
7
|
+
provider?: Record<string, ProviderConfigLike | undefined>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Wrap every provider's `options.fetch` so outgoing model requests carry W3C
|
|
11
|
+
* trace context, joining an OpenCode session and its gateway-side spans into
|
|
12
|
+
* one trace.
|
|
13
|
+
*
|
|
14
|
+
* This is the same interception seam `@vymalo/opencode-ratelimit` uses — and it
|
|
15
|
+
* composes the same way: the existing fetch is captured at install time and
|
|
16
|
+
* delegated to, so stacking the two plugins in either order works.
|
|
17
|
+
*/
|
|
18
|
+
export declare function installTracePropagation(input: PropagationConfigInput, deps: {
|
|
19
|
+
getContext: () => Context | undefined;
|
|
20
|
+
logger: Logger;
|
|
21
|
+
fetchImpl?: typeof fetch;
|
|
22
|
+
}): number;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { defaultTextMapSetter } from "@opentelemetry/api";
|
|
2
|
+
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
3
|
+
const propagator = new W3CTraceContextPropagator();
|
|
4
|
+
/**
|
|
5
|
+
* Wrap every provider's `options.fetch` so outgoing model requests carry W3C
|
|
6
|
+
* trace context, joining an OpenCode session and its gateway-side spans into
|
|
7
|
+
* one trace.
|
|
8
|
+
*
|
|
9
|
+
* This is the same interception seam `@vymalo/opencode-ratelimit` uses — and it
|
|
10
|
+
* composes the same way: the existing fetch is captured at install time and
|
|
11
|
+
* delegated to, so stacking the two plugins in either order works.
|
|
12
|
+
*/
|
|
13
|
+
export function installTracePropagation(input, deps) {
|
|
14
|
+
const providers = input.provider;
|
|
15
|
+
if (!providers) {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
let wrapped = 0;
|
|
19
|
+
for (const [providerId, providerConfig] of Object.entries(providers)) {
|
|
20
|
+
if (!providerConfig) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const options = providerConfig.options ??= {};
|
|
24
|
+
const delegate = typeof options.fetch === "function" ? options.fetch : deps.fetchImpl ?? globalThis.fetch;
|
|
25
|
+
if (typeof delegate !== "function") {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
options.fetch = async (input_, init) => {
|
|
29
|
+
const context = deps.getContext();
|
|
30
|
+
if (!context) {
|
|
31
|
+
return delegate(input_, init);
|
|
32
|
+
}
|
|
33
|
+
const carrier = {};
|
|
34
|
+
propagator.inject(context, carrier, defaultTextMapSetter);
|
|
35
|
+
if (Object.keys(carrier).length === 0) {
|
|
36
|
+
return delegate(input_, init);
|
|
37
|
+
}
|
|
38
|
+
const headers = new Headers(init?.headers ?? {});
|
|
39
|
+
// Never clobber an upstream traceparent — if something already set one,
|
|
40
|
+
// it knows more about the request than we do.
|
|
41
|
+
for (const [key, value] of Object.entries(carrier)) {
|
|
42
|
+
if (!headers.has(key)) {
|
|
43
|
+
headers.set(key, value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return delegate(input_, {
|
|
47
|
+
...init,
|
|
48
|
+
headers
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
wrapped += 1;
|
|
52
|
+
deps.logger.trace("otel_trace_propagation_installed", { providerId });
|
|
53
|
+
}
|
|
54
|
+
return wrapped;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=propagation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AACA,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAY1C,MAAM,aAAa,IAAI,0BAA0B;;;;;;;;;;AAWjD,OAAO,SAAS,wBACd,OACA,MACQ;CACR,MAAM,YAAY,MAAM;CACxB,IAAI,CAAC,WAAW;EACd,OAAO;CACT;CAEA,IAAI,UAAU;CACd,KAAK,MAAM,CAAC,YAAY,mBAAmB,OAAO,QAAQ,SAAS,GAAG;EACpE,IAAI,CAAC,gBAAgB;GACnB;EACF;EACA,MAAM,UAAW,eAAe,YAAY,CAAC;EAC7C,MAAM,WACJ,OAAO,QAAQ,UAAU,aACpB,QAAQ,QACR,KAAK,aAAa,WAAW;EACpC,IAAI,OAAO,aAAa,YAAY;GAClC;EACF;EAEA,QAAQ,QAAQ,OACd,QACA,SACsB;GACtB,MAAM,UAAU,KAAK,WAAW;GAChC,IAAI,CAAC,SAAS;IACZ,OAAO,SAAS,QAAQ,IAAI;GAC9B;GACA,MAAM,UAAkC,CAAC;GACzC,WAAW,OAAO,SAAS,SAAS,oBAAoB;GACxD,IAAI,OAAO,KAAK,OAAO,CAAC,CAAC,WAAW,GAAG;IACrC,OAAO,SAAS,QAAQ,IAAI;GAC9B;GACA,MAAM,UAAU,IAAI,QAAQ,MAAM,WAAW,CAAC,CAAC;;;GAG/C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,GAAG;IAClD,IAAI,CAAC,QAAQ,IAAI,GAAG,GAAG;KACrB,QAAQ,IAAI,KAAK,KAAK;IACxB;GACF;GACA,OAAO,SAAS,QAAQ;IAAE,GAAG;IAAM;GAAQ,CAAC;EAC9C;EACA,WAAW;EACX,KAAK,OAAO,MAAM,oCAAoC,EAAE,WAAW,CAAC;CACtE;CAEA,OAAO;AACT","names":[],"sources":["../src/propagation.ts"],"version":3,"file":"propagation.js","sourceRoot":""}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Logger as OtelLogger } from "@opentelemetry/api-logs";
|
|
2
|
+
import { type MaybePromise, type Resource } from "@opentelemetry/resources";
|
|
3
|
+
import { type LogRecordExporter } from "@opentelemetry/sdk-logs";
|
|
4
|
+
import { type PushMetricExporter } from "@opentelemetry/sdk-metrics";
|
|
5
|
+
import { type SpanExporter } from "@opentelemetry/sdk-trace";
|
|
6
|
+
import type { Tracer } from "@opentelemetry/api";
|
|
7
|
+
import type { Meter } from "@opentelemetry/api";
|
|
8
|
+
import type { Logger } from "./logging.js";
|
|
9
|
+
import { type TokenSource } from "./token-source.js";
|
|
10
|
+
import type { VcsInfo } from "./vcs.js";
|
|
11
|
+
import type { ResolvedOtelConfig } from "./types.js";
|
|
12
|
+
/**
|
|
13
|
+
* The three provider handles the recorder needs, plus lifecycle. `undefined`
|
|
14
|
+
* for a signal whose exporter is `none` — the recorder no-ops on it rather
|
|
15
|
+
* than branching on config everywhere.
|
|
16
|
+
*/
|
|
17
|
+
export interface TelemetryProviders {
|
|
18
|
+
tracer?: Tracer;
|
|
19
|
+
meter?: Meter;
|
|
20
|
+
otelLogger?: OtelLogger;
|
|
21
|
+
/** Push everything buffered. Called on `session.idle` and on process exit. */
|
|
22
|
+
forceFlush(): Promise<void>;
|
|
23
|
+
/** Flush and tear down. */
|
|
24
|
+
shutdown(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Exporter factories, injectable so tests can substitute in-memory exporters
|
|
28
|
+
* without reaching the network or constructing real OTLP clients.
|
|
29
|
+
*/
|
|
30
|
+
export interface ExporterFactories {
|
|
31
|
+
trace?: (config: ResolvedOtelConfig, tokenSource?: TokenSource) => SpanExporter | undefined;
|
|
32
|
+
metric?: (config: ResolvedOtelConfig, tokenSource?: TokenSource) => PushMetricExporter | undefined;
|
|
33
|
+
log?: (config: ResolvedOtelConfig, tokenSource?: TokenSource) => LogRecordExporter | undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Build the resource every signal is stamped with.
|
|
37
|
+
*
|
|
38
|
+
* Deliberately identifies the **machine and the project**, never the developer:
|
|
39
|
+
* no git author email, no account id. An operator who wants per-person
|
|
40
|
+
* attribution adds it explicitly via `resourceAttributes` /
|
|
41
|
+
* `OTEL_RESOURCE_ATTRIBUTES`, which keeps that choice visible in config.
|
|
42
|
+
*/
|
|
43
|
+
export declare function buildResource(config: ResolvedOtelConfig, context: {
|
|
44
|
+
/**
|
|
45
|
+
* May be a promise: OpenCode reports the host version as an
|
|
46
|
+
* `installation.updated` *event*, after the resource is already built. The
|
|
47
|
+
* OTel resource API awaits promise-valued attributes before the first
|
|
48
|
+
* export — see `deferred.ts` for why the deferral is always bounded.
|
|
49
|
+
*/
|
|
50
|
+
version?: MaybePromise<string | undefined>;
|
|
51
|
+
hostname?: string;
|
|
52
|
+
projectName?: string;
|
|
53
|
+
directory?: string;
|
|
54
|
+
worktree?: string;
|
|
55
|
+
/** May be a promise, for the same reason as `version` (`vcs.branch.updated`). */
|
|
56
|
+
branch?: MaybePromise<string | undefined>;
|
|
57
|
+
/** Repository metadata read off disk — see `vcs.ts`. */
|
|
58
|
+
vcs?: VcsInfo;
|
|
59
|
+
}): Resource;
|
|
60
|
+
/**
|
|
61
|
+
* Construct the enabled providers. Each signal is independent: a failure to
|
|
62
|
+
* build one leaves the others running, because partial telemetry is strictly
|
|
63
|
+
* better than an exception escaping into the host's plugin loader.
|
|
64
|
+
*/
|
|
65
|
+
export declare function createProviders(config: ResolvedOtelConfig, resource: Resource, logger: Logger, factories?: ExporterFactories, injectedTokenSource?: TokenSource): TelemetryProviders;
|
|
66
|
+
export declare function describeError(error: unknown): string;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
|
|
2
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto";
|
|
3
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
4
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
5
|
+
import { BatchLogRecordProcessor, ConsoleLogRecordExporter, LoggerProvider } from "@opentelemetry/sdk-logs";
|
|
6
|
+
import { AggregationTemporality, ConsoleMetricExporter, MeterProvider, PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
|
|
7
|
+
import { BatchSpanProcessor, ConsoleSpanExporter, TracerProvider } from "@opentelemetry/sdk-trace";
|
|
8
|
+
import { signalUrl } from "./config.js";
|
|
9
|
+
import { withFailureLogging } from "./export-logging.js";
|
|
10
|
+
import { createTokenSource } from "./token-source.js";
|
|
11
|
+
const INSTRUMENTATION_SCOPE = "@vymalo/opencode-otel";
|
|
12
|
+
/**
|
|
13
|
+
* Build the exporter's `url` + `headers`. With a credential helper configured,
|
|
14
|
+
* `headers` is an async factory the exporter calls before every export — which
|
|
15
|
+
* is what lets a five-minute OIDC token be refreshed underneath a long-running
|
|
16
|
+
* session instead of going stale at the first expiry.
|
|
17
|
+
*/
|
|
18
|
+
function otlpArgs(config, signal, tokenSource) {
|
|
19
|
+
const url = signalUrl(config, signal);
|
|
20
|
+
if (!url) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
if (!tokenSource) {
|
|
24
|
+
return {
|
|
25
|
+
url,
|
|
26
|
+
headers: config.headers
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
url,
|
|
31
|
+
headers: async () => ({
|
|
32
|
+
...config.headers,
|
|
33
|
+
...await tokenSource.headers()
|
|
34
|
+
})
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const defaultFactories = {
|
|
38
|
+
trace: (config, tokenSource) => {
|
|
39
|
+
if (config.exporters.traces === "console") {
|
|
40
|
+
return new ConsoleSpanExporter();
|
|
41
|
+
}
|
|
42
|
+
const args = otlpArgs(config, "traces", tokenSource);
|
|
43
|
+
return args ? new OTLPTraceExporter(args) : undefined;
|
|
44
|
+
},
|
|
45
|
+
metric: (config, tokenSource) => {
|
|
46
|
+
if (config.exporters.metrics === "console") {
|
|
47
|
+
return new ConsoleMetricExporter();
|
|
48
|
+
}
|
|
49
|
+
const args = otlpArgs(config, "metrics", tokenSource);
|
|
50
|
+
return args ? new OTLPMetricExporter({
|
|
51
|
+
...args,
|
|
52
|
+
temporalityPreference: config.metricTemporality === "cumulative" ? AggregationTemporality.CUMULATIVE : AggregationTemporality.DELTA
|
|
53
|
+
}) : undefined;
|
|
54
|
+
},
|
|
55
|
+
log: (config, tokenSource) => {
|
|
56
|
+
if (config.exporters.logs === "console") {
|
|
57
|
+
return new ConsoleLogRecordExporter();
|
|
58
|
+
}
|
|
59
|
+
const args = otlpArgs(config, "logs", tokenSource);
|
|
60
|
+
return args ? new OTLPLogExporter(args) : undefined;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Build the resource every signal is stamped with.
|
|
65
|
+
*
|
|
66
|
+
* Deliberately identifies the **machine and the project**, never the developer:
|
|
67
|
+
* no git author email, no account id. An operator who wants per-person
|
|
68
|
+
* attribution adds it explicitly via `resourceAttributes` /
|
|
69
|
+
* `OTEL_RESOURCE_ATTRIBUTES`, which keeps that choice visible in config.
|
|
70
|
+
*/
|
|
71
|
+
export function buildResource(config, context) {
|
|
72
|
+
const attributes = {
|
|
73
|
+
"service.name": config.serviceName,
|
|
74
|
+
"telemetry.sdk.language": "nodejs"
|
|
75
|
+
};
|
|
76
|
+
if (context.version) {
|
|
77
|
+
attributes["service.version"] = context.version;
|
|
78
|
+
}
|
|
79
|
+
if (config.environment) {
|
|
80
|
+
attributes["deployment.environment.name"] = config.environment;
|
|
81
|
+
}
|
|
82
|
+
if (context.hostname) {
|
|
83
|
+
attributes["host.name"] = context.hostname;
|
|
84
|
+
}
|
|
85
|
+
if (context.projectName) {
|
|
86
|
+
attributes["opencode.project.name"] = context.projectName;
|
|
87
|
+
}
|
|
88
|
+
if (context.directory) {
|
|
89
|
+
attributes["opencode.directory"] = context.directory;
|
|
90
|
+
}
|
|
91
|
+
if (context.worktree) {
|
|
92
|
+
attributes["opencode.worktree"] = context.worktree;
|
|
93
|
+
}
|
|
94
|
+
if (context.branch) {
|
|
95
|
+
attributes["vcs.ref.head.name"] = context.branch;
|
|
96
|
+
// Deprecated in semconv 1.43 in favour of `vcs.ref.head.name`, but it is
|
|
97
|
+
// what `opencode-otel-plugin` emits and what existing dashboards key on.
|
|
98
|
+
// Kept as an alias so a collector receiving both plugins stays coherent;
|
|
99
|
+
// due for removal once those dashboards move.
|
|
100
|
+
attributes["vcs.repository.ref.name"] = context.branch;
|
|
101
|
+
}
|
|
102
|
+
const vcs = context.vcs;
|
|
103
|
+
if (vcs) {
|
|
104
|
+
if (vcs.url) {
|
|
105
|
+
attributes["vcs.repository.url.full"] = vcs.url;
|
|
106
|
+
}
|
|
107
|
+
if (vcs.name) {
|
|
108
|
+
attributes["vcs.repository.name"] = vcs.name;
|
|
109
|
+
}
|
|
110
|
+
if (vcs.owner) {
|
|
111
|
+
attributes["vcs.owner.name"] = vcs.owner;
|
|
112
|
+
}
|
|
113
|
+
if (vcs.provider) {
|
|
114
|
+
attributes["vcs.provider.name"] = vcs.provider;
|
|
115
|
+
}
|
|
116
|
+
if (vcs.revision) {
|
|
117
|
+
attributes["vcs.ref.head.revision"] = vcs.revision;
|
|
118
|
+
}
|
|
119
|
+
if (vcs.refType) {
|
|
120
|
+
attributes["vcs.ref.head.type"] = vcs.refType;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Operator-supplied attributes win — they are the escape hatch, and silently
|
|
124
|
+
// ignoring them would make the escape hatch useless.
|
|
125
|
+
return resourceFromAttributes({
|
|
126
|
+
...attributes,
|
|
127
|
+
...config.resourceAttributes
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Construct the enabled providers. Each signal is independent: a failure to
|
|
132
|
+
* build one leaves the others running, because partial telemetry is strictly
|
|
133
|
+
* better than an exception escaping into the host's plugin loader.
|
|
134
|
+
*/
|
|
135
|
+
export function createProviders(config, resource, logger, factories = {}, injectedTokenSource) {
|
|
136
|
+
const make = {
|
|
137
|
+
...defaultFactories,
|
|
138
|
+
...factories
|
|
139
|
+
};
|
|
140
|
+
const flushers = [];
|
|
141
|
+
const shutdowns = [];
|
|
142
|
+
// An injected source (e.g. one backed by the shared `TokenRuntime` in the
|
|
143
|
+
// `@vymalo/opencode-lightbridge` umbrella) wins over the config-driven
|
|
144
|
+
// credential helper. `createTokenSource` from `config.tokenCommand` remains
|
|
145
|
+
// the standalone plugin's default when nothing is injected.
|
|
146
|
+
const tokenSource = injectedTokenSource ?? (config.tokenCommand.length > 0 ? createTokenSource({
|
|
147
|
+
command: config.tokenCommand,
|
|
148
|
+
header: config.tokenHeader,
|
|
149
|
+
prefix: config.tokenPrefix,
|
|
150
|
+
refreshMs: config.tokenRefreshMs,
|
|
151
|
+
timeoutMs: config.tokenTimeoutMs,
|
|
152
|
+
logger
|
|
153
|
+
}) : undefined);
|
|
154
|
+
// A rejected export is the symptom of a dead credential, so drop the cached
|
|
155
|
+
// token and let the next export re-run the helper rather than retrying with
|
|
156
|
+
// something the collector has already refused.
|
|
157
|
+
const onFailure = tokenSource ? () => tokenSource.invalidate() : undefined;
|
|
158
|
+
const observe = (exporter, signal) => withFailureLogging(exporter, signal, logger, { onFailure });
|
|
159
|
+
let tracer;
|
|
160
|
+
let meter;
|
|
161
|
+
let otelLogger;
|
|
162
|
+
if (config.exporters.traces !== "none") {
|
|
163
|
+
try {
|
|
164
|
+
const built = make.trace(config, tokenSource);
|
|
165
|
+
if (built) {
|
|
166
|
+
const exporter = observe(built, "traces");
|
|
167
|
+
const provider = new TracerProvider({
|
|
168
|
+
resource,
|
|
169
|
+
spanProcessors: [new BatchSpanProcessor({
|
|
170
|
+
exporter,
|
|
171
|
+
scheduledDelayMillis: config.traceExportIntervalMs
|
|
172
|
+
})]
|
|
173
|
+
});
|
|
174
|
+
tracer = provider.getTracer(INSTRUMENTATION_SCOPE);
|
|
175
|
+
flushers.push(() => provider.forceFlush());
|
|
176
|
+
shutdowns.push(() => provider.shutdown());
|
|
177
|
+
}
|
|
178
|
+
} catch (error) {
|
|
179
|
+
logger.warn("otel_traces_init_failed", { error: describeError(error) });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (config.exporters.metrics !== "none") {
|
|
183
|
+
try {
|
|
184
|
+
const built = make.metric(config, tokenSource);
|
|
185
|
+
if (built) {
|
|
186
|
+
const exporter = observe(built, "metrics");
|
|
187
|
+
const provider = new MeterProvider({
|
|
188
|
+
resource,
|
|
189
|
+
readers: [new PeriodicExportingMetricReader({
|
|
190
|
+
exporter,
|
|
191
|
+
exportIntervalMillis: config.metricExportIntervalMs
|
|
192
|
+
})]
|
|
193
|
+
});
|
|
194
|
+
meter = provider.getMeter(INSTRUMENTATION_SCOPE);
|
|
195
|
+
flushers.push(() => provider.forceFlush());
|
|
196
|
+
shutdowns.push(() => provider.shutdown());
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
logger.warn("otel_metrics_init_failed", { error: describeError(error) });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (config.exporters.logs !== "none") {
|
|
203
|
+
try {
|
|
204
|
+
const built = make.log(config, tokenSource);
|
|
205
|
+
if (built) {
|
|
206
|
+
const exporter = observe(built, "logs");
|
|
207
|
+
const provider = new LoggerProvider({
|
|
208
|
+
resource,
|
|
209
|
+
processors: [new BatchLogRecordProcessor({
|
|
210
|
+
exporter,
|
|
211
|
+
scheduledDelayMillis: config.logExportIntervalMs
|
|
212
|
+
})]
|
|
213
|
+
});
|
|
214
|
+
otelLogger = provider.getLogger(INSTRUMENTATION_SCOPE);
|
|
215
|
+
flushers.push(() => provider.forceFlush());
|
|
216
|
+
shutdowns.push(() => provider.shutdown());
|
|
217
|
+
}
|
|
218
|
+
} catch (error) {
|
|
219
|
+
logger.warn("otel_logs_init_failed", { error: describeError(error) });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const runAll = async (tasks) => {
|
|
223
|
+
// `allSettled`, not `all`: one unreachable collector must not stop the
|
|
224
|
+
// other signals from draining.
|
|
225
|
+
await Promise.allSettled(tasks.map((task) => task()));
|
|
226
|
+
};
|
|
227
|
+
return {
|
|
228
|
+
tracer,
|
|
229
|
+
meter,
|
|
230
|
+
otelLogger,
|
|
231
|
+
forceFlush: () => runAll(flushers),
|
|
232
|
+
shutdown: () => runAll(shutdowns)
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
export function describeError(error) {
|
|
236
|
+
if (error instanceof Error) {
|
|
237
|
+
return error.message;
|
|
238
|
+
}
|
|
239
|
+
return String(error);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
//# sourceMappingURL=providers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AACA,SAAS,uBAAuB;AAChC,SAAS,0BAA0B;AACnC,SAAS,yBAAyB;AAClC,SAIE,8BACK;AACP,SACE,yBACA,0BAEA,sBACK;AACP,SACE,wBACA,uBACA,eACA,qCAEK;AACP,SACE,oBACA,qBAEA,sBACK;AAIP,SAAS,iBAAiB;AAC1B,SAA4B,0BAA0B;AAEtD,SAAS,yBAA2C;AAIpD,MAAM,wBAAwB;;;;;;;AAoC9B,SAAS,SAAS,QAA4B,QAAoB,aAA2B;CAC3F,MAAM,MAAM,UAAU,QAAQ,MAAM;CACpC,IAAI,CAAC,KAAK;EACR,OAAO;CACT;CACA,IAAI,CAAC,aAAa;EAChB,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