Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-bats-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pydantic/logfire-api": patch
---

Do not format span_name
1 change: 1 addition & 0 deletions packages/logfire-api/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
28 changes: 17 additions & 11 deletions packages/logfire-api/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,27 @@ 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<string, unknown>, scrubber: BaseScrubber): string {
return logfireFormatWithExtras(formatString, record, scrubber)[0]
}

/**
* Format a string with additional information about attributes and templates
*/
export function logfireFormatWithExtras(
formatString: string,
record: Record<string, unknown>,
scrubber: BaseScrubber
): [string, Record<string, unknown>, string] {
): {
extraAttributes: Record<string, unknown>
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}`)
Expand All @@ -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,
}
}
}

Expand Down
18 changes: 13 additions & 5 deletions packages/logfire-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -62,15 +68,16 @@ export function startSpan(
attributes: Record<string, unknown> = {},
{ 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',
Expand Down Expand Up @@ -118,15 +125,16 @@ export function span<R>(msgTemplate: string, ...args: SpanArgsVariant1<R> | 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()),
},
Expand Down