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

Add core-trace-testkit, sdk-trace-testkit, and oteljava-trace-testkit modules #474

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 55 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ lazy val root = tlCrossRootProject
`core-common`,
`core-metrics`,
`core-trace`,
`core-trace-testkit`,
core,
`sdk-common`,
`sdk-trace`,
`sdk-trace-testkit`,
sdk,
`sdk-exporter-common`,
`sdk-exporter-proto`,
Expand All @@ -92,6 +94,7 @@ lazy val root = tlCrossRootProject
`oteljava-common`,
`oteljava-metrics`,
`oteljava-trace`,
`oteljava-trace-testkit`,
oteljava,
semconv,
benchmarks,
Expand All @@ -103,6 +106,10 @@ lazy val root = tlCrossRootProject
)
.settings(name := "otel4s")

//
// Core
//

lazy val `core-common` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("core/common"))
Expand Down Expand Up @@ -155,6 +162,17 @@ lazy val `core-trace` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
)
.settings(scalafixSettings)

lazy val `core-trace-testkit` =
crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("core/trace-testkit"))
.dependsOn(`core-trace`)
.settings(
name := "otel4s-core-trace-testkit",
startYear := Some(2024)
)
.settings(scalafixSettings)

lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("core/all"))
Expand All @@ -164,6 +182,10 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
)
.settings(scalafixSettings)

//
// SDK
//

lazy val `sdk-common` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.enablePlugins(BuildInfoPlugin)
Expand Down Expand Up @@ -194,7 +216,8 @@ lazy val `sdk-trace` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.in(file("sdk/trace"))
.dependsOn(
`sdk-common` % "compile->compile;test->test",
`core-trace` % "compile->compile;test->test"
`core-trace` % "compile->compile;test->test",
`core-trace-testkit` % Test
)
.settings(
name := "otel4s-sdk-trace",
Expand All @@ -209,6 +232,17 @@ lazy val `sdk-trace` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
)
.settings(munitDependencies)

lazy val `sdk-trace-testkit` =
crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.enablePlugins(NoPublishPlugin)
.in(file("sdk/trace-testkit"))
.dependsOn(`sdk-trace`, `core-trace-testkit`)
.settings(
name := "otel4s-sdk-trace-testkit",
startYear := Some(2024)
)

lazy val sdk = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.enablePlugins(NoPublishPlugin)
Expand Down Expand Up @@ -388,7 +422,11 @@ lazy val `oteljava-metrics` = project

lazy val `oteljava-trace` = project
.in(file("oteljava/trace"))
.dependsOn(`oteljava-common`, `core-trace`.jvm)
.dependsOn(
`oteljava-common`,
`core-trace`.jvm,
`core-trace-testkit`.jvm % Test
)
.settings(munitDependencies)
.settings(
name := "otel4s-oteljava-trace",
Expand All @@ -401,6 +439,18 @@ lazy val `oteljava-trace` = project
)
.settings(scalafixSettings)

lazy val `oteljava-trace-testkit` = project
.in(file("oteljava/trace-testkit"))
.dependsOn(`oteljava-trace`, `core-trace-testkit`.jvm)
.settings(
name := "otel4s-oteljava-trace-testkit",
libraryDependencies ++= Seq(
"io.opentelemetry" % "opentelemetry-sdk-testing" % OpenTelemetryVersion
),
startYear := Some(2024)
)
.settings(scalafixSettings)

lazy val oteljava = project
.in(file("oteljava/all"))
.dependsOn(core.jvm, `oteljava-metrics`, `oteljava-trace`)
Expand Down Expand Up @@ -528,9 +578,11 @@ lazy val unidocs = project
`core-common`.jvm,
`core-metrics`.jvm,
`core-trace`.jvm,
`core-trace-testkit`.jvm,
core.jvm,
`sdk-common`.jvm,
`sdk-trace`.jvm,
`sdk-trace-testkit`.jvm,
sdk.jvm,
`sdk-exporter-common`.jvm,
`sdk-exporter-trace`.jvm,
Expand All @@ -541,6 +593,7 @@ lazy val unidocs = project
`oteljava-common`,
`oteljava-metrics`,
`oteljava-trace`,
`oteljava-trace-testkit`,
oteljava,
semconv.jvm
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* 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.testkit.trace

import cats.Functor
import cats.Hash
import cats.Show
import cats.syntax.functor._
import cats.syntax.hash._
import cats.syntax.show._

/** A tree representation of a span.
*/
sealed trait SpanTree[A] {

/** The current span.
*/
def current: A

/** Children of the current span.
*/
def children: List[SpanTree[A]]

}

