Skip to content
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

Add client and server tracing interceptors #1756

Merged
merged 14 commits into from
Jan 18, 2024
Merged

Conversation

gjcairo
Copy link
Collaborator

@gjcairo gjcairo commented Jan 10, 2024

Motivation

With v2 of gRPC, we want to provide tracing interceptors for both the client and server sides.

Modifications

This change adds tracing interceptors implementations for both the client and the server, using swift-distributed-tracing.

Result

We've got tracing interceptors.

@gjcairo gjcairo marked this pull request as ready for review January 10, 2024 16:59
Package.swift Show resolved Hide resolved
Package.swift Show resolved Hide resolved
Sources/GRPCInterceptors/ClientTracingInterceptor.swift Outdated Show resolved Hide resolved
Sources/GRPCInterceptors/ClientTracingInterceptor.swift Outdated Show resolved Hide resolved
Sources/GRPCInterceptors/ClientTracingInterceptor.swift Outdated Show resolved Hide resolved
Tests/GRPCCoreTests/Call/TracingInterceptorTests.swift Outdated Show resolved Hide resolved
Tests/GRPCCoreTests/Call/TracingTestsUtils.swift Outdated Show resolved Hide resolved

class TracingTestCase: XCTestCase {
override class func setUp() {
InstrumentationSystem.bootstrap(TestTracer())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this blow up like logging/metrics?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You mean if it fails when called more than once? If so, yes, and that's why I had to do it here, so that it's called just once.

tracer.inject(
serviceContext,
into: &request,
using: ClientRequestInjector()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Two questions here:

  1. Should we create the ClientRequestInjector only once in the init of this struct?
  2. Is there a case where the user might want to provide their own injector here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. 👍 - had to change the Carrier type to be Metadata to avoid generics hell.
  2. I don't think so, really - these injectors are provided as part of the basic interceptor we present here. If users need more control I think they could just create their own interceptors.

Sources/GRPCInterceptors/ServerTracingInterceptor.swift Outdated Show resolved Hide resolved
return try await tracer.withSpan(context.descriptor.fullyQualifiedMethod, context: serviceContext, ofKind: .client) { span in
span.addEvent("Sending request")
let response = try await next(request, context)
span.addEvent("Received response")
Copy link
Collaborator

Choose a reason for hiding this comment

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

So this is interesting because we might be streaming here. We should definitely rename this to something like Received response end or similar. Next, should we provide a mode of this interceptor that emits events on every single message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed about changing the event name. With regards to emitting events on each message - I agree it could be useful. I think the only way of doing this is by creating a new RPCWriter wrapping the passed one, so I'll take this approach for both client and server interceptors.

Sources/GRPCInterceptors/ServerTracingInterceptor.swift Outdated Show resolved Hide resolved
using: ClientRequestInjector()
)

return try await tracer.withSpan(context.descriptor.fullyQualifiedMethod, context: serviceContext, ofKind: .client) { span in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are there other attributes that we might want to add to the span from the request?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can't think of anything else that would be universal to all requests

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.

LGTM! One missing final nit

Tests/GRPCCoreTests/Call/TracingTestsUtils.swift Outdated Show resolved Hide resolved
Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

This looks in pretty good shape, I mostly left nits but there are a couple of things that I think do need fixing before we can merge this.

Sources/GRPCInterceptors/ClientTracingInterceptor.swift Outdated Show resolved Hide resolved
Sources/GRPCInterceptors/HookedWriter.swift Outdated Show resolved Hide resolved
Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift Outdated Show resolved Hide resolved
Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift Outdated Show resolved Hide resolved
Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift Outdated Show resolved Hide resolved
Package.swift Show resolved Hide resolved
Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

Looks good! Left one small question.

@glbrntt glbrntt added the version/v2 Relates to v2 label Jan 18, 2024
@gjcairo gjcairo merged commit 378b6e8 into grpc:main Jan 18, 2024
13 of 14 checks passed
@gjcairo gjcairo deleted the gc-tracing branch January 18, 2024 13:24
glbrntt pushed a commit to glbrntt/grpc-swift that referenced this pull request Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
version/v2 Relates to v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants