@overmind-lab/trace-sdk 0.0.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.
@@ -0,0 +1,34 @@
1
+ import type { InstrumentationConfig } from "@opentelemetry/instrumentation";
2
+
3
+ export type ImageUploadCallback = (
4
+ traceId: string,
5
+ spanId: string,
6
+ filename: string,
7
+ base64Data: string
8
+ ) => Promise<string>;
9
+
10
+ export interface OpenAIInstrumentationConfig extends InstrumentationConfig {
11
+ /**
12
+ * Whether to log prompts, completions and embeddings on traces.
13
+ * @default true
14
+ */
15
+ traceContent?: boolean;
16
+
17
+ /**
18
+ * Whether to enrich token information if missing from the trace.
19
+ * @default false
20
+ */
21
+ enrichTokens?: boolean;
22
+
23
+ /**
24
+ * A custom logger to log any exceptions that happen during span creation.
25
+ */
26
+ exceptionLogger?: (e: Error) => void;
27
+
28
+ /**
29
+ * Callback function for uploading base64-encoded images.
30
+ * Used for image generation and vision capabilities.
31
+ * @default undefined
32
+ */
33
+ uploadBase64Image?: ImageUploadCallback;
34
+ }
@@ -0,0 +1,95 @@
1
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
2
+ import type { Instrumentation } from "@opentelemetry/instrumentation";
3
+ import { resourceFromAttributes } from "@opentelemetry/resources";
4
+ import { NodeSDK } from "@opentelemetry/sdk-node";
5
+ import {
6
+ BatchSpanProcessor,
7
+ SimpleSpanProcessor,
8
+ type SpanProcessor,
9
+ } from "@opentelemetry/sdk-trace-base";
10
+ import { ConsoleSpanExporter } from "@opentelemetry/sdk-trace-node";
11
+ import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
12
+
13
+ import OpenAI from "openai";
14
+ import { OpenAIInstrumentation } from "./instrumentation-openai";
15
+
16
+ import { name, version } from "../package.json";
17
+
18
+ type OvermindClientConfig = {
19
+ apiKey: string;
20
+ baseUrl?: string;
21
+ appName?: string;
22
+ };
23
+
24
+ export class OvermindClient {
25
+ public readonly appName: string;
26
+ private version: string = version;
27
+ private baseUrl: string;
28
+ private apiKey: string;
29
+ public experimentSlug?: string;
30
+
31
+ constructor(config: OvermindClientConfig) {
32
+ this.baseUrl =
33
+ config.baseUrl || process.env.OVERMIND_TRACES_URL || "https://api.overmindlab.ai";
34
+
35
+ this.apiKey = config.apiKey || process.env.OVERMIND_API_KEY!;
36
+
37
+ this.appName = config.appName || "overmind-js";
38
+ if (!this.apiKey) {
39
+ throw new Error("OVERMIND_API_KEY is not set");
40
+ }
41
+ if (!this.baseUrl) {
42
+ throw new Error("OVERMIND_TRACES_URL is not set");
43
+ }
44
+ }
45
+
46
+ initTracing({
47
+ instrumentations = [],
48
+ spanProcessors = [],
49
+ ...config
50
+ }: {
51
+ instrumentations?: Instrumentation[];
52
+ spanProcessors?: SpanProcessor[];
53
+ enableBatching: boolean;
54
+ enabledProviders: Partial<Record<"openai" | "anthropic", boolean>>;
55
+ }) {
56
+ const traceExporter = !this.baseUrl
57
+ ? new OTLPTraceExporter({
58
+ url: `${this.baseUrl}/api/v1/traces/create`,
59
+ headers: {
60
+ "X-API-TOKEN": `Bearer ${this.apiKey}`,
61
+ },
62
+ })
63
+ : new ConsoleSpanExporter();
64
+
65
+ const spanProcessor = config.enableBatching
66
+ ? new BatchSpanProcessor(traceExporter)
67
+ : new SimpleSpanProcessor(traceExporter);
68
+
69
+ spanProcessors.push(spanProcessor);
70
+
71
+ const resource = resourceFromAttributes({
72
+ [ATTR_SERVICE_NAME]: this.appName,
73
+ [ATTR_SERVICE_VERSION]: this.version,
74
+ "deployment.environment": process.env.DEPLOYMENT_ENVIRONMENT || "development",
75
+ "overmind.sdk.name": name,
76
+ "overmind.sdk.version": this.version,
77
+ });
78
+
79
+ if (config.enabledProviders.openai) {
80
+ const openaiInstrumentation = new OpenAIInstrumentation({
81
+ enabled: true,
82
+ });
83
+ openaiInstrumentation.manuallyInstrument(OpenAI);
84
+ instrumentations.push(openaiInstrumentation);
85
+ }
86
+
87
+ const _sdk = new NodeSDK({
88
+ resource,
89
+ spanProcessors,
90
+ instrumentations: [...instrumentations],
91
+ });
92
+
93
+ _sdk.start();
94
+ }
95
+ }