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 2 commits
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
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: TextMapUpdater](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: TextMapUpdater](ctx: Vault, carrier: A): A =
carrier
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 TextMapUpdater[A] {
def updated(carrier: A, key: String, value: String): A
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@ private[java] class TextMapPropagatorImpl[F[_]: Sync](
Sync[F].delay(
jPropagator.inject(toJContext(ctx), carrier, fromTextMapSetter)
)

def injected[A](ctx: Vault, carrier: A)(implicit
injector: TextMapUpdater[A]
): A = {
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
var injectedCarrier = carrier
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
jPropagator.inject[Null](
toJContext(ctx),
null, // explicitly allowed per opentelemetry-java, so our setter can be a lambda!
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
(_, key, value) => {
injectedCarrier = injector.updated(injectedCarrier, key, value)
}
)
injectedCarrier
}
}