Skip to content

Commit

Permalink
Split project into modules (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive authored Sep 25, 2022
1 parent 341029f commit fbfe4f9
Show file tree
Hide file tree
Showing 55 changed files with 948 additions and 572 deletions.
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.14"
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-kernel" % CatsEffectVersion,
"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.14",
"org.scalameta" %%% "munit" % "0.7.29" % Test,
"org.typelevel" %%% "munit-cats-effect-3" % "1.0.7" % Test,
"org.typelevel" %%% "cats-effect-testkit" % "3.3.14" % 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.
*/
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

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 @@ -17,7 +17,7 @@
package org.typelevel.otel4s
package metrics

import cats.effect.Resource
import cats.effect.kernel.Resource

import scala.concurrent.duration.TimeUnit

Expand All @@ -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

0 comments on commit fbfe4f9

Please sign in to comment.