Skip to content

Commit

Permalink
Merge pull request #816 from iRevive/semconv/metrics-instrument-facto…
Browse files Browse the repository at this point in the history
…ry-type

semconv-metrics: do not force a type of instrument
  • Loading branch information
iRevive authored Oct 26, 2024
2 parents f57ac1d + 2cfb84c commit 7d84643
Show file tree
Hide file tree
Showing 18 changed files with 2,385 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,35 @@
{%- endmacro -%}

{%- macro instrumentType(type) -%}
{%- if type == "gauge" -%}Gauge[F, Long]
{%- elif type == "counter" -%}Counter[F, Long]
{%- elif type == "updowncounter" -%}UpDownCounter[F, Long]
{%- elif type == "histogram" -%}Histogram[F, Double]
{%- if type == "gauge" -%}Gauge[F, A]
{%- elif type == "counter" -%}Counter[F, A]
{%- elif type == "updowncounter" -%}UpDownCounter[F, A]
{%- elif type == "histogram" -%}Histogram[F, A]
{%- else %}{{ type }}
{%- endif -%}
{%- endmacro -%}

{%- macro instrumentFactory(type) -%}
{%- if type == "gauge" -%}gauge[Long]
{%- elif type == "counter" -%}counter[Long]
{%- elif type == "updowncounter" -%}upDownCounter[Long]
{%- elif type == "histogram" -%}histogram[Double]
{%- if type == "gauge" -%}gauge[A]
{%- elif type == "counter" -%}counter[A]
{%- elif type == "updowncounter" -%}upDownCounter[A]
{%- elif type == "histogram" -%}histogram[A]
{%- else %}{{ type }}
{%- endif -%}
{%- endmacro -%}

{%- macro observableInstrumentType(type) -%}
{%- if type == "gauge" -%}ObservableGauge
{%- elif type == "counter" -%}ObservableCounter
{%- elif type == "updowncounter" -%}ObservableUpDownCounter
{%- else %}{{ type }}
{%- endif -%}
{%- endmacro -%}

{%- macro observableInstrumentFactory(type) -%}
{%- if type == "gauge" -%}observableGauge[A]
{%- elif type == "counter" -%}observableCounter[A]
{%- elif type == "updowncounter" -%}observableUpDownCounter[A]
{%- else %}{{ type }}
{%- endif -%}
{%- endmacro -%}
Expand Down Expand Up @@ -95,17 +111,24 @@ package metrics
package metrics
{%- endif %}

import org.typelevel.otel4s.metrics._
{%- set required_imports = namespace(stable = false, experimental = false) -%}
{%- set required_imports = namespace(cats_effect = false, stable = false, experimental = false) -%}
{%- for metric in ctx.metrics -%}
{%- if metric.instrument != "histogram" -%}
{%- set required_imports.cats_effect = true -%}
{%- endif -%}
{% for attribute in metric.attributes %}
{%- if attribute is stable -%}
{%- set required_imports.stable = true -%}
{%- else -%}
{%- set required_imports.experimental = true -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endfor %}

{% if required_imports.cats_effect == true %}
import cats.effect.Resource
{%- endif %}
import org.typelevel.otel4s.metrics._
{%- if required_imports.stable == true %}
import org.typelevel.otel4s.semconv.attributes._
{%- endif %}
Expand Down Expand Up @@ -168,7 +191,7 @@ object {{ object_name }} {
)
}
{% endif %}
def create[F[_]: Meter]{% if metric.instrument == "histogram" %}(boundaries: BucketBoundaries){% endif %}: F[{{ instrumentType(metric.instrument) }}] =
def create[F[_]: Meter, A: MeasurementValue]{% if metric.instrument == "histogram" %}(boundaries: BucketBoundaries){% endif %}: F[{{ instrumentType(metric.instrument) }}] =
Meter[F]
.{{ instrumentFactory(metric.instrument) }}(name)
.withDescription(description)
Expand All @@ -179,6 +202,23 @@ object {{ object_name }} {
{% else %}
.create
{% endif %}
{% if metric.instrument != "histogram" %}
def createObserver[F[_]: Meter, A: MeasurementValue]: F[ObservableMeasurement[F, A]] =
Meter[F]
.{{ observableInstrumentFactory(metric.instrument) }}(name)
.withDescription(description)
.withUnit(unit)
.createObserver

def createWithCallback[F[_]: Meter, A: MeasurementValue](
callback: ObservableMeasurement[F, A] => F[Unit]
): Resource[F, {{ observableInstrumentType(metric.instrument) }}] =
Meter[F]
.{{ observableInstrumentFactory(metric.instrument) }}(name)
.withDescription(description)
.withUnit(unit)
.createWithCallback(callback)
{% endif %}
}
{% endfor %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package semconv
package experimental
package metrics

import cats.effect.Resource
import org.typelevel.otel4s.metrics._
import org.typelevel.otel4s.semconv.experimental.attributes._

Expand Down Expand Up @@ -71,13 +72,29 @@ object ContainerExperimentalMetrics {
)
}

