Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

feat: users can log raw http request objects with trace#1086

Merged
freelerobot merged 25 commits intomasterfrom
writeHttpRequest
Jun 7, 2021
Merged

feat: users can log raw http request objects with trace#1086
freelerobot merged 25 commits intomasterfrom
writeHttpRequest

Conversation

@freelerobot
Copy link
Copy Markdown
Contributor

@freelerobot freelerobot commented Jun 3, 2021

A. Users can log raw http.incomingmessage (requests) now:

  1. Users can now log.write(entry.metadata.requestObj) where reqObj is a raw incoming HTTP request type.
  2. Users can now log.write(entry.metadata.requestObj) where reqObj is an exported CloudLoggingHttpRequest type.
  3. The above mentioned request logs are auto-formatted into GCP compliant structured request logs. See LogEntry guidelines: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
  4. Lifted make-http-requests out of /middleware subdirectories. Since it’s not just for middleware. This is cleaner, as http-request was already in the main directory.

B. Users can force tracing by providing x-cloud-trace-context in http headers (in various formats):
See: https://cloud.google.com/trace/docs/setup#force-trace

  1. trace, spanID and traceSampled fields in X-Cloud-Trace-Context can be optional (see context/trace_context.proto)
  2. TraceSample should default to false (verified with nwang@)
  3. Trace/span IDs should still be included in log contexts even when traceSampled is false, because "A non-sampled trace value is still useful as a request correlation identifier." (LogEntry spec)

Fixes: #1083

@product-auto-label product-auto-label Bot added the api: logging Issues related to the googleapis/nodejs-logging API. label Jun 3, 2021
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Jun 3, 2021
@freelerobot freelerobot changed the title Write http request feat: users can log raw http request objects Jun 3, 2021
@freelerobot freelerobot self-assigned this Jun 3, 2021
@freelerobot freelerobot changed the title feat: users can log raw http request objects feat: users can log raw http request objects with trace Jun 3, 2021
@freelerobot freelerobot marked this pull request as ready for review June 4, 2021 10:13
@freelerobot freelerobot requested review from a team June 4, 2021 10:13
@freelerobot freelerobot added priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Jun 4, 2021
Copy link
Copy Markdown

@simonz130 simonz130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Nicole!

Comment thread src/entry.ts
Comment on lines +235 to +236
if (this.metadata.traceSampled === undefined && match[5])
this.metadata.traceSampled = match[5] === '1';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is much sense in having traceSampled value as true or false in this.metadata.traceSampled but having trace and spanId not set.
Probably we can reduce the "if" to just

if (match[5])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the users can still submit a header of ';o=0'. Which is a redundant but valid way to indicate both i) there's no trace/span and ii) don't sample this trace and span

Comment thread src/entry.ts
this.metadata.httpRequest = makeHttpRequestData(rawReq);
// Infer trace & span if not user specified already.
if ('headers' in rawReq && rawReq.headers['x-cloud-trace-context']) {
const regex = /([a-f\d]+)?(\/?([a-f\d]+))?(;?o=(\d))?/;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you refactor this to prepare for recognition of trace/span from traceparent (the recognition itself might happen in a different PR).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, ill do this in a following pr with a TODO note on recognizing trace-parent

@freelerobot freelerobot merged commit 19b943e into master Jun 7, 2021
@freelerobot freelerobot deleted the writeHttpRequest branch June 7, 2021 03:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: logging Issues related to the googleapis/nodejs-logging API. cla: yes This human has signed the Contributor License Agreement. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: detect trace & span fields

2 participants