diff --git a/.changeset/cyan-bats-jump.md b/.changeset/cyan-bats-jump.md new file mode 100644 index 0000000..4a5cd7e --- /dev/null +++ b/.changeset/cyan-bats-jump.md @@ -0,0 +1,5 @@ +--- +"@pydantic/logfire-api": patch +--- + +Do not format span_name diff --git a/packages/logfire-api/src/constants.ts b/packages/logfire-api/src/constants.ts index abbcfe6..49aedc7 100644 --- a/packages/logfire-api/src/constants.ts +++ b/packages/logfire-api/src/constants.ts @@ -7,6 +7,7 @@ export const ATTRIBUTES_LEVEL_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.level_num` export const ATTRIBUTES_SPAN_TYPE_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.span_type` export const ATTRIBUTES_TAGS_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.tags` export const ATTRIBUTES_MESSAGE_TEMPLATE_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.msg_template` +export const ATTRIBUTES_MESSAGE_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.msg` /** Key for storing scrubbed attributes information */ export const ATTRIBUTES_SCRUBBED_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.scrubbed` diff --git a/packages/logfire-api/src/formatter.ts b/packages/logfire-api/src/formatter.ts index 2b99459..eb6c9c5 100644 --- a/packages/logfire-api/src/formatter.ts +++ b/packages/logfire-api/src/formatter.ts @@ -218,13 +218,6 @@ class ChunksFormatter { // Create singleton instance export const chunksFormatter = new ChunksFormatter() -/** - * Format a string using a Python-like template syntax - */ -export function logfireFormat(formatString: string, record: Record, scrubber: BaseScrubber): string { - return logfireFormatWithExtras(formatString, record, scrubber)[0] -} - /** * Format a string with additional information about attributes and templates */ @@ -232,11 +225,20 @@ export function logfireFormatWithExtras( formatString: string, record: Record, scrubber: BaseScrubber -): [string, Record, string] { +): { + extraAttributes: Record + formattedMessage: string + newTemplate: string +} { try { - const [chunks, extraAttrs, newTemplate] = chunksFormatter.chunks(formatString, record, scrubber) + const [chunks, extraAttributes, newTemplate] = chunksFormatter.chunks(formatString, record, scrubber) - return [chunks.map((chunk) => chunk.value).join(''), extraAttrs, newTemplate] + const formattedMessage = chunks.map((chunk) => chunk.value).join('') + return { + extraAttributes, + formattedMessage, + newTemplate, + } } catch (err) { if (err instanceof KnownFormattingError) { console.warn(`Formatting error: ${err.message}`) @@ -245,7 +247,11 @@ export function logfireFormatWithExtras( } // Formatting failed, use the original format string as the message - return [formatString, {}, formatString] + return { + extraAttributes: {}, + formattedMessage: formatString, + newTemplate: formatString, + } } } diff --git a/packages/logfire-api/src/index.ts b/packages/logfire-api/src/index.ts index 7fd2a00..2328edf 100644 --- a/packages/logfire-api/src/index.ts +++ b/packages/logfire-api/src/index.ts @@ -2,7 +2,13 @@ import { Span, SpanStatusCode, context as TheContextAPI, trace as TheTraceAPI } from '@opentelemetry/api' import { ATTR_EXCEPTION_MESSAGE, ATTR_EXCEPTION_STACKTRACE } from '@opentelemetry/semantic-conventions' -import { ATTRIBUTES_LEVEL_KEY, ATTRIBUTES_MESSAGE_TEMPLATE_KEY, ATTRIBUTES_SPAN_TYPE_KEY, ATTRIBUTES_TAGS_KEY } from './constants' +import { + ATTRIBUTES_LEVEL_KEY, + ATTRIBUTES_MESSAGE_KEY, + ATTRIBUTES_MESSAGE_TEMPLATE_KEY, + ATTRIBUTES_SPAN_TYPE_KEY, + ATTRIBUTES_TAGS_KEY, +} from './constants' import { logfireFormatWithExtras } from './formatter' import { logfireApiConfig, ScrubbingOptions, serializeAttributes } from './logfireApiConfig' @@ -62,15 +68,16 @@ export function startSpan( attributes: Record = {}, { log, tags = [], level = Level.Info, parentSpan }: LogOptions = {} ): Span { - const [formattedMessage, extraAttributes, newTemplate] = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber) + const { formattedMessage, extraAttributes, newTemplate } = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber) const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : TheContextAPI.active() const span = logfireApiConfig.tracer.startSpan( - formattedMessage, + msgTemplate, { attributes: { ...serializeAttributes({ ...attributes, ...extraAttributes }), [ATTRIBUTES_MESSAGE_TEMPLATE_KEY]: newTemplate, + [ATTRIBUTES_MESSAGE_KEY]: formattedMessage, [ATTRIBUTES_LEVEL_KEY]: level, [ATTRIBUTES_TAGS_KEY]: Array.from(new Set(tags).values()), [ATTRIBUTES_SPAN_TYPE_KEY]: log ? 'log' : 'span', @@ -118,15 +125,16 @@ export function span(msgTemplate: string, ...args: SpanArgsVariant1 | Span callback = args[2] } - const [formattedMessage, extraAttributes, newTemplate] = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber) + const { formattedMessage, extraAttributes, newTemplate } = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber) const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : TheContextAPI.active() return logfireApiConfig.tracer.startActiveSpan( - formattedMessage, + msgTemplate, { attributes: { ...serializeAttributes({ ...attributes, ...extraAttributes }), [ATTRIBUTES_MESSAGE_TEMPLATE_KEY]: newTemplate, + [ATTRIBUTES_MESSAGE_KEY]: formattedMessage, [ATTRIBUTES_LEVEL_KEY]: level, [ATTRIBUTES_TAGS_KEY]: Array.from(new Set(tags).values()), },