Skip to content

Commit fac89ec

Browse files
committed
Document reportError
Add note about CF testing. Fixes #25
1 parent e6f01b3 commit fac89ec

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

.changeset/famous-toes-punch.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+
logfire.reportError - documentation and setting correct span type

.changeset/pink-buses-fry.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+
Document and slightly enhance the `reportError` function.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ export default instrument(handler, {
122122

123123
A working example can be found in the `examples/cloudflare-worker` directory.
124124

125+
Note: if you're testing your worker with Vitest, you need to add the following additional configuration to your `vitest.config.mts`:
126+
127+
```
128+
export default defineWorkersConfig({
129+
test: {
130+
deps: {
131+
optimizer: {
132+
ssr: {
133+
enabled: true,
134+
include: ['@pydantic/logfire-cf-workers'],
135+
},
136+
},
137+
},
138+
poolOptions: {
139+
workers: {
140+
// ...
141+
},
142+
},
143+
},
144+
});
145+
```
146+
125147
### Next.js/Vercel
126148

127149
Vercel provides a comprehensive OpenTelemetry integration through the
@@ -271,6 +293,18 @@ function info(
271293
): void;
272294
```
273295

296+
### Reporting errors
297+
298+
In addition to `trace`, `debug`, the Logfire API exports a `reportError` function that accepts a message and a JavaScript `Error` object. It will extract the necessary details from the error and create a span with the `error` level.
299+
300+
```ts
301+
try {
302+
1 / 0
303+
} catch (error) {
304+
logfire.reportError("An error occurred", error);
305+
}
306+
```
307+
274308
## Contributing
275309

276310
See [CONTRIBUTING.md](CONTRIBUTING.md) for development instructions.

examples/node/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ logfire.configure({
55
serviceName: 'example-node-script',
66
serviceVersion: '1.0.0',
77
environment: 'staging',
8-
token: 'pylf_v1_eu_fcksvB6FNdWKZ3xGbrG8g8GXHFqPfFXgtRgnZdvV6PCj',
98
diagLogLevel: logfire.DiagLogLevel.DEBUG,
10-
codeSource: {
11-
repository: 'https://github.com/pydantic/pydantic',
12-
revision: 'master',
13-
},
149
})
1510

1611

@@ -27,3 +22,12 @@ logfire.span('Hello from Node.js, {next_player}', {
2722
}, (span) => {
2823
span.end()
2924
})
25+
26+
if (process.env.TRIGGER_ERROR) {
27+
try {
28+
throw new Error('This is an error for testing purposes');
29+
} catch (error) {
30+
logfire.reportError("An error occurred", error as Error);
31+
console.error("An error occurred:", error);
32+
}
33+
}

packages/logfire-api/src/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,23 @@ export function warning(message: string, attributes: Record<string, unknown> = {
119119
log(message, attributes, { ...options, level: Level.Warning })
120120
}
121121

122+
/**
123+
* Use this method to report an error to Logfire.
124+
* Captures the error stack trace and message in the respective semantic attributes and sets the correct level and status.
125+
*/
122126
export function reportError(message: string, error: Error, extraAttributes: Record<string, unknown> = {}) {
123-
const span = startSpan(message, {
124-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
125-
[ATTR_EXCEPTION_MESSAGE]: error.message ?? 'error',
126-
[ATTR_EXCEPTION_STACKTRACE]: error.stack,
127-
...extraAttributes,
128-
})
127+
const span = startSpan(
128+
message,
129+
{
130+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
131+
[ATTR_EXCEPTION_MESSAGE]: error.message ?? 'error',
132+
[ATTR_EXCEPTION_STACKTRACE]: error.stack,
133+
...extraAttributes,
134+
},
135+
{
136+
level: Level.Error,
137+
}
138+
)
129139

130140
span.recordException(error)
131141
span.setStatus({ code: SpanStatusCode.ERROR })

0 commit comments

Comments
 (0)