Skip to content

Commit 8c57b16

Browse files
authored
Do not pass the formatted message into span_name (#65)
1 parent fd67ebf commit 8c57b16

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

.changeset/cyan-bats-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pydantic/logfire-api": patch
3+
---
4+
5+
Do not format span_name

packages/logfire-api/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const ATTRIBUTES_LEVEL_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.level_num`
77
export const ATTRIBUTES_SPAN_TYPE_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.span_type`
88
export const ATTRIBUTES_TAGS_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.tags`
99
export const ATTRIBUTES_MESSAGE_TEMPLATE_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.msg_template`
10+
export const ATTRIBUTES_MESSAGE_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.msg`
1011

1112
/** Key for storing scrubbed attributes information */
1213
export const ATTRIBUTES_SCRUBBED_KEY = `${LOGFIRE_ATTRIBUTES_NAMESPACE}.scrubbed`

packages/logfire-api/src/formatter.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,27 @@ class ChunksFormatter {
218218
// Create singleton instance
219219
export const chunksFormatter = new ChunksFormatter()
220220

221-
/**
222-
* Format a string using a Python-like template syntax
223-
*/
224-
export function logfireFormat(formatString: string, record: Record<string, unknown>, scrubber: BaseScrubber): string {
225-
return logfireFormatWithExtras(formatString, record, scrubber)[0]
226-
}
227-
228221
/**
229222
* Format a string with additional information about attributes and templates
230223
*/
231224
export function logfireFormatWithExtras(
232225
formatString: string,
233226
record: Record<string, unknown>,
234227
scrubber: BaseScrubber
235-
): [string, Record<string, unknown>, string] {
228+
): {
229+
extraAttributes: Record<string, unknown>
230+
formattedMessage: string
231+
newTemplate: string
232+
} {
236233
try {
237-
const [chunks, extraAttrs, newTemplate] = chunksFormatter.chunks(formatString, record, scrubber)
234+
const [chunks, extraAttributes, newTemplate] = chunksFormatter.chunks(formatString, record, scrubber)
238235

239-
return [chunks.map((chunk) => chunk.value).join(''), extraAttrs, newTemplate]
236+
const formattedMessage = chunks.map((chunk) => chunk.value).join('')
237+
return {
238+
extraAttributes,
239+
formattedMessage,
240+
newTemplate,
241+
}
240242
} catch (err) {
241243
if (err instanceof KnownFormattingError) {
242244
console.warn(`Formatting error: ${err.message}`)
@@ -245,7 +247,11 @@ export function logfireFormatWithExtras(
245247
}
246248

247249
// Formatting failed, use the original format string as the message
248-
return [formatString, {}, formatString]
250+
return {
251+
extraAttributes: {},
252+
formattedMessage: formatString,
253+
newTemplate: formatString,
254+
}
249255
}
250256
}
251257

packages/logfire-api/src/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import { Span, SpanStatusCode, context as TheContextAPI, trace as TheTraceAPI } from '@opentelemetry/api'
33
import { ATTR_EXCEPTION_MESSAGE, ATTR_EXCEPTION_STACKTRACE } from '@opentelemetry/semantic-conventions'
44

5-
import { ATTRIBUTES_LEVEL_KEY, ATTRIBUTES_MESSAGE_TEMPLATE_KEY, ATTRIBUTES_SPAN_TYPE_KEY, ATTRIBUTES_TAGS_KEY } from './constants'
5+
import {
6+
ATTRIBUTES_LEVEL_KEY,
7+
ATTRIBUTES_MESSAGE_KEY,
8+
ATTRIBUTES_MESSAGE_TEMPLATE_KEY,
9+
ATTRIBUTES_SPAN_TYPE_KEY,
10+
ATTRIBUTES_TAGS_KEY,
11+
} from './constants'
612
import { logfireFormatWithExtras } from './formatter'
713
import { logfireApiConfig, ScrubbingOptions, serializeAttributes } from './logfireApiConfig'
814

@@ -62,15 +68,16 @@ export function startSpan(
6268
attributes: Record<string, unknown> = {},
6369
{ log, tags = [], level = Level.Info, parentSpan }: LogOptions = {}
6470
): Span {
65-
const [formattedMessage, extraAttributes, newTemplate] = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber)
71+
const { formattedMessage, extraAttributes, newTemplate } = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber)
6672

6773
const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : TheContextAPI.active()
6874
const span = logfireApiConfig.tracer.startSpan(
69-
formattedMessage,
75+
msgTemplate,
7076
{
7177
attributes: {
7278
...serializeAttributes({ ...attributes, ...extraAttributes }),
7379
[ATTRIBUTES_MESSAGE_TEMPLATE_KEY]: newTemplate,
80+
[ATTRIBUTES_MESSAGE_KEY]: formattedMessage,
7481
[ATTRIBUTES_LEVEL_KEY]: level,
7582
[ATTRIBUTES_TAGS_KEY]: Array.from(new Set(tags).values()),
7683
[ATTRIBUTES_SPAN_TYPE_KEY]: log ? 'log' : 'span',
@@ -118,15 +125,16 @@ export function span<R>(msgTemplate: string, ...args: SpanArgsVariant1<R> | Span
118125
callback = args[2]
119126
}
120127

121-
const [formattedMessage, extraAttributes, newTemplate] = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber)
128+
const { formattedMessage, extraAttributes, newTemplate } = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber)
122129

123130
const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : TheContextAPI.active()
124131
return logfireApiConfig.tracer.startActiveSpan(
125-
formattedMessage,
132+
msgTemplate,
126133
{
127134
attributes: {
128135
...serializeAttributes({ ...attributes, ...extraAttributes }),
129136
[ATTRIBUTES_MESSAGE_TEMPLATE_KEY]: newTemplate,
137+
[ATTRIBUTES_MESSAGE_KEY]: formattedMessage,
130138
[ATTRIBUTES_LEVEL_KEY]: level,
131139
[ATTRIBUTES_TAGS_KEY]: Array.from(new Set(tags).values()),
132140
},

0 commit comments

Comments
 (0)