You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From the team behind [Pydantic](https://pydantic.dev/), **Logfire** is an observability platform built on the same belief as our
4
-
open source library — that the most powerful tools can be easy to use.
3
+
From the team behind [Pydantic](https://pydantic.dev/), **Logfire** is an
4
+
observability platform built on the same belief as our open source library —
5
+
that the most powerful tools can be easy to use.
5
6
6
7
What sets Logfire apart:
7
8
8
-
-**Simple and Powerful:** Logfire's dashboard is simple relative to the power it provides, ensuring your entire engineering team will actually use it.
9
-
-**SQL:** Query your data using standard SQL — all the control and (for many) nothing new to learn. Using SQL also means you can query your data with existing BI tools and database querying libraries.
10
-
-**OpenTelemetry:** Logfire is an opinionated wrapper around OpenTelemetry, allowing you to leverage existing tooling, infrastructure, and instrumentation for many common packages, and enabling support for virtually any language. We offer full support for all OpenTelemetry signals (traces, metrics and logs).
9
+
-**Simple and Powerful:** Logfire's dashboard is simple relative to the power
10
+
it provides, ensuring your entire engineering team will actually use it.
11
+
-**SQL:** Query your data using standard SQL — all the control and (for many)
12
+
nothing new to learn. Using SQL also means you can query your data with
13
+
existing BI tools and database querying libraries.
14
+
-**OpenTelemetry:** Logfire is an opinionated wrapper around OpenTelemetry,
15
+
allowing you to leverage existing tooling, infrastructure, and instrumentation
16
+
for many common packages, and enabling support for virtually any language. We
17
+
offer full support for all OpenTelemetry signals (traces, metrics, and logs).
11
18
12
-
**Feel free to report issues and ask any questions about Logfire in this repository!**
19
+
**Feel free to report issues and ask any questions about Logfire in this
20
+
repository!**
13
21
14
-
This repo contains the JavaScript SDK for `logfire` and its documentation; the server application for recording and displaying data is closed source.
22
+
This repository contains the JavaScript SDK for `logfire` and its documentation;
23
+
the server application for recording and displaying data is closed source.
15
24
16
25
<imgwidth="1394"alt="Logfire UI with Next.js traces"src="https://github.com/user-attachments/assets/a2a1167b-6bf7-4d6a-8d59-81cb2433c8e9" />
17
26
18
27
## Usage
19
28
20
-
Depending on your environment, you can integrate Logfire in several ways. Follow the specific instructions below:
29
+
Depending on your environment, you can integrate Logfire in several ways. Follow
30
+
the specific instructions below:
21
31
22
32
### Basic Node.js script
23
33
24
-
Using logfire from your Node.js script is as simple as [getting a write token](https://logfire.pydantic.dev/docs/how-to-guides/create-write-tokens/), installing the package, calling configure, and using the provided API. Let's create an empty project:
34
+
Using Logfire from your Node.js script is as simple as
35
+
[getting a write token](https://logfire.pydantic.dev/docs/how-to-guides/create-write-tokens/),
36
+
installing the package, calling configure, and using the provided API. Let's
37
+
create an empty project:
25
38
26
39
```sh
27
-
mkdir test-logfire-js;
28
-
cd test-logfire-js;
29
-
npm init -y es6 #makes the package.json with `type: module`
40
+
mkdir test-logfire-js
41
+
cd test-logfire-js
42
+
npm init -y es6 #creates package.json with `type: module`
30
43
npm install logfire
31
44
```
32
45
33
46
Then, create the following `hello.js` script in the directory:
34
47
35
-
36
48
```js
37
-
import*aslogfirefrom'logfire'
49
+
import*aslogfirefrom"logfire";
38
50
39
51
logfire.configure({
40
-
token:'test-e2e-write-token',
52
+
token:"test-e2e-write-token",
41
53
advanced: {
42
-
baseUrl:'http://localhost:8000'
54
+
baseUrl:"http://localhost:8000",
43
55
},
44
-
serviceName:'example-node-script',
45
-
serviceVersion:'1.0.0',
46
-
})
47
-
56
+
serviceName:"example-node-script",
57
+
serviceVersion:"1.0.0",
58
+
});
48
59
49
-
logfire.info('Hello from Node.js', {
50
-
'attribute-key':'attribute-value'
60
+
logfire.info("Hello from Node.js", {
61
+
"attribute-key":"attribute-value",
51
62
}, {
52
-
tags: ['example', 'example2']
53
-
})
63
+
tags: ["example", "example2"],
64
+
});
54
65
```
55
66
56
-
Run the script above with `node hello.js`, and you should see the span being logged in the live view of your Logfire project.
57
-
58
-
67
+
Run the script with `node hello.js`, and you should see the span being logged in
68
+
the live view of your Logfire project.
59
69
60
70
### Cloudflare Workers
61
71
62
-
First, install the `@pydantic/logfire-cf-workers @pydantic/logfire-api` NPM packages:
63
-
64
-
> [!NOTE]
65
-
> `@pydantic/logfire-cf-workers` wraps the [`@microlabs/otel-cf-workers`](https://github.com/evanderkoogh/otel-cf-workers) NPM package.
72
+
First, install the `@pydantic/logfire-cf-workers @pydantic/logfire-api` NPM
Next, add `compatibility_flags = [ "nodejs_compat" ]` to your wrangler.toml or `"compatibility_flags": ["nodejs_compat"]` if you're using `wrangler.jsonc`.
71
78
72
-
Add your [Logfire write token](https://logfire.pydantic.dev/docs/how-to-guides/create-write-tokens/) to your `.dev.vars` file.
79
+
Next, add `compatibility_flags = [ "nodejs_compat" ]` to your wrangler.toml or
80
+
`"compatibility_flags": ["nodejs_compat"]` if you're using `wrangler.jsonc`.
For the production deployment, check the [Cloudflare documentation for further details on how to manage and deploy the secrets](https://developers.cloudflare.com/workers/configuration/secrets/).
90
+
For production deployment, check the
91
+
[Cloudflare documentation for details on managing and deploying secrets](https://developers.cloudflare.com/workers/configuration/secrets/).
79
92
80
-
One way to do that is through the `npx wrangler` command:
93
+
One way to do this is through the `npx wrangler` command:
81
94
82
95
```sh
83
96
npx wrangler secret put LOGFIRE_TOKEN
84
97
```
85
98
86
-
87
-
Next, add the necessary instrumentation around your handler. The `tracerConfig`function will extract your write token from the `env` object and provide the necessary configuration for the instrumentation:
88
-
99
+
Next, add the necessary instrumentation around your handler. The `tracerConfig`
100
+
function will extract your write token from the `env` object and provide the
logfire.info('info span from inside the worker body');
98
-
returnnewresponse('hello world!');
108
+
async fetch():Promise<Response> {
109
+
logfire.info("info span from inside the worker body");
110
+
returnnewResponse("hello world!");
99
111
},
100
-
} satisfiesexportedHandler;
112
+
} satisfiesExportedHandler;
101
113
102
114
exportdefaultinstrument(handler, {
103
-
serviceName: 'cloudflare-worker',
104
-
serviceNamespace: '',
105
-
serviceVersion: '1.0.0',
115
+
serviceName: "cloudflare-worker",
116
+
serviceNamespace: "",
117
+
serviceVersion: "1.0.0",
106
118
});
107
119
```
108
120
109
-
A working example can be found in the `examples/cloudflare-worker` directory.
121
+
A working example can be found in the `examples/cloudflare-worker` directory.
110
122
111
-
### Next.js / Vercel
123
+
### Next.js/Vercel
112
124
113
-
Vercel provides a comprehensive OpenTelemetry integration through the `@vercel/otel` package. After following [their integration instructions](https://vercel.com/docs/otel), Add the following env variables to your project:
125
+
Vercel provides a comprehensive OpenTelemetry integration through the
126
+
`@vercel/otel` package. After following
127
+
[their integration instructions](https://vercel.com/docs/otel), add the
The above will point the instrumentation to Logfire.
136
+
This will point the instrumentation to Logfire.
122
137
123
138
> [!NOTE]
124
-
> The Vercel production deployments have a caching mechanism that might prevent the changes from taking effect immediately or spans being reported. If you are not seeing spans in Logfire, you can [clear the data cache for your project](https://vercel.com/docs/data-cache/manage-data-cache).
139
+
> Vercel production deployments have a caching mechanism that might prevent
140
+
> changes from taking effect immediately or spans from being reported. If you
141
+
> are not seeing spans in Logfire, you can
142
+
> [clear the data cache for your project](https://vercel.com/docs/data-cache/manage-data-cache).
125
143
126
-
Optionally, you can use the Logfire API package for creating manual spans. Install the `@pydantic/logfire-api` NPM package and call the respective methods from your server-side code:
144
+
Optionally, you can use the Logfire API package for creating manual spans.
145
+
Install the `@pydantic/logfire-api` NPM package and call the respective methods
146
+
from your server-side code:
127
147
128
148
```tsx
129
-
import*aslogfirefrom'@pydantic/logfire-api'
149
+
import*aslogfirefrom"@pydantic/logfire-api";
130
150
131
151
exportdefaultasyncfunction Home() {
132
-
returnlogfire.span('A warning span', {}, {
133
-
level: logfire.Level.Warning,
152
+
returnlogfire.span("A warning span", {}, {
153
+
level: logfire.Level.Warning,
134
154
}, async (span) => {
135
-
logfire.info('Nested info span');
155
+
logfire.info("Nested info span");
136
156
// ending the span is necessary to ensure it is reported
137
-
span.end()
157
+
span.end();
138
158
return <div>Hello</div>;
139
-
})
159
+
});
140
160
}
141
161
```
142
162
143
163
A working example can be found in the `examples/nextjs` directory.
144
164
165
+
#### Next.js client-side instrumentation
166
+
167
+
The `@vercel/otel` package does not support client-side instrumentation, so few additional steps are necessary to send spans and/or instrument the client-side.
168
+
For a working example, refer to the `examples/nextjs-client-side-instrumentation` directory, which instruments the client-side `fetch` calls.
169
+
145
170
### Express, generic Node instrumentation
146
171
147
-
For the purposes of the example, we will instrument this simple Express app:
172
+
For this example, we will instrument a simple Express app:
148
173
149
174
```ts
150
175
/*app.ts*/
@@ -166,30 +191,32 @@ app.listen(PORT, () => {
166
191
});
167
192
```
168
193
169
-
Next, we will install the `logfire` and `dotenv` NPM packages, so that we can keep our Logfire write token in a `.env` file:
194
+
Next, install the `logfire` and `dotenv` NPM packages to keep your Logfire write
195
+
token in a `.env` file:
170
196
171
197
```sh
172
198
npm install logfire dotenv
173
199
```
174
200
175
201
Add your token to the `.env` file:
176
202
177
-
178
203
```sh
179
204
LOGFIRE_TOKEN=your-write-token
180
205
```
181
206
182
-
Afterwards, we will create an `instrumentation.ts` file that will set up the instrumentation. The `logfire` package includes a `configure` function that simplifies the setup:
207
+
Then, create an `instrumentation.ts` file to set up the instrumentation. The
208
+
`logfire` package includes a `configure` function that simplifies the setup:
183
209
184
210
```ts
185
211
// instrumentation.ts
186
-
import*aslogfirefrom'logfire'
187
-
import'dotenv/config'
212
+
import*aslogfirefrom"logfire";
213
+
import"dotenv/config";
188
214
189
-
logfire.configure()
215
+
logfire.configure();
190
216
```
191
217
192
-
The `logfire.configure` call should happen before the actual express module imports, so your NPM start script should look something like this (`package.json`):
218
+
The `logfire.configure` call should happen before the actual express module
219
+
imports, so your NPM start script should look like this (`package.json`):
193
220
194
221
```json
195
222
"scripts": {
@@ -199,36 +226,47 @@ The `logfire.configure` call should happen before the actual express module impo
199
226
200
227
## Deno
201
228
202
-
Deno has [built-in support for OpenTelemetry](https://docs.deno.com/runtime/fundamentals/open_telemetry/). The examples directory includes a `Hello world` example that configures Deno OTel export to Logfire through environment variables.
229
+
Deno has
230
+
[built-in support for OpenTelemetry](https://docs.deno.com/runtime/fundamentals/open_telemetry/).
231
+
The examples directory includes a `Hello world` example that configures Deno
232
+
OTel export to Logfire through environment variables.
203
233
204
-
Optionally, you can use the Logfire API package for creating manual spans. Install the `@pydantic/logfire-api` NPM package and call the respective methods from your code.
234
+
Optionally, you can use the Logfire API package for creating manual spans.
235
+
Install the `@pydantic/logfire-api` NPM package and call the respective methods
236
+
from your code.
205
237
206
-
####Configuring the instrumentation
238
+
### Configuring the instrumentation
207
239
208
-
The `logfire.configure` function accepts a set of configuration options that control the behavior of the instrumentation. Alternatively, you can [use environment variables](https://logfire.pydantic.dev/docs/reference/configuration/#programmatically-via-configure) to configure the instrumentation.
240
+
The `logfire.configure` function accepts a set of configuration options that
241
+
control the behavior of the instrumentation. Alternatively, you can
The `@pydantic/logfire-api` exports several convenience wrappers around the OpenTelemetry span creation API. The `logfire` package re-exports those.
247
+
The `@pydantic/logfire-api` exports several convenience wrappers around the
248
+
OpenTelemetry span creation API. The `logfire` package re-exports these.
213
249
214
-
The following methods create spans with the respective log levels (ordered by severity):
250
+
The following methods create spans with their respective log levels (ordered by
251
+
severity):
215
252
216
-
*`logfire.trace`
217
-
*`logfire.debug`
218
-
*`logfire.info`
219
-
*`logfire.notice`
220
-
*`logfire.warn`
221
-
*`logfire.error`
222
-
*`logfire.fatal`
253
+
-`logfire.trace`
254
+
-`logfire.debug`
255
+
-`logfire.info`
256
+
-`logfire.notice`
257
+
-`logfire.warn`
258
+
-`logfire.error`
259
+
-`logfire.fatal`
223
260
224
-
Each method accepts a message, attributes, and, optionally options that let you specify the span tags. The attributes values must be serializable to JSON.
261
+
Each method accepts a message, attributes, and optionally, options that let you
262
+
specify the span tags. The attribute values must be serializable to JSON.
0 commit comments