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

trace: add @span macro #1

Merged
merged 3 commits into from
Jul 9, 2024
Merged
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
54 changes: 51 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
os: [ubuntu-latest]
scala: [2.13, 3]
java: [temurin@8]
project: [rootJVM]
project: [rootJS, rootJVM, rootNative]
runs-on: ${{ matrix.os }}
timeout-minutes: 60
steps:
Expand Down Expand Up @@ -63,6 +63,14 @@ jobs:
if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-latest'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' 'scalafixAll --check'

- name: scalaJSLink
if: matrix.project == 'rootJS'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' Test/scalaJSLinkerResult

- name: nativeLink
if: matrix.project == 'rootNative'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' Test/nativeLink

- name: Test
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' test

Expand All @@ -76,11 +84,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 modules/metrics/jvm/target project/target
run: mkdir -p modules/trace/.jvm/target modules/trace/.js/target modules/metrics/jvm/target modules/trace/.native/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 modules/metrics/jvm/target project/target
run: tar cf targets.tar modules/trace/.jvm/target modules/trace/.js/target modules/metrics/jvm/target modules/trace/.native/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 Expand Up @@ -117,6 +125,16 @@ jobs:
if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false'
run: sbt +update

- name: Download target directories (2.13, rootJS)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootJS

- name: Inflate target directories (2.13, rootJS)
run: |
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.13, rootJVM)
uses: actions/download-artifact@v4
with:
Expand All @@ -127,6 +145,26 @@ jobs:
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.13, rootNative)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootNative

- name: Inflate target directories (2.13, rootNative)
run: |
tar xf targets.tar
rm targets.tar

- name: Download target directories (3, rootJS)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootJS

- name: Inflate target directories (3, rootJS)
run: |
tar xf targets.tar
rm targets.tar

- name: Download target directories (3, rootJVM)
uses: actions/download-artifact@v4
with:
Expand All @@ -137,6 +175,16 @@ jobs:
tar xf targets.tar
rm targets.tar

- name: Download target directories (3, rootNative)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootNative

- name: Inflate target directories (3, rootNative)
run: |
tar xf targets.tar
rm targets.tar

- name: Import signing key
if: env.PGP_SECRET != '' && env.PGP_PASSPHRASE == ''
env:
Expand Down
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Provide access to the unstable functionality without breaking the `otel4s`
* Some features may be upstreamed to the `otel4s` eventually

## Getting started
## Metrics - getting started

Add the `otel4s-experimental-metrics` dependency to the `build.sbt`:
```scala
Expand Down Expand Up @@ -42,5 +42,49 @@ object Main extends IOApp.Simple {

The metrics can be visualized in Grafana using this [dashboard][grafana-ce-dashboard].

## Trace - getting started

Add the `otel4s-experimental-trace` dependency to the `build.sbt`:
```scala
libraryDependencies ++= Seq(
"org.typelevel" %% "otel4s-experimental-trace" % "<version>"
)
```

### 1) `@span` annotation

The body of a method annotated with `@span` will be wrapped into a span:
```scala
import org.typelevel.otel4s.experimental.{attribute, trace}

@span
def findUser(
@attribute userId: Long,
@attribute("user.hash") hash: String
): F[User] = ???

// expands into

def findUser(
userId: Long,
hash: String
): F[User] =
Tracer[F].span(
"findUser",
Attribute("userId", userId), Attribute("user.hash", hash)
).surround(???)
```

The macro works with variables too:
```scala
@span("custom_name")
val findUser: IO[User] = ???

// expands into

val findUser: IO[User] =
Tracer[IO].span("custom_name").surround(???)
```

[otel4s]: https://github.com/typelevel/otel4s
[grafana-ce-dashboard]: https://grafana.com/grafana/dashboards/21487-cats-effect-runtime-metrics/
18 changes: 16 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ThisBuild / scalaVersion := Versions.Scala213 // the default Scala

lazy val root = tlCrossRootProject
.settings(name := "otel4s-experimental")
.aggregate(metrics)
.aggregate(metrics, trace)

lazy val metrics = crossProject(JVMPlatform)
.crossType(CrossType.Full)
Expand All @@ -39,12 +39,26 @@ lazy val metrics = crossProject(JVMPlatform)
name := "otel4s-experimental-metrics",
Test / fork := true,
libraryDependencies ++= Seq(
"org.typelevel" %%% "otel4s-core-metrics" % Versions.Otel4s,
"org.typelevel" %%% "otel4s-core-metrics" % Versions.Otel4s,
"org.typelevel" %%% "otel4s-sdk-metrics-testkit" % Versions.Otel4s % Test
)
)

lazy val trace = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("modules/trace"))
.settings(munitDependencies)
.settings(scalaReflectDependency)
.settings(
name := "otel4s-experimental-trace",
libraryDependencies ++= Seq(
"org.typelevel" %%% "otel4s-core-trace" % Versions.Otel4s
),
scalacOptions ++= {
if (tlIsScala3.value) Nil else Seq("-Ymacro-annotations")
}
)

lazy val scalaReflectDependency = Def.settings(
libraryDependencies ++= {
if (tlIsScala3.value) Nil
Expand Down
Loading