diff --git a/README.md b/README.md index fa0e7d22..1815e266 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,21 @@ how to populate the Http request metadata for log entries. If you already have a "raw" Http `request` object you can assign it to `entry.metadata.httpRequest` directly. More information about how the `request` is interpreted as raw can be found in the [code](https://github.com/googleapis/nodejs-logging/blob/15849160116a814ab71113138cb211c2e0c2d4b4/src/entry.ts#L224-L238). +## Automatic Trace/Span Id Extraction +Cloud Logging libraries use [trace fields within LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.trace) to capture trace contexts, which enables the [correlation of logs and traces](https://cloud.google.com/logging/docs/view/correlate-logs), and supporting distributed tracing. +These trace fields, including [trace](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.trace), [spanId](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.span_id), and [traceSampled](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.trace_sampled) define the trace context for the log entry. + +If not provided explicitly, Google Cloud Logging library will automatically populate `LogEntry fields`_ +`trace`, `span_id`, and `trace_sampled` from detected OpenTelemetry span context, or HTTP request headers. + +### Extracting from OpenTelemetry Context +If you are using OpenTelemetry and there is an active span in the OpenTelemetry Context, that log entry will automatically have the `trace`, `span_id`, and `trace_sampled` fields populated from that span. More information about OpenTelemetry can be found [here](https://opentelemetry.io/docs/languages/js/). + +### Extracting from HTTP headers +If tracing fields are not provided explicitly and no OpenTelemetry context were detected, `trace` / `span_id` fields will be extracted automatically from HTTP headers. +Trace information is automatically populated from either the `[W3C Traceparent](https://www.w3.org/TR/trace-context)` or `[X-Cloud-Trace-Context](https://cloud.google.com/trace/docs/trace-context#legacy-http-header)` headers. +Populating trace information this way also automatically populates the `http_request` field in the `LogEntry` as well. + ## Error handling with logs written or deleted asynchronously The `Log` class provide users the ability to write and delete logs asynchronously. However, there are cases when log entries diff --git a/test/gapic_config_service_v2_v2.ts b/test/gapic_config_service_v2_v2.ts index 431eaaa7..7f6f4748 100644 --- a/test/gapic_config_service_v2_v2.ts +++ b/test/gapic_config_service_v2_v2.ts @@ -4251,9 +4251,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listBuckets.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4299,9 +4299,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listBuckets.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4342,9 +4342,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listBuckets.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4384,9 +4384,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listBuckets.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); }); @@ -4547,9 +4547,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listViews.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4595,9 +4595,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listViews.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4637,9 +4637,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listViews.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4678,9 +4678,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listViews.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); }); @@ -4841,9 +4841,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listSinks.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4889,9 +4889,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listSinks.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4931,9 +4931,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listSinks.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -4972,9 +4972,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listSinks.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); }); @@ -5135,9 +5135,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listLinks.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -5183,9 +5183,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listLinks.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -5225,9 +5225,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listLinks.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -5266,9 +5266,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listLinks.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); }); @@ -5432,9 +5432,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listExclusions.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -5478,9 +5478,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listExclusions.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -5521,9 +5521,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listExclusions.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -5561,9 +5561,9 @@ describe('v2.ConfigServiceV2Client', () => { assert( (client.descriptors.page.listExclusions.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); }); diff --git a/test/gapic_logging_service_v2_v2.ts b/test/gapic_logging_service_v2_v2.ts index ba8b6519..7a5583ec 100644 --- a/test/gapic_logging_service_v2_v2.ts +++ b/test/gapic_logging_service_v2_v2.ts @@ -1206,9 +1206,9 @@ describe('v2.LoggingServiceV2Client', () => { assert( (client.descriptors.page.listLogs.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -1254,9 +1254,9 @@ describe('v2.LoggingServiceV2Client', () => { assert( (client.descriptors.page.listLogs.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -1292,9 +1292,9 @@ describe('v2.LoggingServiceV2Client', () => { assert( (client.descriptors.page.listLogs.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -1333,9 +1333,9 @@ describe('v2.LoggingServiceV2Client', () => { assert( (client.descriptors.page.listLogs.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); }); diff --git a/test/gapic_metrics_service_v2_v2.ts b/test/gapic_metrics_service_v2_v2.ts index cc9eba4c..e0be7d5a 100644 --- a/test/gapic_metrics_service_v2_v2.ts +++ b/test/gapic_metrics_service_v2_v2.ts @@ -984,9 +984,9 @@ describe('v2.MetricsServiceV2Client', () => { assert( (client.descriptors.page.listLogMetrics.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -1030,9 +1030,9 @@ describe('v2.MetricsServiceV2Client', () => { assert( (client.descriptors.page.listLogMetrics.createStream as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -1073,9 +1073,9 @@ describe('v2.MetricsServiceV2Client', () => { assert( (client.descriptors.page.listLogMetrics.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); @@ -1113,9 +1113,9 @@ describe('v2.MetricsServiceV2Client', () => { assert( (client.descriptors.page.listLogMetrics.asyncIterate as SinonStub) .getCall(0) - .args[2].otherArgs.headers['x-goog-request-params'].includes( - expectedHeaderRequestParams - ) + .args[2].otherArgs.headers[ + 'x-goog-request-params' + ].includes(expectedHeaderRequestParams) ); }); });