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

SDK metrics: Prometheus exporter #751

Open
iRevive opened this issue Sep 1, 2024 · 6 comments
Open

SDK metrics: Prometheus exporter #751

iRevive opened this issue Sep 1, 2024 · 6 comments
Assignees
Labels
help wanted Extra attention is needed metrics Improvement to metrics module sdk exporter module Features and improvements to the sdk exporter module

Comments

@iRevive
Copy link
Contributor

iRevive commented Sep 1, 2024

Category Link
OpenTelemetry spec https://opentelemetry.io/docs/specs/otel/metrics/sdk_exporters/prometheus
OpenTelemetry Java implementation https://github.com/open-telemetry/opentelemetry-java/tree/main/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus
Prometheus formats https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md
Prometheus text writer https://github.com/prometheus/client_java/blob/main/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java
Prometheus scrape handler https://github.com/prometheus/client_java/blob/main/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

Prometheus exporter works as an HTTP server that exposes metrics in Prometheus-compatible format.

A prototype: iRevive#5.

A few things to consider:

  1. Auto-configuration mode - SDK must launch an HTTP server
  2. A user should be able to use exporter routes with its own HTTP server (see example)
  3. Should we make a separate module? For example, otel4s-sdk-exporter-prometheus?
  4. According to the spec, we need to support only the text writer
  5. Since we need to support all platforms (JVM, Scala.js, Scala Native) we will need to partially reimplement Prometheus Writer and Scrape Handler

Examples

Auto-configuration

Env configuration:

export OTEL_METRICS_EXPORTER=prometheus
export OTEL_EXPORTER_PROMETHEUS_HOST=localhost
export OTEL_EXPORTER_PROMETHEUS_PORT=9464

Code:

OpenTelemetrySdk.autoConfigured[IO](
  _.addMetricExporterConfigurer(PrometheusMetricExporterAutoConfigure[IO])
).use { otel =>
  // emulate metrics
  for {
    meter <- otel.sdk.meterProvider.get("meter")
    counter <- meter.counter[Long]("counter").create
    _ <- counter.add(1L).delayBy(1.second).foreverM
  } yield ()
}

SDK will launch a dedicated HTTP server at localhost:9464.

Manual configuration

PrometheusMetricExporter.builder[IO].build.flatMap { exporter =>
  OpenTelemetrySdk.autoConfigured[IO](
    _.addMeterProviderCustomizer((b, _) => b.registerMetricReader(exporter.metricReader))
  ).use { autoConfigured =>
    val appRoutes: HttpRoutes[IO] = ???
    val routes = appRoutes <+> PrometheusHttpRoutes.routes[IO]
    
    EmberServerBuilder.default[IO].withHttpApp(routes.orNotFound).build.useForever
  }
}

Instead of launching a dedicated HTTP server, a user can merge exporter routes with the app routes.

@iRevive iRevive added help wanted Extra attention is needed metrics Improvement to metrics module sdk exporter module Features and improvements to the sdk exporter module labels Sep 1, 2024
@iRevive iRevive changed the title SDK metrics: Prometheus exporter MetricsSDK: Prometheus exporter Sep 1, 2024
@iRevive iRevive changed the title MetricsSDK: Prometheus exporter SDK metrics: Prometheus exporter Sep 2, 2024
@bio-aeon
Copy link
Contributor

bio-aeon commented Sep 4, 2024

I would take it if no one minds.

@alexcardell
Copy link

Does this cover pushing to a push gateway as well (e.g. for serverless)? Or is that separate

@bio-aeon
Copy link
Contributor

From my point of view, this issue covers exactly prometheus exporter part from OpenTelemetry spec. And according to spec, prometheus exporter is a pull metric exporter which responds to HTTP requests. That is, this issue doesn't imply the push model.
@iRevive maybe you can comment if there is anything planned outside of spec in the future.

@alexcardell
Copy link

Ah yes, it has a MUST not

Maybe I can add a contrib module after the basic prometheus is in? We have otel collectors at $work but only configured for traces and not metrics, we do have a push gateway though

@bio-aeon
Copy link
Contributor

bio-aeon commented Sep 26, 2024

I think it makes sense to create a separate issue where this proposal can be discussed :)
But by the way you can use prometheus as opentelemetry backend by enabling OTLP receiver, without pushgateway.

@iRevive
Copy link
Contributor Author

iRevive commented Sep 27, 2024

I would be careful keeping non-spec functionality in the main repository. Eventually, the maintenance burden could catch us up.

If it's really needed, you can implement it here: https://github.com/typelevel/otel4s-experimental.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed metrics Improvement to metrics module sdk exporter module Features and improvements to the sdk exporter module
Projects
None yet
Development

No branches or pull requests

3 participants