object SpanTree {

def apply[A](span: A): SpanTree[A] =
Impl(span, Nil)

def apply[A](span: A, children: Iterable[SpanTree[A]]): SpanTree[A] =
Impl(span, children.toList)

@annotation.implicitNotFound("""
Could not find the `SpanLike` for `${A}`. The `SpanLike` is available out of the box for the following models:

1) SDK SpanData, via `otel4s-sdk-tracing-testkit` module:

import org.typelevel.otel4s.sdk.trace.data.SpanData
import org.typelevel.otel4s.sdk.testkit.trace.instances.spanlike._

val spanLike: SpanLike[SpanData] = implicitly

2) Open Telemetry Java SpanData, via `otel4s-oteljava-tracing-testkit` module:

import io.opentelemetry.sdk.trace.data.SpanData
import org.typelevel.otel4s.oteljava.testkit.trace.instances.spanlike._

val spanLike: SpanLike[SpanData] = implicitly
""")
trait SpanLike[A] {
def spanIdHex(a: A): String
def parentSpanIdHex(a: A): Option[String]
}

object SpanLike {
def apply[A](implicit ev: SpanLike[A]): SpanLike[A] = ev

def make[A](
getSpanIdHex: A => String,
getParentSpanIdHex: A => Option[String]
): SpanLike[A] =
new SpanLike[A] {
def spanIdHex(a: A): String = getSpanIdHex(a)
def parentSpanIdHex(a: A): Option[String] = getParentSpanIdHex(a)
}
}

/** Transforms the given spans into the tree-like structure.
*/
def of[A: SpanLike](spans: Iterable[A]): List[SpanTree[A]] = {
val byParent = spans.toList.groupBy(s => SpanLike[A].parentSpanIdHex(s))
val topNodes = byParent.getOrElse(None, Nil)
val bottomToTop = sortNodesByDepth(0, topNodes, byParent, Nil)
val maxDepth = bottomToTop.headOption.map(_.depth).getOrElse(0)
buildFromBottom(maxDepth, bottomToTop, byParent, Map.empty)
}

implicit def spanTreeHash[S: Hash]: Hash[SpanTree[S]] =
Hash.by(p => (p.current, p.children.map(_.hash)))

implicit def spanTreeShow[S: Show]: Show[SpanTree[S]] =
Show.show(p => show"SpanTree{span=${p.current}, children=${p.children}}")

implicit val spanTreeFunctor: Functor[SpanTree] =
new Functor[SpanTree] {
def map[A, B](fa: SpanTree[A])(f: A => B): SpanTree[B] =
SpanTree(f(fa.current), fa.children.map(_.fmap(f)))
}

private case class EntryWithDepth[A](data: A, depth: Int)

@annotation.tailrec
private def sortNodesByDepth[A: SpanLike](
depth: Int,
nodesInDepth: List[A],
nodesByParent: Map[Option[String], List[A]],
acc: List[EntryWithDepth[A]]
): List[EntryWithDepth[A]] = {
val withDepth = nodesInDepth.map(n => EntryWithDepth(n, depth))
val calculated = withDepth ++ acc

val children = nodesInDepth.flatMap { n =>
nodesByParent.getOrElse(Some(SpanLike[A].spanIdHex(n)), Nil)
}

children match {
case Nil =>
calculated

case _ =>
sortNodesByDepth(depth + 1, children, nodesByParent, calculated)
}
}

@annotation.tailrec
private def buildFromBottom[A: SpanLike](
depth: Int,
remaining: List[EntryWithDepth[A]],
nodesByParent: Map[Option[String], List[A]],
processedNodesById: Map[String, SpanTree[A]]
): List[SpanTree[A]] = {
val (nodesOnCurrentDepth, rest) = remaining.span(_.depth == depth)
val newProcessedNodes = nodesOnCurrentDepth.map { n =>
val nodeId = SpanLike[A].spanIdHex(n.data)
val children = nodesByParent
.getOrElse(Some(nodeId), Nil)
.flatMap(c => processedNodesById.get(SpanLike[A].spanIdHex(c)))
val leaf = SpanTree(n.data, children)
nodeId -> leaf
}.toMap

if (depth > 0) {
buildFromBottom(
depth - 1,
rest,
nodesByParent,
processedNodesById ++ newProcessedNodes
)
} else {
// top nodes
newProcessedNodes.values.toList
}
}

private final case class Impl[A](
current: A,
children: List[SpanTree[A]]
) extends SpanTree[A]

}
Loading
Loading