def create[F[_]: Meter]: F[Counter[F, Long]] =
def create[F[_]: Meter, A: MeasurementValue]: F[Counter[F, A]] =
Meter[F]
.counter[Long](name)
.counter[A](name)
.withDescription(description)
.withUnit(unit)
.create

def createObserver[F[_]: Meter, A: MeasurementValue]: F[ObservableMeasurement[F, A]] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createObserver

def createWithCallback[F[_]: Meter, A: MeasurementValue](
callback: ObservableMeasurement[F, A] => F[Unit]
): Resource[F, ObservableCounter] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createWithCallback(callback)

}

/** Container's CPU usage, measured in cpus. Range from 0 to the number of allocatable CPUs <p>
Expand Down Expand Up @@ -118,13 +135,29 @@ object ContainerExperimentalMetrics {
)
}

def create[F[_]: Meter]: F[Gauge[F, Long]] =
def create[F[_]: Meter, A: MeasurementValue]: F[Gauge[F, A]] =
Meter[F]
.gauge[Long](name)
.gauge[A](name)
.withDescription(description)
.withUnit(unit)
.create

def createObserver[F[_]: Meter, A: MeasurementValue]: F[ObservableMeasurement[F, A]] =
Meter[F]
.observableGauge[A](name)
.withDescription(description)
.withUnit(unit)
.createObserver

def createWithCallback[F[_]: Meter, A: MeasurementValue](
callback: ObservableMeasurement[F, A] => F[Unit]
): Resource[F, ObservableGauge] =
Meter[F]
.observableGauge[A](name)
.withDescription(description)
.withUnit(unit)
.createWithCallback(callback)

}

/** Disk bytes for the container. <p>
Expand Down Expand Up @@ -172,13 +205,29 @@ object ContainerExperimentalMetrics {
)
}

def create[F[_]: Meter]: F[Counter[F, Long]] =
def create[F[_]: Meter, A: MeasurementValue]: F[Counter[F, A]] =
Meter[F]
.counter[Long](name)
.counter[A](name)
.withDescription(description)
.withUnit(unit)
.create

def createObserver[F[_]: Meter, A: MeasurementValue]: F[ObservableMeasurement[F, A]] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createObserver

def createWithCallback[F[_]: Meter, A: MeasurementValue](
callback: ObservableMeasurement[F, A] => F[Unit]
): Resource[F, ObservableCounter] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createWithCallback(callback)

}

/** Memory usage of the container. <p>
Expand All @@ -193,13 +242,29 @@ object ContainerExperimentalMetrics {
val stability: Stability = Stability.experimental
val attributeSpecs: List[AttributeSpec[_]] = Nil

def create[F[_]: Meter]: F[Counter[F, Long]] =
def create[F[_]: Meter, A: MeasurementValue]: F[Counter[F, A]] =
Meter[F]
.counter[Long](name)
.counter[A](name)
.withDescription(description)
.withUnit(unit)
.create

def createObserver[F[_]: Meter, A: MeasurementValue]: F[ObservableMeasurement[F, A]] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createObserver

def createWithCallback[F[_]: Meter, A: MeasurementValue](
callback: ObservableMeasurement[F, A] => F[Unit]
): Resource[F, ObservableCounter] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createWithCallback(callback)

}

/** Network bytes for the container. <p>
Expand Down Expand Up @@ -247,13 +312,29 @@ object ContainerExperimentalMetrics {
)
}

def create[F[_]: Meter]: F[Counter[F, Long]] =
def create[F[_]: Meter, A: MeasurementValue]: F[Counter[F, A]] =
Meter[F]
.counter[Long](name)
.counter[A](name)
.withDescription(description)
.withUnit(unit)
.create

def createObserver[F[_]: Meter, A: MeasurementValue]: F[ObservableMeasurement[F, A]] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createObserver

def createWithCallback[F[_]: Meter, A: MeasurementValue](
callback: ObservableMeasurement[F, A] => F[Unit]
): Resource[F, ObservableCounter] =
Meter[F]
.observableCounter[A](name)
.withDescription(description)
.withUnit(unit)
.createWithCallback(callback)

}

}
Loading

0 comments on commit 7d84643

Please sign in to comment.