All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zio.metrics.connectors.datadog.package.scala Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package zio.metrics.connectors

import zio.{metrics, _}
import zio.internal.RingBuffer
import zio.metrics.{MetricClient, MetricKey, MetricKeyType}
import zio.metrics.connectors.internal.MetricsClient
import zio.metrics.connectors.statsd.StatsdClient
import zio.metrics.connectors.statsd.StatsdConfig

package object datadog {

  def statsdHandler(client: StatsdClient): Iterable[MetricEvent] => UIO[Unit] = events =>
    statsd.statsdHandler(client)(events.filter(!_.metricKey.keyType.isInstanceOf[metrics.MetricKeyType.Histogram]))

  lazy val datadogLayer: ZLayer[DatadogConfig & MetricsConfig, Nothing, Unit] =
    ZLayer.scoped(
      for {
        config  <- ZIO.service[DatadogConfig]
        clt     <- StatsdClient.make.provideSome[Scope](ZLayer.succeed(StatsdConfig(config.host, config.port)))
        queue    = RingBuffer.apply[(MetricKey[MetricKeyType.Histogram], Double)](config.maxQueueSize)
        listener = new DataDogListener(queue)
        _       <- Unsafe.unsafe(implicit unsafe =>
                     ZIO.acquireRelease(ZIO.succeed(MetricClient.addListener(listener)))(_ =>
                       ZIO.succeed(MetricClient.removeListener(listener)),
                     ),
                   )
        _       <- DataDogEventProcessor.make(clt, queue)
        _       <- MetricsClient.make(statsdHandler(clt))
      } yield (),
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy