Skip to content

Auto extract IDs from headers on the server #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

glbrntt
Copy link
Collaborator

@glbrntt glbrntt commented Oct 21, 2022

Motivation:

It's common for service authors to extract a tracing ID from a request header and attach that value to the logger for an RPC. This only requires a few lines of code but must be done in each RPC. gRPC should provide configuration to do this automatically.

Modifications:

  • Add server configuration which extracts a value from headers and attaches it to a logger at the start of each RPC.
  • Wire up the config. Add tests.

Result:

Trace IDs can be automatically extracted from headers on the server and attached to loggers.

@glbrntt glbrntt added the 🆕 semver/minor Adds new public API. label Oct 21, 2022
@@ -451,6 +454,22 @@ extension Server {
}
}

extension Server.Configuration {
/// Configuration for extracting IDs from metadata and inserting them into a logger.
public struct TraceIDExtractor {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this sufficiently flexible? Do we need to allow the user to define a type that can do arbitrary searches?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question. I suspect not, I can imagine a situation where we could fallback to a different header field for example.

Copy link

Choose a reason for hiding this comment

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

Whoa wait a moment guys -- this is reinventing tracing infrastructure and types. Such extract/inject things are exactly what a Tracer (or other Instrument (a Tracer is-a Instrument)) would be doing. So rather, grpc should instrument itself using the tracing instruments, and make use of those to populate baggage, which then is fed into logging.

Here's the basic API: https://github.com/apple/swift-distributed-tracing/blob/main/Sources/Instrumentation/Instrument.swift

We can set aside a moment to talk this through this week if you want @glbrntt? It'd be really valuable to use the tracing types here; The only holdoff from 1.0 them all is projects like this having a serious look at it, as well as the swift-log discussion we're having right now about propagation, so it'd be really a waste to invent your own "extractor" types right now.

Motivation:

It's common for service authors to extract a tracing ID from a request
header and attach that value to the logger for an RPC. This only
requires a few lines of code but must be done in each RPC. gRPC should
provide configuration to do this automatically.

Modifications:

- Add server configuration which extracts a value from headers and
  attaches it to a logger at the start of each RPC.
- Wire up the config. Add tests.

Result:

Trace IDs can be automatically extracted from headers on the server and
attached to loggers.
@glbrntt glbrntt force-pushed the gb-uuid-extraction branch from 5cf4d9c to c0209df Compare October 21, 2022 14:11
@glbrntt glbrntt requested a review from Lukasa October 24, 2022 10:25
/// - Parameters:
/// - loggerKey: The key in the logger to set the trace ID on.
/// - extractor: A function to extract a trace ID from
public init(loggerKey: String, extractor: @escaping (HPACKHeaders) -> String?) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also @Sendable, right?

Copy link
Collaborator

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

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

In general I love this! I have been doing the exact same thing but needed to extract in each individual method which was kinda annoying.

I am just wondering if we should extend this a bit further to allow more things to get extracted to metadata. Something like this (HPACKHeaders) -> [String] or even (HPACKHeaders, Logger) -> Logger?

My thinking behind this is that there might be more that needs to get logged than just a trace id. Sometimes you want to log the parent id, span id, trace flags etc. https://www.w3.org/TR/trace-context/#traceparent-header

Maybe @ktoso could chime in here as well.

@ktoso
Copy link

ktoso commented Oct 24, 2022

Chimed in in-line - this is exactly what swift-distributed-tracing types are for, and yeah it can be very generic -- that's what the Baggage type is for after all.

I am just wondering if we should extend this a bit further to allow more things to get extracted to metadata. Something like this (HPACKHeaders) -> [String] or even (HPACKHeaders, Logger) -> Logger?

My thinking behind this is that there might be more that needs to get logged than just a trace id. Sometimes you want to log the parent id, span id, trace flags etc.

Precisely. And you can never know the actual set of things, which is why it must be just a blank "instrument" that people can fill in with logic -- and you can provide a simple one if you wanted to :)

Shall we catch up tomorrow a bit? I'll schedule something.

@ktoso
Copy link

ktoso commented Oct 25, 2022

Cool, so I spent an hour or so and here's the same functionality implemented with the tracing APIs (as PR to this PR branch): glbrntt#2

Let's figure out how to quickly stabilize them so we don't have to do the ad-hoc implementation in here 👍

We also have the 2 year old PoC over here which even did spans: #941 so between those two branches I'm sure we can find a way to land the proper solution nicely – and get the tracing 1.0 out there ASAP so this lib can depend on it 👍

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 1, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@ktoso
Copy link

ktoso commented Nov 1, 2022

CLA signed and all other approvals done. Let's get collaboratin~ 🥳

// url: "https://github.com/apple/swift-log.git",
// from: "1.4.4"
url: "https://github.com/slashmo/swift-log.git",
branch: "feature/baggage"
Copy link

Choose a reason for hiding this comment

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

This is now merged and available in 1.5.x

@glbrntt
Copy link
Collaborator Author

glbrntt commented Jan 15, 2024

We won't adopt tracing out-of-the-box in v1, instead this will come in v2 via interceptors.

@glbrntt glbrntt closed this Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants