-
Notifications
You must be signed in to change notification settings - Fork 37
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
Immutable injection in TextMapPropagator #182
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s | ||
|
||
trait TextMapInjector[A] { | ||
type Builder | ||
|
||
def textMapSetter: TextMapSetter[Builder] | ||
|
||
def toBuilder(carrier: A): Builder | ||
|
||
def toCarrier(builder: Builder): A | ||
} | ||
rossabaker marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,16 @@ private[java] class TextMapPropagatorImpl[F[_]: Sync]( | |
Sync[F].delay( | ||
jPropagator.inject(toJContext(ctx), carrier, fromTextMapSetter) | ||
) | ||
|
||
def injected[A](ctx: Vault, carrier: A)(implicit | ||
injector: TextMapInjector[A] | ||
): A = { | ||
val builder = injector.toBuilder(carrier) | ||
jPropagator.inject( | ||
toJContext(ctx), | ||
builder, | ||
fromTextMapSetter(injector.textMapSetter) | ||
) | ||
injector.toCarrier(builder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good: the mutation is hidden from the outside. Bad: allocates a builder and carrier even if no fields are injected. Can we ask Awkward: this implementation needs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could always check if the vault is empty first. idk if that covers all cases, but it could optimise some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know whether every injector is guaranteed not to inject anything if there's nothing to inject... seems reasonable, but is the absence of an attribute ever serialized somehow? 🤔 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will scaladoc this if people like the approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the approach ;P