Google Cloud provides powerful monitoring, logging, and diagnostics for
Node.js applications.

The Node.js client libraries are instrumented to emit tracing data. The
instrumentation is opt-in; you need to explicitly enable it. This document
describes the available signals and how to enable them.

## Available signals

The Node.js client libraries are instrumented to generate the following signals:

1. `INFO` spans for each logical client request. Typically a single method call in the client struct gets such a span (for example, calling `accessSecretVersion` on a `SecretManagerServiceClient`).
2. `INFO` spans for each low-level RPC attempt. Typically a single method in the client struct gets one such span, but there may be more if the library has to retry the RPC.

These spans and logs follow [OpenTelemetry Semantic
Conventions](https://opentelemetry.io/docs/specs/semconv/) with additional
Google Cloud attributes. Both the spans and logs should be suitable for
production monitoring. The signals include standard OpenTelemetry attributes
(for example, `http.response.status_code` and `rpc.system.name`).

For a full list of standard attributes, see the [OpenTelemetry HTTP and gRPC
semantic conventions](https://opentelemetry.io/docs/specs/semconv/).

## Enable telemetry signals

> [!WARNING]
> Observability signals at any level may contain sensitive data such as
> resource names, full URLs, and error messages.
>
> Before configuring subscribers or exporters for traces, review the
> contents of the spans and consult the tracing framework documentation to
> prevent leaking sensitive information, depending on your intended use case.

To protect sensitive data, telemetry signals are disabled by default.

In Node.js, you must configure the client to emit traces **and** must configure
subscribers and exporters to send these signals to an external service.

To configure the client, you can set the following environment variable:

```bash
export GOOGLE_SDK_NODE_ENABLE_TRACING=true
```

When tracing is enabled and your application has an active OpenTelemetry span
in the current context when you call a client library method, the client
library automatically links its spans as children of your active span.

## Exporting traces

Once telemetry is enabled in the client libraries, your application must be
configured to collect and export this data to your observability service.

For detailed instructions on configuring the OpenTelemetry trace correlation
data, see the [observability sample](https://github.com/googleapis/google-cloud-node/tree/main/core/packages/gax/samples/observability.js) within the Node.js repository.

To export the `tracing` spans generated by the Google Cloud client libraries
to OpenTelemetry, you need to set up a tracer provider and exporter:

```bash
const cloudTraceExporter = new TraceExporter();
const spanProcessor = new BatchSpanProcessor(cloudTraceExporter);

const provider = new NodeTracerProvider({
  spanProcessors: [spanProcessor],
});
provider.register();
```