-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KVL-874 Add unit tests for Telemetry (#9500)
* Add unit tests for Telemetry CHANGELOG_BEGIN CHANGELOG_END * Make Tracers injectable, improve the TelemetryContextSpec, add a metrics-test-lib package
- Loading branch information
Showing
11 changed files
with
815 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
ledger/metrics/src/test/lib/scala/com/daml/telemetry/TelemetrySpecBase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.telemetry | ||
|
||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider | ||
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
trait TelemetrySpecBase { | ||
|
||
protected val anInstrumentationName = "com.daml.telemetry.TelemetrySpec" | ||
protected val aSpanName = "aSpan" | ||
protected val anApplicationIdSpanAttribute: (SpanAttribute, String) = | ||
SpanAttribute.ApplicationId -> "anApplicationId" | ||
protected val aCommandIdSpanAttribute: (SpanAttribute, String) = | ||
SpanAttribute.CommandId -> "aCommandId" | ||
|
||
protected val spanExporter: InMemorySpanExporter = InMemorySpanExporter.create | ||
protected val tracerProvider: SdkTracerProvider = SdkTracerProvider | ||
.builder() | ||
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter)) | ||
.build() | ||
|
||
protected object TestTelemetry extends DefaultTelemetry(tracerProvider.get(anInstrumentationName)) | ||
|
||
protected implicit class RichInMemorySpanExporter(exporter: InMemorySpanExporter) { | ||
def finishedSpanAttributes: Map[SpanAttribute, String] = { | ||
val finishedSpans = exporter.getFinishedSpanItems.asScala | ||
finishedSpans.flatMap { span => | ||
val attributes = span.getAttributes.asMap.asScala | ||
attributes.map { case (key, value) => | ||
SpanAttribute(key.toString) -> value.toString | ||
} | ||
}.toMap | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions
62
ledger/metrics/src/test/suite/scala/com/daml/telemetry/TelemetryContextSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.telemetry | ||
|
||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.context.Context | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
import org.scalatest.{Assertion, BeforeAndAfterEach} | ||
|
||
/** Other cases are covered by [[TelemetrySpec]] */ | ||
class TelemetryContextSpec | ||
extends TelemetrySpecBase | ||
with AnyWordSpecLike | ||
with Matchers | ||
with BeforeAndAfterEach { | ||
|
||
override protected def afterEach(): Unit = spanExporter.reset() | ||
|
||
"DefaultTelemetryContext.runInOpenTelemetryScope" should { | ||
"run a body and create a current context with a span" in { | ||
val tracer = tracerProvider.get(anInstrumentationName) | ||
val span = tracer | ||
.spanBuilder(aSpanName) | ||
.setAttribute( | ||
anApplicationIdSpanAttribute._1.key, | ||
anApplicationIdSpanAttribute._2, | ||
) | ||
.startSpan() | ||
|
||
runInOpenTelemetryScopeAndAssert(DefaultTelemetryContext(tracer, span)) | ||
|
||
val attributes = spanExporter.finishedSpanAttributes | ||
attributes should contain(anApplicationIdSpanAttribute) | ||
} | ||
} | ||
|
||
"RootDefaultTelemetryContext.runInOpenTelemetryScope" should { | ||
"run a body" in { | ||
val tracer = tracerProvider.get(anInstrumentationName) | ||
runInOpenTelemetryScopeAndAssert(RootDefaultTelemetryContext(tracer)) | ||
} | ||
} | ||
|
||
"NoOpTelemetryContext.runInOpenTelemetryScope" should { | ||
"run a body" in { | ||
runInOpenTelemetryScopeAndAssert(NoOpTelemetryContext) | ||
} | ||
} | ||
|
||
private def runInOpenTelemetryScopeAndAssert(telemetryContext: TelemetryContext): Assertion = { | ||
var placeholder: Option[_] = None | ||
telemetryContext.runInOpenTelemetryScope { | ||
Span | ||
.fromContext(Context.current()) | ||
.end() // end the span from the current context to be able to make assertions on its attributes | ||
placeholder = Some(()) | ||
} | ||
placeholder shouldBe Some(()) | ||
} | ||
} |
Oops, something went wrong.