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

docs(api): clarify TextMapPropagator API requirements #5370

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chancancode
Copy link
Contributor

@chancancode chancancode commented Jan 24, 2025

Non-breaking parts from #5368

Which problem is this PR solving?

The current interface places the generic paramter on the interface itself. This implies that the implementors of TextMapPropagator can specify what carrier types it accepts (and that each implementor only work with one specific type of carrier).

interface TextMapPropagator<Carrier> {
  inject(context: Context, carrier: Carrier, setter: TextMapSetter<Carrier>): void;
  extract(context: Context, carrier: Carrier, getter: TextMapGetter<Carrier>): void;
}

In reality, this is not the case.

The propagator API is designed to be called by participating code around the various transport layers (such as the fetch inst on the browser, or integration with the HTTP server library on the backend), and it is these callers that ultimately controls what carrier type the currently configured propagator is called with.

Therefore, a correct implementation of this interface must treat the carrier as an opaque value, and only work with it using the provided getter/setter.

Ideally, the interface should look like this instead:

interface TextMapPropagator {
  inject<Carrier>(context: Context, carrier: Carrier, setter: TextMapSetter<Carrier>): void;
  extract<Carrier>(context: Context, carrier: Carrier, getter: TextMapGetter<Carrier>): void;
}

This communicates and enforces the contract. Unfortunately, that would be a breking change we are not currently prepared to make (see #5368).

Short description of the changes

Instead, this commit updates the documentation to explicitly document the discrapancy and advice implemntors the correct way forward.

It also updates our own implementations to follow the recommended pattern, as well as updating the tests to be more well-behaved around this, as some of them are written to rely on this exact behavior that would be problematic in the real world.

Ref #5365
Ref #5368

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added updated
  • Documentation has been updated

The current interface places the generic paramter on the interface
itself. This implies that the implementors of `TextMapPropagator`
can specify what carrier types it accepts (and that each implementor
only work with one specific type of carrier).

```ts
interface TextMapPropagator<Carrier> {
  inject(context: Context, carrier: Carrier, setter: TextMapSetter<Carrier>): void;
  extract(context: Context, carrier: Carrier, getter: TextMapGetter<Carrier>): void;
}
```

In reality, this is not the case.

The propagator API is designed to be called by participating code
around the various transport layers (such as the `fetch` inst on
the browser, or integration with the HTTP server library on the
backend), and it is these callers that ultimately controls what
carrier type the currently configured propagator is called with.

Therefore, a correct implementation of this interface must treat
the carrier as an opaque value, and only work with it using the
provided getter/setter.

Ideally, the interface should look like this instead:

```ts
interface TextMapPropagator {
  inject<Carrier>(context: Context, carrier: Carrier, setter: TextMapSetter<Carrier>): void;
  extract<Carrier>(context: Context, carrier: Carrier, getter: TextMapGetter<Carrier>): void;
}
```

This communicates and enforces the contract. Unfortunately, that
would be a breking change we are not currently prepared to make.

Instead, this commit updates the documentation to explicitly
document the discrapancy and advice implemntors the correct way
forward.

It also updates our own implementations to follow the recommended
pattern, as well as updating the tests to be more well-behaved
around this, as some of them are written to rely on this exact
behavior that would be problematic in the real world.

Ref open-telemetry#5365
Ref open-telemetry#5368
@chancancode chancancode force-pushed the fix-propagator-generic-2 branch from 3e66c66 to 82d8f73 Compare January 24, 2025 19:08
Copy link

codecov bot commented Jan 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.62%. Comparing base (f927e82) to head (82d8f73).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5370      +/-   ##
==========================================
+ Coverage   94.54%   94.62%   +0.07%     
==========================================
  Files         318      318              
  Lines        8052     8052              
  Branches     1694     1694              
==========================================
+ Hits         7613     7619       +6     
+ Misses        439      433       -6     
Files with missing lines Coverage Δ
api/src/propagation/TextMapPropagator.ts 57.14% <ø> (+42.85%) ⬆️
...re/src/baggage/propagation/W3CBaggagePropagator.ts 97.22% <100.00%> (ø)
...es/opentelemetry-core/src/propagation/composite.ts 100.00% <100.00%> (ø)
...emetry-core/src/trace/W3CTraceContextPropagator.ts 100.00% <100.00%> (ø)
...entelemetry-propagator-b3/src/B3MultiPropagator.ts 100.00% <100.00%> (ø)
...es/opentelemetry-propagator-b3/src/B3Propagator.ts 100.00% <100.00%> (ø)
...ntelemetry-propagator-b3/src/B3SinglePropagator.ts 100.00% <100.00%> (ø)
...elemetry-propagator-jaeger/src/JaegerPropagator.ts 100.00% <100.00%> (ø)

@chancancode chancancode mentioned this pull request Jan 24, 2025
25 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant