opentelemetry-collector icon indicating copy to clipboard operation
opentelemetry-collector copied to clipboard

ctx propagation in otlp/http vs otlp/grpc

Open therealpandey opened this issue 1 year ago • 9 comments

Context I have written a custom auth extension which derives a key called consumer. I want to batch requests based on consumer by using the batch processor and use the same in a custom exporter. Issue 1: Batch processor uses the incoming metadata keys to batch. I can set the client.Metadata in my custom auth extension but the otlp/grpc receiver overrides client.Metadata. Issue 2: For the exporter, I tried setting client.Auth as well in my custom auth extension but the batch processor does not respect it. It creates a new context with only metadata, discarding the Auth (if set) by any authenticators.

Describe the bug I have created a custom server authenticator extension. At the end I am calling the following:

return client.NewContext(ctx, client.Info{
		Metadata: client.NewMetadata(map[string][]string{
		      MetadataKeyId:      {key},
	        }),
	}), nil

Basically, I am trying to set some information in the metadata.

Now in one of my subsequent processors, I am trying to access this key. It works fine for http requests but the key is not present for grpc requests.

Steps to reproduce

What did you expect to see? Context should be preserved for both otlp/http and otlp/grpc

What did you see instead?

What version did you use? v0.99.0

What config did you use?

extensions:
  customauthextension:
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
        include_metadata: true
        max_recv_msg_size_mib: 16
        auth:
          authenticator: customauthextension
      http:
        cors:
          allowed_origins:
            - '*'
        endpoint: 0.0.0.0:4318
        include_metadata: true
        auth:
          authenticator: customauthextension
exporters:
  logging:
processors:
  customprocessor:
service:
  extensions:
    - customauthextension
  pipelines:
    logs:
      receivers: [otlp]
      processors: [customprocessor, batch]
      exporters: [logging]

*** Additional Context *** I have been trying for a way to propagate context data from my custom authenticator to all processors and exporters for some time now. Here is a related PR regarding the same: https://github.com/open-telemetry/opentelemetry-collector/pull/10002

therealpandey avatar May 06 '24 21:05 therealpandey

Update: If I set a custom context key in my authenticator like :

return context.WithValue(ctx, mycustomKey, key), nil

and then access it with

ctx.Value(mycustomKey)

in my processor, it worksss!!

therealpandey avatar May 06 '24 21:05 therealpandey

I guess the root cause is that in grpc an interceptor to enrich the incoming context is called after the extension's auth method which is overwriting the Metadata set by the extension.

I need the batch processor to run after my customprocessor which requires a set of metadata keys to be set. However I am setting the metadata key in the auth extension which is being overidden in grpc requests.

therealpandey avatar May 07 '24 12:05 therealpandey

@grandwizard28 do you have link to where the override is happening?

TylerHelmuth avatar May 07 '24 19:05 TylerHelmuth

@TylerHelmuth Amend the following line located at https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configgrpc/configgrpc.go#L413:

- uInterceptors = append(uInterceptors, enhanceWithClientInformation(gss.IncludeMetadata))
+ uInterceptors = append(enhanceWithClientInformation(gss.IncludeMetadata), uInterceptors...)

This gives us control over playing with the Metadata in our interceptors.

therealpandey avatar May 07 '24 19:05 therealpandey

@TylerHelmuth Any thoughts?

therealpandey avatar May 12 '24 15:05 therealpandey

@grandwizard28 , are you able to create a new test case here, demonstrating the problem? By looking at the code, I'd say that there's no override of these values, but perhaps I'm missing some information.

https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configgrpc/configgrpc_test.go

jpkrohling avatar May 13 '24 11:05 jpkrohling

Sample test here https://github.com/open-telemetry/opentelemetry-collector/pull/10162

therealpandey avatar May 15 '24 21:05 therealpandey

I left a comment on that PR (and marked it as draft).

jpkrohling avatar May 16 '24 10:05 jpkrohling

Commented on the PR itself!

therealpandey avatar May 16 '24 11:05 therealpandey

@jpkrohling have commented the PR, can you please check?

therealpandey avatar May 26 '24 18:05 therealpandey

Based on the discussion in the linked PR, I'm closing this.

jpkrohling avatar Aug 19 '24 14:08 jpkrohling