Skip to content

Commit 685eaf4

Browse files
committed
changed package.json for installation. openai instrumentation changes (span is exported now, TODO: Trace and project type)
1 parent a267c88 commit 685eaf4

10 files changed

Lines changed: 128 additions & 25 deletions

File tree

‎typescript/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"typescript": "^5.0.0"
2121
},
2222
"dependencies": {
23+
"@traceai/fi-semantic-conventions": "workspace:*",
24+
"@traceai/openai": "workspace:*",
25+
"@traceai/fi-core": "workspace:*",
2326
"uuid": "^11.1.0"
2427
}
2528
}

‎typescript/packages/fi-core/index.ts‎

Whitespace-only changes.

‎typescript/packages/fi-core/src/trace/trace-config/FISpan.ts‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Exception,
88
AttributeValue,
99
Link,
10+
diag,
1011
} from "@opentelemetry/api";
1112
import { TraceConfig } from "./types";
1213
import { mask } from "./maskingRules";
@@ -17,13 +18,23 @@ import {
1718
export class FISpan implements Span {
1819
private readonly span: Span;
1920
private readonly config: TraceConfig;
21+
private isEnded: boolean = false;
2022

2123
constructor({ span, config }: { span: Span; config: TraceConfig }) {
2224
this.span = span;
2325
this.config = config;
26+
const wrappedSpanContext = span.spanContext();
27+
const isNoOp = wrappedSpanContext.traceFlags === 0 && wrappedSpanContext.spanId === '0000000000000000';
28+
diag.debug(
29+
`FISpan CONSTRUCTOR: Wrapping span. ID: ${wrappedSpanContext.spanId}, TraceID: ${wrappedSpanContext.traceId}, TraceFlags: ${wrappedSpanContext.traceFlags}, Is NoOp: ${isNoOp}`
30+
);
2431
}
2532

2633
setAttribute(key: string, value: AttributeValue): this {
34+
if (this.isEnded) {
35+
diag.warn(`FISpan.setAttribute called on ended span ID: ${this.span.spanContext().spanId} for key: ${key}`);
36+
return this;
37+
}
2738
const maskedValue = mask({ config: this.config, key, value });
2839
if (maskedValue != null) {
2940
this.span.setAttribute(key, maskedValue);
@@ -32,10 +43,17 @@ import {
3243
}
3344

3445
setAttributes(attributes: Attributes): this {
46+
if (this.isEnded) {
47+
diag.warn(`FISpan.setAttributes called on ended span ID: ${this.span.spanContext().spanId}`);
48+
return this;
49+
}
3550
const maskedAttributes = Object.entries(attributes).reduce(
36-
(maskedAttributes, [key, value]) => {
37-
maskedAttributes[key] = mask({ config: this.config, key, value });
38-
return maskedAttributes;
51+
(acc, [key, value]) => {
52+
const maskedValue = mask({ config: this.config, key, value });
53+
if (maskedValue != null) {
54+
acc[key] = maskedValue;
55+
}
56+
return acc;
3957
},
4058
{} as Attributes,
4159
);
@@ -48,29 +66,41 @@ import {
4866
attributesOrStartTime?: Attributes | TimeInput,
4967
startTime?: TimeInput,
5068
): this {
69+
if (this.isEnded) return this;
5170
this.span.addEvent(name, attributesOrStartTime, startTime);
5271
return this;
5372
}
5473

5574
setStatus(status: SpanStatus): this {
75+
if (this.isEnded) return this;
5676
this.span.setStatus(status);
5777
return this;
5878
}
5979

6080
updateName(name: string): this {
81+
if (this.isEnded) return this;
6182
this.span.updateName(name);
6283
return this;
6384
}
6485

6586
end(endTime?: TimeInput): void {
87+
if (this.isEnded) {
88+
diag.warn(`FISpan.end called on already ended span ID: ${this.span.spanContext().spanId}`);
89+
return;
90+
}
6691
this.span.end(endTime);
92+
this.isEnded = true;
93+
diag.debug(
94+
`FISpan.end CALLED for span ID: ${this.span.spanContext().spanId}`
95+
);
6796
}
6897

6998
isRecording(): boolean {
7099
return this.span.isRecording();
71100
}
72101

73102
recordException(exception: Exception, time?: TimeInput): void {
103+
if (this.isEnded) return;
74104
this.span.recordException(exception, time);
75105
}
76106

@@ -79,11 +109,13 @@ import {
79109
}
80110

81111
addLink(link: Link): this {
112+
if (this.isEnded) return this;
82113
this.span.addLink(link);
83114
return this;
84115
}
85116

86117
addLinks(links: Link[]): this {
118+
if (this.isEnded) return this;
87119
this.span.addLinks(links);
88120
return this;
89121
}

‎typescript/packages/fi-core/src/trace/trace-config/FITracer.ts‎

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
Span,
66
SpanOptions,
77
Tracer,
8+
diag,
9+
trace,
810
} from "@opentelemetry/api";
911
import {
1012
FIActiveSpanCallback,
@@ -14,6 +16,10 @@ import {
1416
import { FISpan } from "./FISpan";
1517
import { generateTraceConfig } from "./traceConfig";
1618
import { getAttributesFromContext } from "../contextAttributes";
19+
// Removed unused imports that were causing linter errors
20+
// import { INPUT_ATTRIBUTES, ... } from "./constants";
21+
// import { applyAttributeRulesToTarget, ... } from "./maskingRules";
22+
// import { MessageDirection } from "./types"; // REMOVING THIS LINE
1723

1824
/**
1925
* Formats the params for the startActiveSpan method
@@ -67,6 +73,10 @@ import {
6773
}) {
6874
this.tracer = tracer;
6975
this.config = generateTraceConfig(traceConfig);
76+
// ADDED LOG
77+
diag.debug(
78+
`FITracer CONSTRUCTOR: Received tracer type: ${tracer?.constructor?.name}. TraceConfig provided: ${!!traceConfig}`
79+
);
7080
}
7181
startActiveSpan<F extends (span: FISpan) => unknown>(
7282
name: string,
@@ -113,19 +123,35 @@ import {
113123
}
114124

115125
startSpan(name: string, options?: SpanOptions, context?: Context): FISpan {
116-
const attributes = options?.attributes;
117-
const ctx = context ?? apiContext.active();
118-
const contextAttributes = getAttributesFromContext(ctx);
119-
const mergedAttributes = { ...contextAttributes, ...attributes };
120-
const span = new FISpan({
121-
span: this.tracer.startSpan(
122-
name,
123-
{ ...options, attributes: undefined },
124-
ctx,
125-
),
126-
config: this.config,
127-
});
128-
span.setAttributes(mergedAttributes);
129-
return span;
126+
// ADDED LOG
127+
diag.debug(
128+
`FITracer.startSpan CALLED for name: "${name}". Internal this.tracer type: ${this.tracer?.constructor?.name}`
129+
);
130+
131+
if (!this.tracer || this.tracer.constructor.name === "NoopTracer") {
132+
diag.warn(
133+
`FITracer.startSpan: Internal tracer is NoopTracer or null. Returning NoopSpan for "${name}".`
134+
);
135+
// Return an actual NoopSpan from the API to avoid downstream errors if this path is taken.
136+
const noopOtelSpan = trace.getTracer("fi-noop-tracer-internal").startSpan(name, options, context);
137+
return new FISpan({ span: noopOtelSpan, config: this.config });
138+
}
139+
140+
const { attributes: originalAttributes, ...otherOptions } = options ?? {};
141+
142+
const span = this.tracer.startSpan(
143+
name,
144+
{ ...otherOptions, attributes: originalAttributes },
145+
context,
146+
);
147+
148+
// ADDED LOGS - CRITICAL
149+
const spanContext = span.spanContext();
150+
const isNoOp = spanContext.traceFlags === 0 && spanContext.spanId === '0000000000000000';
151+
diag.debug(
152+
`FITracer.startSpan: OTel tracer (${this.tracer?.constructor?.name}) created span. ID: ${spanContext.spanId}, TraceID: ${spanContext.traceId}, TraceFlags: ${spanContext.traceFlags}, Is NoOp: ${isNoOp}`
153+
);
154+
155+
return new FISpan({ span, config: this.config });
130156
}
131157
}

‎typescript/packages/fi-core/tsconfig.tsbuildinfo‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎typescript/packages/fi-semantic-conventions/tsconfig.tsbuildinfo‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎typescript/packages/traceai_openai/examples/basic-otel-test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ async function main() {
3232

3333
// 1. Register FI Core TracerProvider (sets up exporter)
3434
const tracerProvider = register({
35-
projectName: "openai-ts-chat-test",
35+
projectName: "ts-observability-suite-v1",
3636
projectType: ProjectType.OBSERVE,
37-
projectVersionName: "1.0.1",
37+
// projectVersionName: "1.0.1",
3838
verbose: true,
3939
...(fiBaseUrl && { endpoint: fiBaseUrl }),
4040
batch: false, // Send spans immediately for testing

‎typescript/packages/traceai_openai/src/instrumentation.ts‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ function getExecContext(span: Span) {
9595
* @param traceConfig The FI trace configuration. Can be used to mask or redact sensitive information on spans. @see {@link TraceConfigOptions}
9696
*/
9797
export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
98-
private fiTracer: FITracer;
98+
private fiTracer!: FITracer;
99+
private _traceConfig?: TraceConfigOptions;
100+
99101
constructor({
100102
instrumentationConfig,
101103
traceConfig,
@@ -116,7 +118,12 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
116118
VERSION,
117119
Object.assign({}, instrumentationConfig),
118120
);
119-
this.fiTracer = new FITracer({ tracer: this.tracer, traceConfig });
121+
this._traceConfig = traceConfig;
122+
}
123+
124+
public override enable(): void {
125+
super.enable();
126+
this.fiTracer = new FITracer({ tracer: this.tracer, traceConfig: this._traceConfig });
120127
}
121128

122129
protected init(): InstrumentationModuleDefinition<typeof openai> {
@@ -152,6 +159,25 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
152159
// eslint-disable-next-line @typescript-eslint/no-this-alias
153160
const instrumentation: OpenAIInstrumentation = this;
154161

162+
// ADDED: Ensure fiTracer uses the most up-to-date tracer from InstrumentationBase
163+
// The `tracer` property in `InstrumentationBase` (accessed via `instrumentation.tracer`)
164+
// is initialized as a ProxyTracer and then updated to the actual Tracer.
165+
// FITracer might have been initialized with the ProxyTracer in `enable()`.
166+
// Here, we ensure it uses the (potentially updated) Tracer.
167+
// Accessing FITracer's internal tracer `tracer` for comparison.
168+
// This assumes FITracer has an internal property named `tracer`.
169+
if (!(instrumentation.fiTracer as any).tracer || (instrumentation.fiTracer as any).tracer !== instrumentation.tracer) {
170+
diag.debug(
171+
`OpenAIInstrumentation.patch: fiTracer's internal tracer (${(instrumentation.fiTracer as any).tracer?.constructor?.name}) ` +
172+
`differs from current base tracer (${instrumentation.tracer?.constructor?.name}) or is not set. Re-initializing fiTracer.`
173+
);
174+
instrumentation.fiTracer = new FITracer({ tracer: instrumentation.tracer, traceConfig: instrumentation._traceConfig });
175+
} else {
176+
diag.debug(
177+
`OpenAIInstrumentation.patch: fiTracer already using current base tracer (${instrumentation.tracer?.constructor?.name}). No re-initialization needed.`
178+
);
179+
}
180+
155181
// Patch create chat completions
156182
type ChatCompletionCreateType =
157183
typeof module.OpenAI.Chat.Completions.prototype.create;
@@ -168,8 +194,15 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
168194
const body = args[0];
169195
const { messages: _messages, ...invocationParameters } = body;
170196

171-
// --- ADD LOG ---
172197
diag.debug("@traceai/openai: ChatCompletion patch CALLED. Starting span...");
198+
199+
// --- ADD LOGS FOR TRACER AND CONTEXT ---
200+
const activeContextForSuppressionCheck = context.active();
201+
const isSuppressed = isTracingSuppressed(activeContextForSuppressionCheck);
202+
diag.debug(`@traceai/openai: Is tracing suppressed? ${isSuppressed}`);
203+
diag.debug(`@traceai/openai: this.tracer type: ${instrumentation.tracer?.constructor?.name}`); // Accessing the base tracer
204+
diag.debug(`@traceai/openai: this.fiTracer type: ${instrumentation.fiTracer?.constructor?.name}`);
205+
// --- END ADDED LOGS ---
173206

174207
const span = instrumentation.fiTracer.startSpan(
175208
`OpenAI Chat Completions`,

‎typescript/packages/traceai_openai/tsconfig.tsbuildinfo‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎typescript/pnpm-lock.yaml‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)