Skip to content

Split project into modules #39

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

Merged
merged 4 commits into from
Sep 25, 2022
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p target .js/target site/target core/.js/target core/.jvm/target .jvm/target .native/target java/.jvm/target testkit/jvm/target project/target
run: mkdir -p testkit/metrics/jvm/target java/metrics/target testkit/common/jvm/target target core/common/.jvm/target .js/target site/target core/metrics/.jvm/target core/all/.js/target java/all/target java/common/target core/metrics/.js/target core/all/.jvm/target .jvm/target .native/target core/common/.js/target testkit/all/jvm/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar target .js/target site/target core/.js/target core/.jvm/target .jvm/target .native/target java/.jvm/target testkit/jvm/target project/target
run: tar cf targets.tar testkit/metrics/jvm/target java/metrics/target testkit/common/jvm/target target core/common/.jvm/target .js/target site/target core/metrics/.jvm/target core/all/.js/target java/all/target java/common/target core/metrics/.js/target core/all/.jvm/target .jvm/target .native/target core/common/.js/target testkit/all/jvm/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
130 changes: 100 additions & 30 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,127 @@ val Scala213 = "2.13.8"
ThisBuild / crossScalaVersions := Seq(Scala213, "3.1.3")
ThisBuild / scalaVersion := Scala213 // the default Scala

val CatsVersion = "2.8.0"
val CatsEffectVersion = "3.3.13"
val MUnitVersion = "0.7.29"
val MUnitCatsEffectVersion = "1.0.7"
val OpenTelemetryVersion = "1.15.0"

lazy val scalaReflectDependency = Def.settings(
libraryDependencies ++= {
if (tlIsScala3.value) Nil
else Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided)
}
)

lazy val root = tlCrossRootProject
.aggregate(core, testkit, java)
.aggregate(
`core-common`,
`core-metrics`,
core,
`testkit-common`,
`testkit-metrics`,
testkit,
`java-common`,
`java-metrics`,
java
)
.settings(name := "otel4s")

lazy val `core-common` = crossProject(JVMPlatform, JSPlatform)
.crossType(CrossType.Pure)
.in(file("core/common"))
.settings(
name := "otel4s-core-common",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % CatsVersion,
"org.scalameta" %%% "munit" % MUnitVersion % Test
)
)

lazy val `core-metrics` = crossProject(JVMPlatform, JSPlatform)
.crossType(CrossType.Pure)
.in(file("core/metrics"))
.dependsOn(`core-common`)
.settings(scalaReflectDependency)
.settings(
name := "otel4s-core-metrics",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % CatsEffectVersion,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need full IO here, should it be ce-kernel or ce-std? Does it matter (it won't if we add fs2 deps). Do we care?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Actually, cats-effect-kernel is enough.

I was thinking about fs2 dependency in tracing module recently. Unless we want to introduce some deep tracing for stream scopes (I'm not even sure how plausible it is) the scodec is more than enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played with extending stream tracing to Natchez, and concluded that having a way to trace Resource was good enough for streams. All a stream dependency brings is a convenience method around Stream.resource(whateverCreatesTheSpan).

Copy link
Contributor Author

@iRevive iRevive Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, once a streaming scope is described as a Resource, the propagation works well with streams.

def flow(tracer: Tracer[IO]): Stream[IO, Unit] =
for {
span <- Stream.resource(tracer.span("span"))
_ <- Stream.eval(
tracer.currentSpanContext.assertEquals(Some(span.context))
)
span2 <- Stream.resource(tracer.span("span-2"))
_ <- Stream.eval(
tracer.currentSpanContext.assertEquals(Some(span2.context))
)
span3 <- Stream.resource(
tracer.spanBuilder("span-3").withParent(span.context).createAuto
)
_ <- Stream.eval(
tracer.currentSpanContext.assertEquals(Some(span3.context))
)
} yield ()

"org.scalameta" %%% "munit" % MUnitVersion % Test,
"org.typelevel" %%% "munit-cats-effect-3" % MUnitCatsEffectVersion % Test,
"org.typelevel" %%% "cats-effect-testkit" % CatsEffectVersion % Test
)
)

lazy val core = crossProject(JVMPlatform, JSPlatform)
.crossType(CrossType.Pure)
.in(file("core"))
.in(file("core/all"))
.dependsOn(`core-common`, `core-metrics`)
.settings(
name := "otel4s-core"
)

lazy val `testkit-common` = crossProject(JVMPlatform)
.crossType(CrossType.Full)
.in(file("testkit/common"))
.dependsOn(`core-common`)
.settings(
name := "otel4s-core",
name := "otel4s-testkit-common"
)

lazy val `testkit-metrics` = crossProject(JVMPlatform)
.crossType(CrossType.Full)
.in(file("testkit/metrics"))
.dependsOn(`testkit-common`, `core-metrics`)
.settings(
name := "otel4s-testkit-metrics"
)
.jvmSettings(
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % "2.8.0",
"org.typelevel" %%% "cats-effect" % "3.3.13",
"org.scalameta" %%% "munit" % "0.7.29" % Test,
"org.typelevel" %%% "munit-cats-effect-3" % "1.0.7" % Test,
"org.typelevel" %%% "cats-effect-testkit" % "3.3.13" % Test
),
libraryDependencies ++= {
if (tlIsScala3.value) Nil
else
Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided)
}
"io.opentelemetry" % "opentelemetry-api" % OpenTelemetryVersion,
"io.opentelemetry" % "opentelemetry-sdk" % OpenTelemetryVersion,
"io.opentelemetry" % "opentelemetry-sdk-testing" % OpenTelemetryVersion
)
)

