-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from iRevive/sdk-trace/testkit
Add `sdk-trace-testkit` module
- Loading branch information
Showing
7 changed files
with
174 additions
and
7 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
53 changes: 53 additions & 0 deletions
53
...-testkit/src/main/scala/org/typelevel/otel4s/sdk/testkit/trace/InMemorySpanExporter.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,53 @@ | ||
/* | ||
* Copyright 2024 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.sdk.testkit.trace | ||
|
||
import cats.Foldable | ||
import cats.Monad | ||
import cats.effect.Concurrent | ||
import cats.effect.std.Queue | ||
import cats.syntax.foldable._ | ||
import cats.syntax.functor._ | ||
import org.typelevel.otel4s.sdk.trace.data.SpanData | ||
import org.typelevel.otel4s.sdk.trace.exporter.SpanExporter | ||
|
||
final class InMemorySpanExporter[F[_]: Monad] private ( | ||
queue: Queue[F, SpanData] | ||
) extends SpanExporter[F] { | ||
val name: String = "InMemorySpanExporter" | ||
|
||
def exportSpans[G[_]: Foldable](spans: G[SpanData]): F[Unit] = | ||
spans.traverse_(span => queue.offer(span)) | ||
|
||
def flush: F[Unit] = | ||
Monad[F].unit | ||
|
||
def finishedSpans: F[List[SpanData]] = | ||
queue.tryTakeN(None) | ||
|
||
} | ||
|
||
object InMemorySpanExporter { | ||
|
||
def create[F[_]: Concurrent]( | ||
capacity: Option[Int] | ||
): F[InMemorySpanExporter[F]] = | ||
for { | ||
queue <- capacity.fold(Queue.unbounded[F, SpanData])(Queue.bounded(_)) | ||
} yield new InMemorySpanExporter[F](queue) | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
sdk/trace-testkit/src/main/scala/org/typelevel/otel4s/sdk/testkit/trace/TracesTestkit.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,86 @@ | ||
/* | ||
* Copyright 2024 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.sdk.testkit.trace | ||
|
||
import cats.FlatMap | ||
import cats.Parallel | ||
import cats.effect.Async | ||
import cats.effect.Resource | ||
import cats.effect.std.Console | ||
import cats.effect.std.Random | ||
import cats.syntax.flatMap._ | ||
import org.typelevel.otel4s.context.LocalProvider | ||
import org.typelevel.otel4s.sdk.context.Context | ||
import org.typelevel.otel4s.sdk.context.LocalContext | ||
import org.typelevel.otel4s.sdk.context.LocalContextProvider | ||
import org.typelevel.otel4s.sdk.trace.SdkTracerProvider | ||
import org.typelevel.otel4s.sdk.trace.data.SpanData | ||
import org.typelevel.otel4s.sdk.trace.processor.BatchSpanProcessor | ||
import org.typelevel.otel4s.sdk.trace.processor.SpanProcessor | ||
import org.typelevel.otel4s.trace.TracerProvider | ||
|
||
trait TracesTestkit[F[_]] { | ||
|
||
/** The [[org.typelevel.otel4s.trace.TracerProvider TracerProvider]]. | ||
*/ | ||
def tracerProvider: TracerProvider[F] | ||
|
||
/** The list of finished spans. | ||
* | ||
* @note | ||
* each invocation cleans up the internal buffer. | ||
*/ | ||
def finishedSpans: F[List[SpanData]] | ||
} | ||
|
||
object TracesTestkit { | ||
|
||
/** Creates [[TracesTestkit]] that keeps spans in-memory. | ||
* | ||
* @param customize | ||
* the customization of the builder | ||
*/ | ||
def inMemory[F[_]: Async: Parallel: Console: LocalContextProvider]( | ||
customize: SdkTracerProvider.Builder[F] => SdkTracerProvider.Builder[F] = | ||
(b: SdkTracerProvider.Builder[F]) => b | ||
): Resource[F, TracesTestkit[F]] = { | ||
def createTracerProvider( | ||
processor: SpanProcessor[F] | ||
)(implicit local: LocalContext[F]): F[TracerProvider[F]] = | ||
Random.scalaUtilRandom[F].flatMap { implicit random => | ||
val builder = SdkTracerProvider.builder[F].addSpanProcessor(processor) | ||
customize(builder).build | ||
} | ||
|
||
for { | ||
local <- Resource.eval(LocalProvider[F, Context].local) | ||
exporter <- Resource.eval(InMemorySpanExporter.create[F](None)) | ||
processor <- BatchSpanProcessor.builder(exporter).build | ||
tracerProvider <- Resource.eval(createTracerProvider(processor)(local)) | ||
} yield new Impl(tracerProvider, processor, exporter) | ||
} | ||
|
||
private final class Impl[F[_]: FlatMap]( | ||
val tracerProvider: TracerProvider[F], | ||
processor: SpanProcessor[F], | ||
exporter: InMemorySpanExporter[F] | ||
) extends TracesTestkit[F] { | ||
def finishedSpans: F[List[SpanData]] = | ||
processor.forceFlush >> exporter.finishedSpans | ||
} | ||
|
||
} |
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