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
{{ message }}
This repository was archived by the owner on Jan 21, 2026. It is now read-only.
This module provides automatic tracing for Node.js applications with Stackdriver Trace. [Stackdriver Trace](https://cloud.google.com/cloud-trace/) is a feature of [Google Cloud Platform](https://cloud.google.com/) that collects latency data (traces) from your applications and displays it in near real-time in the [Google Cloud Console][cloud-console].
> **Note**: Using the Trace Agent requires a Google Cloud Project with the [Stackdriver Trace API enabled](https://console.cloud.google.com/flows/enableapi?apiid=cloudtrace) and associated credentials. These values are auto-detected if the application is running on Google Cloud Platform. If your application is not running on GCP, you will need to specify the project ID and credentials either through the configuration object, or with environment variables. See [Setting Up Stackdriver Trace for Node.js][setting-up-stackdriver-trace] for more details.
12
+
13
+
> **Note**: The Trace Agent does not currently work out-of-the-box with Google Cloud Functions (or Firebase Cloud Functions). See [#725](https://github.com/googleapis/cloud-trace-nodejs/issues/725) for a tracking issue and details on how to work around this.
14
+
15
+
Simply require and start the Trace Agent as the first module in your application:
16
+
17
+
```js
18
+
require('@google-cloud/trace-agent').start();
19
+
// ...
20
+
```
21
+
22
+
Optionally, you can pass a [configuration object](https://github.com/googleapis/cloud-trace-nodejs/blob/master/src/config.ts) to the `start()` function as follows:
23
+
24
+
<!-- TODO(kjin): Generate documentation from the public interface of the Trace Agent, and link it here. -->
25
+
26
+
```js
27
+
require('@google-cloud/trace-agent').start({
28
+
samplingRate: 5, // sample 5 traces per second, or at most 1 every 200 milliseconds.
29
+
ignoreUrls: [ /^\/ignore-me/ ] // ignore the "/ignore-me" endpoint.
You can use the [Custom Tracing API](#custom-tracing-api) to trace other modules in your application.
69
+
70
+
To request automatic tracing support for a module not on this list, please [file an issue](https://github.com/googleapis/cloud-trace-nodejs/issues). Alternatively, you can [write a plugin yourself](https://github.com/googleapis/cloud-trace-nodejs/blob/master/doc/plugin-guide.md).
71
+
72
+
### Tracing Additional Modules
73
+
74
+
To load an additional plugin, specify it in the agent's configuration:
75
+
76
+
```js
77
+
require('@google-cloud/trace-agent').start({
78
+
plugins: {
79
+
// You may use a package name or absolute path to the file.
This list of plugins will be merged with the list of built-in plugins, which will be loaded by the plugin loader. Each plugin is only loaded when the module that it patches is loaded; in other words, there is no computational overhead for listing plugins for unused modules.
87
+
88
+
## Custom Tracing API
89
+
90
+
The custom tracing API can be used to create custom trace spans. A *span* is a particular unit of work within a trace, such as an RPC request. Spans may be nested; the outermost span is called a *root span*, even if there are no nested child spans. Root spans typically correspond to incoming requests, while *child spans* typically correspond to outgoing requests, or other work that is triggered in response to incoming requests. This means that root spans shouldn't be created in a context where a root span already exists; a child span is more suitable here. Instead, root spans should be created to track work that happens outside of the request lifecycle entirely, such as periodically scheduled work. To illustrate:
For any of the web frameworks for which we provide [built-in plugins](#what-gets-traced), a root span is automatically started whenever an incoming request is received (in other words, all middleware already runs within a root span). If you wish to record a span outside of any of these frameworks, any traced code must run within a root span that you create yourself.
116
+
117
+
### Accessing the API
118
+
119
+
Calling the `start` function returns an instance of `Tracer`, which provides an interface for tracing:
A `Tracer` object is guaranteed to be returned by both of these calls, even if the agent is disabled.
133
+
134
+
A fully detailed overview of the `Tracer` object is available [here](https://github.com/googleapis/cloud-trace-nodejs/blob/master/doc/trace-api.md).
135
+
136
+
## How does automatic tracing work?
137
+
138
+
The Trace Agent automatically patches well-known modules to insert calls to functions that start, label, and end spans to measure latency of RPCs (such as mysql, redis, etc.) and incoming requests (such as express, hapi, etc.). As each RPC is typically performed on behalf of an incoming request, we must make sure that this association is accurately reflected in span data. To provide a uniform, generalized way of keeping track of which RPC belongs to which incoming request, we rely on [`async_hooks`][async-hooks] to keep track of the "trace context" across asynchronous boundaries.
139
+
140
+
`async_hooks` works well in most cases. However, it does have some limitations that can prevent us from being able to properly propagate trace context:
141
+
142
+
* It is possible that a module does its own queuing of callback functions – effectively merging asynchronous execution contexts. For example, one may write an http request buffering library that queues requests and then performs them in a batch in one shot. In such a case, when all the callbacks fire, they will execute in the context which flushed the queue instead of the context which added the callbacks to the queue. This problem is called the pooling problem or the [user-space queuing problem][queuing-problem], and is a fundamental limitation of JavaScript. If your application uses such code, you will notice that RPCs from many requests are showing up under a single trace, or that certain portions of your outbound RPCs do not get traced. In such cases we try to work around the problem through monkey patching, or by working with the library authors to fix the code to properly propagate context. However, finding problematic code is not always trivial.
143
+
* The `async_hooks` API has [issues tracking context](https://github.com/nodejs/node/issues/26064) around `await`-ed "thenables" (rather than real promises). Requests originating from the body of a `then` implementation in such a user-space "thenable" may not get traced. This is largely an unconventional case but is present in the `knex` module, which monkeypatches the Bluebird Promise's prototype to make database calls. __If you are using `knex` (esp. the `raw` function), see [#946](https://github.com/googleapis/cloud-trace-nodejs/issues/946) for more details on whether you are affected, as well as a suggested workaround.__
144
+
145
+
### Tracing bundled or webpacked server code.
146
+
147
+
*unsupported*
148
+
149
+
The Trace Agent does not support bundled server code, so bundlers like webpack or @zeit/ncc will not work.
*[Installing the client library](#installing-the-client-library)
32
+
33
+
*[Samples](#samples)
34
+
*[Versioning](#versioning)
35
+
*[Contributing](#contributing)
36
+
*[License](#license)
37
+
38
+
## Quickstart
39
+
40
+
### Before you begin
41
+
42
+
1.[Select or create a Cloud Platform project][projects].
43
+
1.[Enable the Stackdriver Trace API][enable_api].
44
+
1.[Set up authentication with a service account][auth] so you can access the
45
+
API from your local workstation.
46
+
47
+
### Installing the client library
48
+
49
+
```bash
50
+
npm install @google-cloud/trace-agent
51
+
```
9
52
10
-
> **Beta**. *This is a Beta release of the Stackdriver Trace agent for Node.js. These libraries might be changed in backward-incompatible ways and are not subject to any SLA or deprecation policy.*
11
53
12
54
This module provides automatic tracing for Node.js applications with Stackdriver Trace. [Stackdriver Trace](https://cloud.google.com/cloud-trace/) is a feature of [Google Cloud Platform](https://cloud.google.com/) that collects latency data (traces) from your applications and displays it in near real-time in the [Google Cloud Console][cloud-console].
13
55
@@ -83,13 +125,13 @@ To request automatic tracing support for a module not on this list, please [file
83
125
To load an additional plugin, specify it in the agent's configuration:
84
126
85
127
```js
86
-
require('@google-cloud/trace-agent').start({
87
-
plugins: {
88
-
// You may use a package name or absolute path to the file.
This list of plugins will be merged with the list of built-in plugins, which will be loaded by the plugin loader. Each plugin is only loaded when the module that it patches is loaded; in other words, there is no computational overhead for listing plugins for unused modules.
@@ -157,6 +199,47 @@ The Trace Agent automatically patches well-known modules to insert calls to func
157
199
158
200
The Trace Agent does not support bundled server code, so bundlers like webpack or @zeit/ncc will not work.
Samples are in the [`samples/`](https://github.com/googleapis/cloud-trace-nodejs/tree/master/samples) directory. The samples' `README.md`
| App |[source code](https://github.com/googleapis/cloud-trace-nodejs/blob/master/samples/app.js)|[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/cloud-trace-nodejs&page=editor&open_in_editor=samples/app.js,samples/README.md)|
218
+
| Snippets |[source code](https://github.com/googleapis/cloud-trace-nodejs/blob/master/samples/snippets.js)|[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/cloud-trace-nodejs&page=editor&open_in_editor=samples/snippets.js,samples/README.md)|
219
+
220
+
221
+
222
+
The [Stackdriver Trace Node.js Client API Reference][client-docs] documentation
223
+
also contains samples.
224
+
225
+
## Versioning
226
+
227
+
This library follows [Semantic Versioning](http://semver.org/).
228
+
229
+
230
+
231
+
This library is considered to be in **beta**. This means it is expected to be
232
+
mostly stable while we work toward a general availability release; however,
233
+
complete stability is not guaranteed. We will address issues and requests
234
+
against beta libraries with a high priority.
235
+
236
+
237
+
238
+
239
+
More Information: [Google Cloud Platform Launch Stages][launch_stages]
Contributions welcome! See the [Contributing Guide](https://github.com/googleapis/cloud-trace-nodejs/blob/master/CONTRIBUTING.md).
@@ -167,19 +250,12 @@ Apache Version 2.0
167
250
168
251
See [LICENSE](https://github.com/googleapis/cloud-trace-nodejs/blob/master/LICENSE)
0 commit comments