Skip to content

Allowing to configure the component name for the http4s client metrics #57

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -28,26 +28,34 @@ import org.http4s.client.Client

object KamonSupport {

private var _instrumentation = instrumentation(Kamon.config())
private val defaultComponentName = "http4s.client"
private var _instrumentation =
instrumentation(Kamon.config(), defaultComponentName)

private def instrumentation(
kamonConfig: Config
kamonConfig: Config,
componentName: String = "http4s.client"
): HttpClientInstrumentation = {
val httpClientConfig =
kamonConfig.getConfig("kamon.instrumentation.http4s.client")
HttpClientInstrumentation.from(httpClientConfig, "http4s.client")
HttpClientInstrumentation.from(httpClientConfig, componentName)
}

Kamon.onReconfigure(newConfig =>
_instrumentation = instrumentation(newConfig)
)

def apply[F[_]](underlying: Client[F])(implicit F: Sync[F]): Client[F] =
this(underlying, defaultComponentName)

def apply[F[_]](underlying: Client[F], componentName: String)(implicit
F: Sync[F]
): Client[F] = {
Kamon.onReconfigure(newConfig =>
_instrumentation = instrumentation(newConfig, componentName)
)
Client { request =>
// this needs to run on the same thread as the caller, so can't be suspended in F
val ctx = Kamon.currentContext()
kamonClient(underlying)(request)(ctx)(_instrumentation)
}
}

private def kamonClient[F[_]](
underlying: Client[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class ClientInstrumentationSpec
}

val client: Client[IO] =
KamonSupport[IO](Client.fromHttpApp[IO](service.orNotFound))
KamonSupport[IO](
Client.fromHttpApp[IO](service.orNotFound),
"my-service-name"
)

"The Client instrumentation" should {
"propagate the current context and generate a span inside an action and complete the ws request" in {
Expand All @@ -66,7 +69,7 @@ class ClientInstrumentationSpec

span.operationName shouldBe "/tracing/ok"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.metricTags.get(plainLong("http.status_code")) shouldBe 200
span.metricTags.get(
Expand Down Expand Up @@ -99,7 +102,7 @@ class ClientInstrumentationSpec
val span = testSpanReporter().nextSpan().value
span.operationName shouldBe "/tracing/ok"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.hasError shouldBe true

Expand All @@ -122,7 +125,7 @@ class ClientInstrumentationSpec
val span = testSpanReporter().nextSpan().value
span.operationName shouldBe "/tracing/not-found"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.metricTags.get(plainLong("http.status_code")) shouldBe 404
span.metricTags.get(
Expand All @@ -149,7 +152,7 @@ class ClientInstrumentationSpec

span.operationName shouldBe "/tracing/error"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.hasError shouldBe true
span.metricTags.get(plainLong("http.status_code")) shouldBe 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,34 @@ import org.http4s.client.Client

object KamonSupport {

private var _instrumentation = instrumentation(Kamon.config())
private val defaultComponentName = "http4s.client"
private var _instrumentation =
instrumentation(Kamon.config(), defaultComponentName)

private def instrumentation(
kamonConfig: Config
kamonConfig: Config,
componentName: String = "http4s.client"
): HttpClientInstrumentation = {
val httpClientConfig =
kamonConfig.getConfig("kamon.instrumentation.http4s.client")
HttpClientInstrumentation.from(httpClientConfig, "http4s.client")
HttpClientInstrumentation.from(httpClientConfig, componentName)
}

Kamon.onReconfigure(newConfig =>
_instrumentation = instrumentation(newConfig)
)

def apply[F[_]](underlying: Client[F])(implicit F: Sync[F]): Client[F] =
this(underlying, defaultComponentName)

def apply[F[_]](underlying: Client[F], componentName: String)(implicit
F: Sync[F]
): Client[F] = {
Kamon.onReconfigure(newConfig =>
_instrumentation = instrumentation(newConfig, componentName)
)
Client { request =>
// this needs to run on the same thread as the caller, so can't be suspended in F
val ctx = Kamon.currentContext()
kamonClient(underlying)(request)(ctx)(_instrumentation)
}
}

private def kamonClient[F[_]](
underlying: Client[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class ClientInstrumentationSpec
}

val client: Client[IO] =
KamonSupport[IO](Client.fromHttpApp[IO](service.orNotFound))
KamonSupport[IO](
Client.fromHttpApp[IO](service.orNotFound),
"my-service-name"
)

"The Client instrumentation" should {
"propagate the current context and generate a span inside an action and complete the ws request" in {
Expand All @@ -67,7 +70,7 @@ class ClientInstrumentationSpec

span.operationName shouldBe "/tracing/ok"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.metricTags.get(plainLong("http.status_code")) shouldBe 200
span.metricTags.get(
Expand Down Expand Up @@ -100,7 +103,7 @@ class ClientInstrumentationSpec
val span = testSpanReporter().nextSpan().value
span.operationName shouldBe "/tracing/ok"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.hasError shouldBe true

Expand All @@ -123,7 +126,7 @@ class ClientInstrumentationSpec
val span = testSpanReporter().nextSpan().value
span.operationName shouldBe "/tracing/not-found"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.metricTags.get(plainLong("http.status_code")) shouldBe 404
span.metricTags.get(
Expand All @@ -150,7 +153,7 @@ class ClientInstrumentationSpec

span.operationName shouldBe "/tracing/error"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.hasError shouldBe true
span.metricTags.get(plainLong("http.status_code")) shouldBe 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,34 @@ import org.http4s.client.Client

object KamonSupport {

private var _instrumentation = instrumentation(Kamon.config())
private val defaultComponentName = "http4s.client"
private var _instrumentation =
instrumentation(Kamon.config(), defaultComponentName)

private def instrumentation(
kamonConfig: Config
kamonConfig: Config,
componentName: String = "http4s.client"
): HttpClientInstrumentation = {
val httpClientConfig =
kamonConfig.getConfig("kamon.instrumentation.http4s.client")
HttpClientInstrumentation.from(httpClientConfig, "http4s.client")
HttpClientInstrumentation.from(httpClientConfig, componentName)
}

Kamon.onReconfigure(newConfig =>
_instrumentation = instrumentation(newConfig)
)

def apply[F[_]](underlying: Client[F])(implicit F: Sync[F]): Client[F] =
this(underlying, defaultComponentName)

def apply[F[_]](underlying: Client[F], componentName: String)(implicit
F: Sync[F]
): Client[F] = {
Kamon.onReconfigure(newConfig =>
_instrumentation = instrumentation(newConfig, componentName)
)
Client { request =>
// this needs to run on the same thread as the caller, so can't be suspended in F
val ctx = Kamon.currentContext()
kamonClient(underlying)(request)(ctx)(_instrumentation)
}
}

private def kamonClient[F[_]](
underlying: Client[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class ClientInstrumentationSpec
}

val client: Client[IO] =
KamonSupport[IO](Client.fromHttpApp[IO](service.orNotFound))
KamonSupport[IO](
Client.fromHttpApp[IO](service.orNotFound),
"my-service-name"
)

"The Client instrumentation" should {
"propagate the current context and generate a span inside an action and complete the ws request" in {
Expand All @@ -67,7 +70,7 @@ class ClientInstrumentationSpec

span.operationName shouldBe "/tracing/ok"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.metricTags.get(plainLong("http.status_code")) shouldBe 200
span.metricTags.get(
Expand Down Expand Up @@ -100,7 +103,7 @@ class ClientInstrumentationSpec
val span = testSpanReporter().nextSpan().value
span.operationName shouldBe "/tracing/ok"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.hasError shouldBe true

Expand All @@ -123,7 +126,7 @@ class ClientInstrumentationSpec
val span = testSpanReporter().nextSpan().value
span.operationName shouldBe "/tracing/not-found"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.metricTags.get(plainLong("http.status_code")) shouldBe 404
span.metricTags.get(
Expand All @@ -150,7 +153,7 @@ class ClientInstrumentationSpec

span.operationName shouldBe "/tracing/error"
span.kind shouldBe Span.Kind.Client
span.metricTags.get(plain("component")) shouldBe "http4s.client"
span.metricTags.get(plain("component")) shouldBe "my-service-name"
span.metricTags.get(plain("http.method")) shouldBe "GET"
span.hasError shouldBe true
span.metricTags.get(plainLong("http.status_code")) shouldBe 500
Expand Down