Skip to content

Commit

Permalink
Merge pull request #607 from iRevive/sdk-metrics/metric-exporter [no ci]
Browse files Browse the repository at this point in the history
sdk-metrics: add `MetricExporter`
  • Loading branch information
iRevive authored Apr 19, 2024
2 parents c5bdd9d + 9cf73d5 commit 5f150a1
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 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.sdk.metrics.exporter

import cats.Applicative
import cats.Foldable
import org.typelevel.otel4s.sdk.metrics.data.MetricData

/** `MetricExporter` is a push based interface for exporting `MetricData`.
*
* @see
* [[https://opentelemetry.io/docs/specs/otel/metrics/sdk/#metricexporter]]
*/
trait MetricExporter[F[_]] {

/** The name of the exporter.
*/
def name: String

/** The preferred aggregation temporality for the given instrument.
*/
def aggregationTemporalitySelector: AggregationTemporalitySelector

/** The preferred aggregation for the given instrument.
*/
def defaultAggregationSelector: AggregationSelector

/** The preferred cardinality limit for the given instrument.
*/
def defaultCardinalityLimitSelector: CardinalityLimitSelector

/** Exports the sampled `MetricData`.
*
* @param metrics
* the sampled metrics to export
*/
def exportMetrics[G[_]: Foldable](metrics: G[MetricData]): F[Unit]

/** Exports the collection of sampled `MetricData` that have not yet been
* exported.
*/
def flush: F[Unit]

override def toString: String =
name

}

object MetricExporter {

/** Creates a no-op implementation of the [[MetricExporter]].
*
* All export operations are no-op.
*/
def noop[F[_]: Applicative]: MetricExporter[F] =
new Noop

private final class Noop[F[_]: Applicative] extends MetricExporter[F] {
val name: String = "MetricExporter.Noop"

def aggregationTemporalitySelector: AggregationTemporalitySelector =
AggregationTemporalitySelector.alwaysCumulative

def defaultAggregationSelector: AggregationSelector =
AggregationSelector.default

def defaultCardinalityLimitSelector: CardinalityLimitSelector =
CardinalityLimitSelector.default

def exportMetrics[G[_]: Foldable](metrics: G[MetricData]): F[Unit] =
Applicative[F].unit

def flush: F[Unit] =
Applicative[F].unit
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 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.sdk.metrics.exporter

import cats.effect.IO
import munit.CatsEffectSuite

class MetricExporterSuite extends CatsEffectSuite {

test("create a no-op instance") {
val exporter = MetricExporter.noop[IO]

assertEquals(exporter.toString, "MetricExporter.Noop")
}

}

0 comments on commit 5f150a1

Please sign in to comment.