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

Immutable injection in TextMapPropagator #182

Merged
merged 4 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] {
Copy link
Member Author

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.

Copy link
Contributor

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

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
Expand Up @@ -53,8 +53,8 @@ trait TextMapPropagator[F[_]] {
*/
def extract[A: TextMapGetter](ctx: Vault, carrier: A): Vault

/** Injects data from the context into the given `carrier` for downstream
* consumers, for example as HTTP headers.
/** Injects data from the context into the given mutable `carrier` for
* downstream consumers, for example as HTTP headers.
*
* @param ctx
* the context containing the value to be injected
Expand All @@ -63,9 +63,29 @@ trait TextMapPropagator[F[_]] {
* holds propagation fields
*
* @tparam A
* the type of the carrier
* the type of the carrier, which is mutable
*/
def inject[A: TextMapSetter](ctx: Vault, carrier: A): F[Unit]

/** Injects data from the context into a copy of the given immutable `carrier`
* for downstream consumers, for example as HTTP headers.
*
* This method is an extension to the OpenTelemetry specification to support
* immutable carrier types.
*
* @param ctx
* the context containing the value to be injected
*
* @param carrier
* holds propagation fields
*
* @tparam A
* the type of the carrier
*
* @return
* a copy of the carrier, with new fields injected
*/
def injected[A: TextMapInjector](ctx: Vault, carrier: A): A
}

object TextMapPropagator {
Expand All @@ -79,5 +99,8 @@ object TextMapPropagator {

def inject[A: TextMapSetter](ctx: Vault, carrier: A): F[Unit] =
Applicative[F].unit

def injected[A: TextMapInjector](ctx: Vault, carrier: A): A =
carrier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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 jPropagator whether it's actually going to inject anything? We could do a decorator and lazily create the builder.

Awkward: this implementation needs Sync just for the inject, which I think would be less used in Scala than this variant!

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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? 🤔

}
}