-
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.
- Loading branch information
Showing
19 changed files
with
708 additions
and
71 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
core/metrics/src/main/scala-2/org/typelevel/otel4s/metrics/GaugeMacro.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,71 @@ | ||
/* | ||
* 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 | ||
package metrics | ||
|
||
import scala.collection.immutable | ||
|
||
private[otel4s] trait GaugeMacro[F[_], A] { | ||
def backend: Gauge.Backend[F, A] | ||
|
||
/** Sets the gauge value with a set of attributes. | ||
* | ||
* @param value | ||
* the value to set | ||
* | ||
* @param attributes | ||
* the set of attributes to associate with the value | ||
*/ | ||
def set(value: A, attributes: Attribute[_]*): F[Unit] = | ||
macro GaugeMacro.set[A] | ||
|
||
/** Sets the gauge value with a set of attributes. | ||
* | ||
* @param value | ||
* the value to set | ||
* | ||
* @param attributes | ||
* the set of attributes to associate with the value | ||
*/ | ||
def set(value: A, attributes: immutable.Iterable[Attribute[_]]): F[Unit] = | ||
macro GaugeMacro.setColl[A] | ||
|
||
} | ||
|
||
object GaugeMacro { | ||
import scala.reflect.macros.blackbox | ||
|
||
def set[A](c: blackbox.Context)( | ||
value: c.Expr[A], | ||
attributes: c.Expr[Attribute[_]]* | ||
): c.universe.Tree = { | ||
import c.universe._ | ||
setColl(c)(value, c.Expr(q"_root_.scala.Seq(..$attributes)")) | ||
} | ||
|
||
def setColl[A](c: blackbox.Context)( | ||
value: c.Expr[A], | ||
attributes: c.Expr[immutable.Iterable[Attribute[_]]] | ||
): c.universe.Tree = { | ||
import c.universe._ | ||
val backend = q"${c.prefix}.backend" | ||
val meta = q"$backend.meta" | ||
|
||
q"if ($meta.isEnabled) $backend.set($value, $attributes) else $meta.unit" | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
core/metrics/src/main/scala/org/typelevel/otel4s/metrics/Gauge.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,114 @@ | ||
/* | ||
* 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 | ||
package metrics | ||
|
||
import cats.Applicative | ||
import org.typelevel.otel4s.meta.InstrumentMeta | ||
|
||
import scala.collection.immutable | ||
|
||
/** A `Gauge` instrument that records non-additive values of type `A`. | ||
* | ||
* @see | ||
* See [[UpDownCounter]] to record additive values | ||
* | ||
* @see | ||
* [[https://opentelemetry.io/docs/specs/otel/metrics/api/#gauge]] | ||
* | ||
* @tparam F | ||
* the higher-kinded type of a polymorphic effect | ||
* | ||
* @tparam A | ||
* the type of the values to record. The type must have an instance of | ||
* [[MeasurementValue]]. [[scala.Long]] and [[scala.Double]] are supported | ||
* out of the box. | ||
*/ | ||
trait Gauge[F[_], A] extends GaugeMacro[F, A] | ||
|
||
object Gauge { | ||
|
||
/** A builder of [[Gauge]]. | ||
* | ||
* @tparam F | ||
* the higher-kinded type of a polymorphic effect | ||
* | ||
* @tparam A | ||
* the type of the values to record. The type must have an instance of | ||
* [[MeasurementValue]]. [[scala.Long]] and [[scala.Double]] are supported | ||
* out of the box. | ||
*/ | ||
trait Builder[F[_], A] { | ||
|
||
/** Sets the unit of measure for this counter. | ||
* | ||
* @see | ||
* [[https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit Instrument Unit]] | ||
* | ||
* @param unit | ||
* the measurement unit. Must be 63 or fewer ASCII characters. | ||
*/ | ||
def withUnit(unit: String): Builder[F, A] | ||
|
||
/** Sets the description for this gauge. | ||
* | ||
* @see | ||
* [[https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description Instrument Description]] | ||
* | ||
* @param description | ||
* the description | ||
*/ | ||
def withDescription(description: String): Builder[F, A] | ||
|
||
/** Creates a [[Gauge]] with the given `unit` and `description` (if any). | ||
*/ | ||
def create: F[Gauge[F, A]] | ||
} | ||
|
||
trait Backend[F[_], A] { | ||
def meta: InstrumentMeta[F] | ||
|
||
/** Sets the gauge value with a set of attributes. | ||
* | ||
* @param value | ||
* the value to set | ||
* | ||
* @param attributes | ||
* the set of attributes to associate with the value | ||
*/ | ||
def set(value: A, attributes: immutable.Iterable[Attribute[_]]): F[Unit] | ||
|
||
} | ||
|
||
def noop[F[_], A](implicit F: Applicative[F]): Gauge[F, A] = | ||
new Gauge[F, A] { | ||
val backend: Backend[F, A] = | ||
new Backend[F, A] { | ||
val meta: InstrumentMeta[F] = InstrumentMeta.disabled | ||
def set( | ||
value: A, | ||
attributes: immutable.Iterable[Attribute[_]] | ||
): F[Unit] = meta.unit | ||
} | ||
} | ||
|
||
private[otel4s] def fromBackend[F[_], A](b: Backend[F, A]): Gauge[F, A] = | ||
new Gauge[F, A] { | ||
def backend: Backend[F, A] = b | ||
} | ||
|
||
} |
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
Oops, something went wrong.