lazy val testkit = crossProject(JVMPlatform)
.crossType(CrossType.Full)
.in(file("testkit"))
.in(file("testkit/all"))
.dependsOn(`testkit-common`, `testkit-metrics`)
.settings(
name := "otel4s-testkit"
)
.jvmSettings(

lazy val `java-common` = project
.in(file("java/common"))
.dependsOn(`core-common`.jvm, `testkit-common`.jvm)
.settings(
name := "otel4s-java-common",
libraryDependencies ++= Seq(
"io.opentelemetry" % "opentelemetry-api" % "1.15.0",
"io.opentelemetry" % "opentelemetry-sdk" % "1.15.0",
"io.opentelemetry" % "opentelemetry-sdk-testing" % "1.15.0"
"io.opentelemetry" % "opentelemetry-api" % OpenTelemetryVersion
)
)
.dependsOn(core)

lazy val java = crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.in(file("java"))
lazy val `java-metrics` = project
.in(file("java/metrics"))
.dependsOn(`java-common`, `core-metrics`.jvm, `testkit-metrics`.jvm)
.settings(
name := "otel4s-java",
name := "otel4s-java-metrics",
libraryDependencies ++= Seq(
"io.opentelemetry" % "opentelemetry-api" % "1.15.0",
"io.opentelemetry" % "opentelemetry-sdk" % "1.15.0" % Test,
"io.opentelemetry" % "opentelemetry-sdk-testing" % "1.15.0" % Test,
"org.scalameta" %% "munit" % "0.7.29" % Test,
"org.typelevel" %% "munit-cats-effect-3" % "1.0.7" % Test
"io.opentelemetry" % "opentelemetry-api" % OpenTelemetryVersion,
"io.opentelemetry" % "opentelemetry-sdk" % OpenTelemetryVersion % Test,
"io.opentelemetry" % "opentelemetry-sdk-testing" % OpenTelemetryVersion % Test,
"org.scalameta" %% "munit" % MUnitVersion % Test,
"org.typelevel" %% "munit-cats-effect-3" % MUnitCatsEffectVersion % Test
)
)
.dependsOn(core, testkit)

lazy val java = project
.in(file("java/all"))
.dependsOn(core.jvm, `java-metrics`)
.settings(
name := "otel4s-java"
)

lazy val docs = project.in(file("site")).enablePlugins(TypelevelSitePlugin)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.typelevel.otel4s.metrics.MeterProvider

trait Otel4s[F[_]] {

/** A registry for creating named [[org.typelevel.otel4s.metrics.Meter]].
/** A registry for creating named meters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use a pipe operator if you want it to say meters but link to Meter:

[[org.typelevel.otel4s.metrics.Meter|meters]]

That should not slow this down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc command was failing. I do not remember the exact reason, but I will bring back the link if it's possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iRevive you should just use a space there, not a |.

*/
def meterProvider: MeterProvider[F]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.meta
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the module name be meta maybe? Typically the module name and package name are somehow matching.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is this file misplaced? Or maybe I'm very confused by the source reorganizaiton 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, ignore me 😂


import cats.Applicative

trait InstrumentMeta[F[_]] {

/** Indicates whether instrumentation is enabled or not.
*/
def isEnabled: Boolean

/** A no-op effect.
*/
def unit: F[Unit]

}

object InstrumentMeta {

def enabled[F[_]: Applicative]: InstrumentMeta[F] =
make(enabled = true)

def disabled[F[_]: Applicative]: InstrumentMeta[F] =
make(enabled = false)

private def make[F[_]: Applicative](enabled: Boolean): InstrumentMeta[F] =
new InstrumentMeta[F] {
val isEnabled: Boolean = enabled
val unit: F[Unit] = Applicative[F].unit
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@
* limitations under the License.
*/

package org.typelevel.otel4s.metrics
package org.typelevel.otel4s.meta

import munit.FunSuite

class InstrumentMetaSuite extends FunSuite {

test("enabled") {
val disabled = InstrumentMeta.enabled[cats.Id]

assertEquals(disabled.unit, ())
assertEquals(disabled.isEnabled, true)
}

test("disabled") {
val disabled = InstrumentMeta.disabled[cats.Id]

assertEquals(disabled.unit, ())
assertEquals(disabled.isEnabled, false)
}

/** A backend that implements instrument operations: add, record, etc.
*/
trait InstrumentBackend[F[_]] {
def isEnabled: Boolean
def unit: F[Unit]
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,40 @@ private[otel4s] trait CounterMacro[F[_], A] {
* the set of attributes to associate with the value
*/
def add(value: A, attributes: Attribute[_]*): F[Unit] =
macro MetricsMacro.add[A]
macro CounterMacro.add[A]

/** Increments a counter by one.
*
* @param attributes
* the set of attributes to associate with the value
*/
def inc(attributes: Attribute[_]*): F[Unit] =
macro MetricsMacro.inc
macro CounterMacro.inc

}

object CounterMacro {
import scala.reflect.macros.blackbox

def add[A](c: blackbox.Context)(
value: c.Expr[A],
attributes: c.Expr[Attribute[_]]*
): c.universe.Tree = {
import c.universe._
val backend = q"${c.prefix}.backend"
val meta = q"$backend.meta"

q"if ($meta.isEnabled) $backend.add($value, ..$attributes) else $meta.unit"
}

def inc(c: blackbox.Context)(
attributes: c.Expr[Attribute[_]]*
): c.universe.Tree = {
import c.universe._
val backend = q"${c.prefix}.backend"
val meta = q"$backend.meta"

q"if ($meta.isEnabled) $backend.inc(..$attributes) else $meta.unit"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private[otel4s] trait HistogramMacro[F[_], A] {
* the set of attributes to associate with the value
*/
def record(value: A, attributes: Attribute[_]*): F[Unit] =
macro MetricsMacro.record[A]
macro HistogramMacro.record[A]

/** Records duration of the given effect.
*
Expand All @@ -58,6 +58,33 @@ private[otel4s] trait HistogramMacro[F[_], A] {
timeUnit: TimeUnit,
attributes: Attribute[_]*
): Resource[F, Unit] =
macro MetricsMacro.recordDuration
macro HistogramMacro.recordDuration

}

object HistogramMacro {
import scala.reflect.macros.blackbox

def record[A](c: blackbox.Context)(
value: c.Expr[A],
attributes: c.Expr[Attribute[_]]*
): c.universe.Tree = {
import c.universe._
val backend = q"${c.prefix}.backend"
val meta = q"$backend.meta"

q"if ($meta.isEnabled) $backend.record($value, ..$attributes) else $meta.unit"
}

def recordDuration(c: blackbox.Context)(
timeUnit: c.Expr[TimeUnit],
attributes: c.Expr[Attribute[_]]*
): c.universe.Tree = {
import c.universe._
val backend = q"${c.prefix}.backend"
val meta = q"$backend.meta"

q"if ($meta.isEnabled) $backend.recordDuration($timeUnit, ..$attributes) else $meta.resourceUnit"
}

}
Loading