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: add ConsoleMetricExporter #642

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 cats.Monad
import cats.effect.std.Console
import cats.syntax.applicative._
import cats.syntax.flatMap._
import cats.syntax.foldable._
import cats.syntax.functor._
import org.typelevel.otel4s.sdk.metrics.data.MetricData

/** A metric exporter that logs every metric using [[cats.effect.std.Console]].
*
* @note
* use this exporter for debugging purposes because it may affect the
* performance
*
* @tparam F
* the higher-kinded type of a polymorphic effect
*/
private final class ConsoleMetricExporter[F[_]: Monad: Console](
val aggregationTemporalitySelector: AggregationTemporalitySelector
) extends MetricExporter[F] {

val name: String = "ConsoleMetricExporter"

def defaultAggregationSelector: AggregationSelector =
AggregationSelector.default

def defaultCardinalityLimitSelector: CardinalityLimitSelector =
CardinalityLimitSelector.default

def exportMetrics[G[_]: Foldable](metrics: G[MetricData]): F[Unit] = {
def doExport: F[Unit] =
for {
_ <- Console[F].println(
s"ConsoleMetricExporter: received a collection of [${metrics.size}] metrics for export."
)
_ <- metrics.traverse_ { metric =>
Console[F].println(s"ConsoleMetricExporter: $metric")
}
} yield ()

doExport.whenA(metrics.nonEmpty)
}

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

object ConsoleMetricExporter {

/** Creates a metric exporter that logs every metric using
* [[cats.effect.std.Console]].
*
* @tparam F
* the higher-kinded type of a polymorphic effect
*/
def apply[F[_]: Monad: Console]: MetricExporter[F] =
apply(AggregationTemporalitySelector.alwaysCumulative)

/** Creates a metric exporter that logs every metric using
* [[cats.effect.std.Console]].
*
* @param aggregationTemporalitySelector
* the aggregation temporality selector to use
*
* @tparam F
* the higher-kinded type of a polymorphic effect
*/
def apply[F[_]: Monad: Console](
aggregationTemporalitySelector: AggregationTemporalitySelector
): MetricExporter[F] =
new ConsoleMetricExporter[F](aggregationTemporalitySelector)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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
import munit.ScalaCheckEffectSuite
import org.scalacheck.Gen
import org.scalacheck.Test
import org.scalacheck.effect.PropF
import org.typelevel.otel4s.sdk.metrics.scalacheck.Gens
import org.typelevel.otel4s.sdk.test.InMemoryConsole
import org.typelevel.otel4s.sdk.test.InMemoryConsole._

class ConsoleMetricExporterSuite
extends CatsEffectSuite
with ScalaCheckEffectSuite {

test("print metrics to the console") {
PropF.forAllF(Gen.listOf(Gens.metricData)) { metrics =>
InMemoryConsole.create[IO].flatMap { implicit C: InMemoryConsole[IO] =>
val exporter = ConsoleMetricExporter[IO]

val expected =
if (metrics.isEmpty) Nil
else {
val first = Entry(
Op.Println,
s"ConsoleMetricExporter: received a collection of [${metrics.size}] metrics for export."
)

val rest = metrics.map { metric =>
Entry(Op.Println, s"ConsoleMetricExporter: $metric")
}

first :: rest
}

for {
_ <- exporter.exportMetrics(metrics)
entries <- C.entries
} yield assertEquals(entries, expected)
}
}
}

override protected def scalaCheckTestParameters: Test.Parameters =
super.scalaCheckTestParameters
.withMinSuccessfulTests(20)
.withMaxSize(20)

}