-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
otel4s-testkit
with otel4s-oteljava-testkit
- Loading branch information
Showing
25 changed files
with
1,027 additions
and
826 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
oteljava/all/src/test/scala/org/typelevel/otel4s/oteljava/metrics/CounterSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2022 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 | ||
package oteljava | ||
package metrics | ||
|
||
import cats.effect.IO | ||
import cats.effect.Resource | ||
import io.opentelemetry.sdk.metrics._ | ||
import munit.CatsEffectSuite | ||
import org.typelevel.otel4s.oteljava.testkit.InstrumentationScope | ||
import org.typelevel.otel4s.oteljava.testkit.TelemetryResource | ||
import org.typelevel.otel4s.oteljava.testkit.metrics.TestkitMetrics | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
class CounterSuite extends CatsEffectSuite { | ||
|
||
test("Counter record a proper data") { | ||
makeSdk.use { sdk => | ||
for { | ||
meter <- sdk.meterProvider | ||
.meter("java.otel.suite") | ||
.withVersion("1.0") | ||
.withSchemaUrl("https://localhost:8080") | ||
.get | ||
|
||
counter <- meter | ||
.counter[Long]("counter") | ||
.withUnit("unit") | ||
.withDescription("description") | ||
.create | ||
|
||
_ <- counter.add(1L, Attribute("string-attribute", "value")) | ||
|
||
metrics <- sdk.metrics | ||
} yield { | ||
val resourceAttributes = Attributes( | ||
Attribute("service.name", "unknown_service:java"), | ||
Attribute("telemetry.sdk.language", "java"), | ||
Attribute("telemetry.sdk.name", "opentelemetry"), | ||
Attribute("telemetry.sdk.version", BuildInfo.openTelemetrySdkVersion) | ||
) | ||
|
||
val scope = InstrumentationScope( | ||
name = "java.otel.suite", | ||
version = Some("1.0"), | ||
schemaUrl = Some("https://localhost:8080"), | ||
attributes = Attributes.empty | ||
) | ||
|
||
assertEquals(metrics.map(_.name), List("counter")) | ||
assertEquals(metrics.map(_.description), List(Some("description"))) | ||
assertEquals(metrics.map(_.unit), List(Some("unit"))) | ||
assertEquals( | ||
metrics.map(_.resource), | ||
List(TelemetryResource(resourceAttributes, None)) | ||
) | ||
assertEquals(metrics.map(_.scope), List(scope)) | ||
assertEquals[List[Any], List[Any]]( | ||
metrics.map(_.data.points.map(_.value)), | ||
List(List(1L)) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private def makeSdk: Resource[IO, TestkitMetrics[IO]] = { | ||
def customize(builder: SdkMeterProviderBuilder): SdkMeterProviderBuilder = | ||
builder.registerView( | ||
InstrumentSelector.builder().setType(InstrumentType.HISTOGRAM).build(), | ||
View | ||
.builder() | ||
.setAggregation( | ||
Aggregation.explicitBucketHistogram( | ||
HistogramBuckets.map(Double.box).asJava | ||
) | ||
) | ||
.build() | ||
) | ||
|
||
TestkitMetrics.inMemory[IO](customize) | ||
} | ||
|
||
private val HistogramBuckets: List[Double] = | ||
List(0.01, 1, 100) | ||
|
||
} |
Oops, something went wrong.