Skip to content

Commit

Permalink
Fix baggage propagation on Linux, add severityNumber (#409)
Browse files Browse the repository at this point in the history
Empirically on Linux (but not on macOS) context is not active when the
log record processor runs. This reworks the custom process so that
context can be accessed explicitly.

Additionally, update the OpenTelemetry logger to set `severityNumber`.
  • Loading branch information
petersalas committed Oct 17, 2023
1 parent 7814487 commit 90370c4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/ai-jsx/package.json
Expand Up @@ -4,7 +4,7 @@
"repository": "fixie-ai/ai-jsx",
"bugs": "https://github.com/fixie-ai/ai-jsx/issues",
"homepage": "https://ai-jsx.com",
"version": "0.22.0",
"version": "0.22.1",
"volta": {
"extends": "../../package.json"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/ai-jsx/src/core/log.ts
Expand Up @@ -129,6 +129,15 @@ export class OpenTelemetryLogger extends LogImplementation {
): void {
this.logger.emit({
severityText: level.toUpperCase(),
severityNumber: {
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
trace: 1,
debug: 5,
info: 9,
warn: 13,
error: 17,
fatal: 21,
}[level],
body: typeof metadataOrMessage === 'object' ? message ?? '' : metadataOrMessage,
attributes: {
element: `<${element.tag.name}>`,
Expand Down
13 changes: 12 additions & 1 deletion packages/docs/docs/changelog.md
@@ -1,6 +1,17 @@
# Changelog

## 0.21.2
# 0.22.1

- In the OpenTelemetry logger, ensure that SeverityNumber is set.

## [0.22.0](https://github.com/fixie-ai/ai-jsx/commit/b5337176cd0d50c1691c2ae4bcc02e71a37b407c)

- In the `Sidekick` component:
- Put the user system messages before the built-in system messages.
- Make the MDX formatting logic is conditional on using MDX
- Accept `children` as the conversation to act on (defaults to `<ConversationHistory>`)

## [0.21.2](https://github.com/fixie-ai/ai-jsx/commit/f59cd9990d9f64f94dc961dd0308f0c0eca44e00)

- Fix a bug in `LimitToValidMdx` where a whitespace character was initially yieled.

Expand Down
2 changes: 1 addition & 1 deletion packages/fixie-sdk/package.json
@@ -1,6 +1,6 @@
{
"name": "@fixieai/sdk",
"version": "1.5.0",
"version": "1.5.1",
"license": "MIT",
"repository": "fixie-ai/ai-jsx",
"bugs": "https://github.com/fixie-ai/ai-jsx/issues",
Expand Down
34 changes: 23 additions & 11 deletions packages/fixie-sdk/src/instrument.ts
Expand Up @@ -4,7 +4,7 @@ import fastify_instrumentation from '@opentelemetry/instrumentation-fastify';
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc';
import { BatchLogRecordProcessor, LogRecord } from '@opentelemetry/sdk-logs';
import { BatchLogRecordProcessor, LogRecord, LogRecordProcessor } from '@opentelemetry/sdk-logs';
import { Context, propagation } from '@opentelemetry/api';
import { BatchSpanProcessor, Span } from '@opentelemetry/sdk-trace-base';

Expand All @@ -31,23 +31,35 @@ class CustomSpanProcessor extends BatchSpanProcessor {
}
}

class CustomLogProcessor extends BatchLogRecordProcessor {
onEmit(logRecord: LogRecord): void {
class CustomLogProcessor implements LogRecordProcessor {
// N.B. Wraps (rather than extends) BatchLogRecordProcessor so that we can access
// the second argument of `onEmit` to access baggage.
constructor(private readonly delegated: LogRecordProcessor) {}

forceFlush(): Promise<void> {
return this.delegated.forceFlush();
}
shutdown(): Promise<void> {
return this.delegated.shutdown();
}
onEmit(logRecord: LogRecord, context?: Context | undefined): void {
// Attach any Fixie baggage to the log record.
const baggage = propagation.getActiveBaggage();
baggage?.getAllEntries().forEach(([key, value]) => {
if (key.startsWith('ai.fixie.')) {
logRecord.attributes[key] = value.value;
}
});
if (context) {
const baggage = propagation.getBaggage(context);
baggage?.getAllEntries().forEach(([key, value]) => {
if (key.startsWith('ai.fixie.')) {
logRecord.attributes[key] = value.value;
}
});
}

super.onEmit(logRecord);
this.delegated.onEmit(logRecord);
}
}

const sdk = new NodeSDK({
spanProcessor: new CustomSpanProcessor(new OTLPTraceExporter()),
logRecordProcessor: new CustomLogProcessor(new OTLPLogExporter()),
logRecordProcessor: new CustomLogProcessor(new BatchLogRecordProcessor(new OTLPLogExporter())),
instrumentations: [
new HttpInstrumentation(),
new fastify_instrumentation.FastifyInstrumentation(),
Expand Down

4 comments on commit 90370c4

@vercel
Copy link

@vercel vercel bot commented on 90370c4 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-voice – ./packages/voice

ai-jsx-voice-git-main-fixie-ai.vercel.app
voice.fixie.ai
ai-jsx-voice.vercel.app
ai-jsx-voice-fixie-ai.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 90370c4 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-docs – ./packages/docs

ai-jsx-docs.vercel.app
docs.ai-jsx.com
ai-jsx-docs-fixie-ai.vercel.app
ai-jsx-docs-git-main-fixie-ai.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 90370c4 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-tutorial-nextjs – ./packages/tutorial-nextjs

ai-jsx-tutorial-nextjs-git-main-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 90370c4 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-nextjs-demo – ./packages/nextjs-demo

ai-jsx-nextjs-demo-git-main-fixie-ai.vercel.app
ai-jsx-nextjs-demo.vercel.app
ai-jsx-nextjs-demo-fixie-ai.vercel.app

Please sign in to comment.