Skip to content

Commit

Permalink
Merge pull request #838 from lenguyenthanh/prometheus-exporter/2
Browse files Browse the repository at this point in the history
prometheus exporter: add shutdown timeout config
  • Loading branch information
iRevive authored Nov 15, 2024
2 parents ae32def + ceb560b commit 286cb01
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
14 changes: 14 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.typesafe.tools.mima.core._

ThisBuild / tlBaseVersion := "0.11"

ThisBuild / organization := "org.typelevel"
Expand Down Expand Up @@ -446,6 +448,18 @@ lazy val `sdk-exporter-prometheus` =
startYear := Some(2024),
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-ember-server" % Http4sVersion
),
mimaBinaryIssueFilters ++= Seq(
// see #838
ProblemFilters.exclude[DirectMissingMethodProblem](
"org.typelevel.otel4s.sdk.exporter.prometheus.PrometheusMetricExporter#HttpServerBuilderImpl.*"
),
ProblemFilters.exclude[IncompatibleResultTypeProblem](
"org.typelevel.otel4s.sdk.exporter.prometheus.PrometheusMetricExporter#HttpServerBuilderImpl.*"
),
ProblemFilters.exclude[ReversedMissingMethodProblem](
"org.typelevel.otel4s.sdk.exporter.prometheus.PrometheusMetricExporter#HttpServerBuilder.withShutdownTimeout"
)
)
)
.jsSettings(scalaJSLinkerSettings)
Expand Down
3 changes: 2 additions & 1 deletion docs/sdk/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Target-specific properties are prioritized. E.g. `otel.exporter.otlp.metrics.end

### Prometheus exporter

The exporter launches an HTTP server which responds to the HTTP requests with Prometheus metrics in the appropriate format.
The exporter launches an HTTP server which responds to the HTTP requests with Prometheus metrics in the appropriate format.

| System property | Environment variable | Description |
|----------------------------------------------|-------------------------------------------------------|------------------------------------------------------------------------------------------|
Expand All @@ -116,6 +116,7 @@ The exporter launches an HTTP server which responds to the HTTP requests with Pr
| otel.exporter.prometheus.without.type.suffix | OTEL\\_EXPORTER\\_PROMETHEUS\_WITHOUT\\_TYPE\\_SUFFIX | If metrics are produced without a type suffix. Default is `false`. |
| otel.exporter.prometheus.without.scope.info | OTEL\\_EXPORTER\\_PROMETHEUS\_WITHOUT\\_SCOPE\\_INFO | If metrics are produced without a scope info metric or scope labels. Default is `false`. |
| otel.exporter.prometheus.without.target.info | OTEL\\_EXPORTER\\_PROMETHEUS\_WITHOUT\\_TARGET\\_INFO | If metrics are produced without a target info metric. Default is `false`. |
| otel.exporter.prometheus.shutdown.timeout | OTEL\\_EXPORTER\\_PROMETHEUS\_SHUTDOWN\\_TIMEOUT | The time to wait for provider to do any cleanup required. Default is `10 seconds`. |

### Period metric reader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import org.http4s.server.Router
import org.typelevel.otel4s.sdk.metrics.data.MetricData
import org.typelevel.otel4s.sdk.metrics.exporter._

import scala.concurrent.duration.FiniteDuration

/** Exports metrics on request (pull based).
*
* @see
Expand Down Expand Up @@ -97,6 +99,7 @@ object PrometheusMetricExporter {
private[prometheus] object Defaults {
val Host: Host = host"localhost"
val Port: Port = port"9464"
val ShutdownTimeout: FiniteDuration = FiniteDuration(10, "seconds")
}

/** Builder for [[PrometheusMetricExporter]] */
Expand All @@ -121,6 +124,9 @@ object PrometheusMetricExporter {
/** Sets the port that metrics are served on. */
def withPort(port: Port): HttpServerBuilder[F]

/** Sets the shutdown timeout of the HTTP server. */
def withShutdownTimeout(shutdownTimeout: FiniteDuration): HttpServerBuilder[F]

/** Sets the default aggregation selector to use. */
def withDefaultAggregationSelector(
selector: AggregationSelector
Expand Down Expand Up @@ -150,6 +156,7 @@ object PrometheusMetricExporter {
new HttpServerBuilderImpl[F](
host = Defaults.Host,
port = Defaults.Port,
shutdownTimeout = Defaults.ShutdownTimeout,
defaultAggregationSelector = AggregationSelector.default,
writerConfig = PrometheusWriter.Config.default
)
Expand All @@ -174,6 +181,7 @@ object PrometheusMetricExporter {
private final case class HttpServerBuilderImpl[F[_]: Async: Network: Compression: Console](
host: Host,
port: Port,
shutdownTimeout: FiniteDuration,
defaultAggregationSelector: AggregationSelector,
writerConfig: PrometheusWriter.Config
) extends HttpServerBuilder[F] {
Expand All @@ -183,6 +191,9 @@ object PrometheusMetricExporter {
def withPort(port: Port): HttpServerBuilder[F] =
copy(port = port)

def withShutdownTimeout(shutdowntimeout: FiniteDuration): HttpServerBuilder[F] =
copy(shutdownTimeout = shutdownTimeout)

def withDefaultAggregationSelector(
selector: AggregationSelector
): HttpServerBuilder[F] = copy(defaultAggregationSelector = selector)
Expand All @@ -204,6 +215,7 @@ object PrometheusMetricExporter {
.default[F]
.withHost(host)
.withPort(port)
.withShutdownTimeout(shutdownTimeout)
.withHttpApp(Router("metrics" -> routes).orNotFound)
.build
.evalTap { _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.typelevel.otel4s.sdk.autoconfigure.ConfigurationError
import org.typelevel.otel4s.sdk.metrics.exporter.AggregationSelector
import org.typelevel.otel4s.sdk.metrics.exporter.MetricExporter

import scala.concurrent.duration.FiniteDuration
import scala.util.chaining._

/** Autoconfigures Prometheus [[MetricExporter]].
Expand All @@ -45,6 +46,7 @@ import scala.util.chaining._
* | otel.exporter.prometheus.without.type.suffix | OTEL_EXPORTER_PROMETHEUS_WITHOUT_TYPE_SUFFIX | If metrics are produced without a type suffix. Default is `false`. |
* | otel.exporter.prometheus.without.scope.info | OTEL_EXPORTER_PROMETHEUS_WITHOUT_SCOPE_INFO | If metrics are produced without a scope info metric or scope labels. Default is `false`. |
* | otel.exporter.prometheus.without.target.info | OTEL_EXPORTER_PROMETHEUS_WITHOUT_TARGET_INFO | If metrics are produced without a target info metric. Default is `false`. |
* | otel.exporter.prometheus.shutdown.timeout | OTEL_EXPORTER_PROMETHEUS_SHUTDOWN_TIMEOUT | The time to wait for provider to do any cleanup required. Default is `10 seconds`. |
* }}}
*
* @see
Expand Down Expand Up @@ -85,10 +87,14 @@ private final class PrometheusMetricExporterAutoConfigure[
withoutTargetInfo <- F
.fromEither(config.getOrElse(ConfigKeys.WithoutTargetInfo, defaultConfig.targetInfoDisabled))
.toResource
shutdownTimeout <- F
.fromEither(config.getOrElse(ConfigKeys.ShutdownTimeout, Defaults.ShutdownTimeout))
.toResource
exporter <- PrometheusMetricExporter
.serverBuilder[F]
.withHost(host)
.withPort(port)
.withShutdownTimeout(shutdownTimeout)
.withDefaultAggregationSelector(defaultAggregation)
.withWriterConfig(mkWriterConfig(withoutUnits, withoutTypeSuffixes, withoutScopeInfo, withoutTargetInfo))
.build
Expand Down Expand Up @@ -153,8 +159,20 @@ object PrometheusMetricExporterAutoConfigure {
val WithoutTargetInfo: Config.Key[Boolean] =
Config.Key("otel.exporter.prometheus.without.target.info")

val ShutdownTimeout: Config.Key[FiniteDuration] =
Config.Key("otel.exporter.prometheus.shutdown.timeout")

val All: Set[Config.Key[_]] =
Set(Host, Port, DefaultAggregation, WithoutUnits, WithoutTypeSuffixes, WithoutScopeInfo, WithoutTargetInfo)
Set(
Host,
Port,
DefaultAggregation,
WithoutUnits,
WithoutTypeSuffixes,
WithoutScopeInfo,
WithoutTargetInfo,
ShutdownTimeout
)
}

/** Returns [[org.typelevel.otel4s.sdk.autoconfigure.AutoConfigure.Named]] that configures Prometheus
Expand All @@ -171,6 +189,7 @@ object PrometheusMetricExporterAutoConfigure {
* | otel.exporter.prometheus.without.type.suffix | OTEL_EXPORTER_PROMETHEUS_WITHOUT_TYPE_SUFFIX | If metrics are produced without a type suffix. Default is `false`. |
* | otel.exporter.prometheus.without.scope.info | OTEL_EXPORTER_PROMETHEUS_WITHOUT_SCOPE_INFO | If metrics are produced without a scope info metric or scope labels. Default is `false`. |
* | otel.exporter.prometheus.without.target.info | OTEL_EXPORTER_PROMETHEUS_WITHOUT_TARGET_INFO | If metrics are produced without a target info metric. Default is `false`. |
* | otel.exporter.prometheus.shutdown.timeout | OTEL_EXPORTER_PROMETHEUS_SHUTDOWN_TIMEOUT | The time to wait for provider to do any cleanup required. Default is `10 seconds`. |
* }}}
*
* @see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class PrometheusMetricExporterAutoConfigureSuite extends CatsEffectSuite with Su
Map(
"otel.exporter.prometheus.host" -> "",
"otel.exporter.prometheus.port" -> "",
"otel.exporter.prometheus.shutdown.timeout" -> "",
"otel.exporter.prometheus.default.aggregation" -> "",
"otel.exporter.prometheus.without.units" -> "",
"otel.exporter.prometheus.without.type.suffix" -> "",
Expand Down Expand Up @@ -98,6 +99,7 @@ class PrometheusMetricExporterAutoConfigureSuite extends CatsEffectSuite with Su
Map(
"otel.exporter.prometheus.host" -> "127.0.0.2",
"otel.exporter.prometheus.port" -> "9465",
"otel.exporter.prometheus.shutdown.timeout" -> "10000",
"otel.exporter.prometheus.without.units" -> "true",
"otel.exporter.prometheus.without.type.suffix" -> "true",
"otel.exporter.prometheus.without.scope.info" -> "true",
Expand Down Expand Up @@ -148,10 +150,11 @@ class PrometheusMetricExporterAutoConfigureSuite extends CatsEffectSuite with Su
|1) `otel.exporter.prometheus.default.aggregation` - unspecified
|2) `otel.exporter.prometheus.host` - N/A
|3) `otel.exporter.prometheus.port` - N/A
|4) `otel.exporter.prometheus.without.scope.info` - N/A
|5) `otel.exporter.prometheus.without.target.info` - N/A
|6) `otel.exporter.prometheus.without.type.suffix` - N/A
|7) `otel.exporter.prometheus.without.units` - N/A""".stripMargin
|4) `otel.exporter.prometheus.shutdown.timeout` - N/A
|5) `otel.exporter.prometheus.without.scope.info` - N/A
|6) `otel.exporter.prometheus.without.target.info` - N/A
|7) `otel.exporter.prometheus.without.type.suffix` - N/A
|8) `otel.exporter.prometheus.without.units` - N/A""".stripMargin
)
)
}
Expand Down

0 comments on commit 286cb01

Please sign in to comment.