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

MeterProvider that delegates to Micrometer MeterRegistry #328

Merged
merged 21 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7fdfe9b
Initial commit of OTel MeterProvider that delegates to Micrometer Met…
HaloFour May 6, 2022
09e4b49
Fix copypasta
HaloFour May 6, 2022
c565d85
Unit tests
HaloFour May 6, 2022
2ecb33f
Integration test, gradle cleanup
HaloFour May 6, 2022
58622c6
Documentation
HaloFour May 6, 2022
95a4dc1
Handle exceptions on observable instrument callbacksr
HaloFour May 7, 2022
2340cf4
Reduce accessibility of instruments and constructors
HaloFour May 7, 2022
62da23b
Refactor polling behind interface
HaloFour May 10, 2022
cd8dad1
Spotless
HaloFour May 10, 2022
79f9836
Defer use of MeterRegistry (for Spring), create polling meter on demand
HaloFour May 13, 2022
e74a60b
Rename module to micrometer-meter-provider
HaloFour May 13, 2022
f6499e9
Add timer-based CallbackRegistrar for custom polling
HaloFour May 13, 2022
e20b997
Add more javadocs, update README.md
HaloFour May 13, 2022
e621314
Feedback, switch to ScheduledExecutorService for registrar
HaloFour May 14, 2022
605feb3
Poll instruments using single schedule with ScheduledCallbackRegistrar
HaloFour May 19, 2022
7a1d2f6
Add instrumentation tags and cache attributes to tags mappings
HaloFour May 19, 2022
f79b919
Change TestCallbackRegistrar to not directly extend ArrayList
HaloFour May 19, 2022
c0065ea
PR feedback
HaloFour May 19, 2022
aa8c650
Record absolute values in async counters
HaloFour May 20, 2022
9da6968
Instrumentation scope as Micrometer tags in meter state
HaloFour May 20, 2022
befdef4
Switch UpDownCounters to Micrometer Gauge, memoize MeterRegistry supp…
HaloFour May 20, 2022
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
2 changes: 2 additions & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ components:
maven-extension:
- cyrille-leclerc
- kenfinnigan
micrometer-meter-provider:
- HaloFour
runtime-attach:
- iNikem
- trask
Expand Down
61 changes: 61 additions & 0 deletions micrometer-meter-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Micrometer MeterProvider

This utility provides an implementation of `MeterProvider` which wraps a Micrometer `MeterRegistry`
and delegates the reporting of all metrics through Micrometer. This enables projects which already
rely on Micrometer and cannot currently migrate to OpenTelemetry Metrics to be able to report on
metrics that are reported through the OpenTelemetry Metrics API.

### Usage

Create the `MicrometerMeterProvider` passing an existing instance of `MeterRegistry`. Then you can
use the OpenTelemetry Metrics `MeterProvider` API to create instruments.

```java
MeterRegistry meterRegistry = ...;

// create the meter provider
MeterProvider meterProvider = MicrometerMeterProvider.builder(meterRegistry)
.build();
Meter meter = meterProvider.get("my-app");

// create an instrument
LongCounter counter = meter.counterBuilder("my.counter")
.build();

// record metrics
count.add(1, Attributes.of(AttributeKey.stringKey("key"), "value"));
```

**Note**: Instruments in OpenTelemetry are created without tags, which are reported with each
measurement. But tags are required to create Micrometer metrics. Because of this difference the
adapter must listen for when measurements are being read by the `MeterRegistry` in order to call
callbacks registered for observable metrics in order to create the Micrometer meters on demand.

By default the `MicrometerMeterProvider` will create a dummy `Metric` with the name
"otel-polling-meter" which will be used to poll the asynchronous OpenTelemetry instruments as it
is measured. However, you can also specify an alternative `CallbackRegistrar` strategy.

```java
MeterRegistry meterRegistry = ...;

// create the meter provider
MeterProvider meterProvider = MicrometerMeterProvider.builder(meterRegistry)
.setCallbackRegistrar(ScheduledCallbackRegistrar.builder()
.setPeriod(Duration.ofSeconds(10L))
.build())
.build();
Meter meter = meterProvider.get("my-app");

// create an asynchronous instrument
ObservableDoubleGauge gauge = meter.gaugeBuilder("my.gauge")
.buildWithCallback(measurement -> {
// record metrics
measurement.record(queue.size(), Attributes.of(AttributeKey.stringKey("key"), "value"));
});
```

## Component owners

- [Justin Spindler](https://github.com/HaloFour), Comcast

Learn more about component owners in [component_owners.yml](../.github/component_owners.yml).
32 changes: 32 additions & 0 deletions micrometer-meter-provider/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
id("otel.java-conventions")
id("otel.publish-conventions")
}

description = "OpenTelemetry Micrometer MeterProvider"

dependencies {
api("io.opentelemetry:opentelemetry-api")
api("io.opentelemetry:opentelemetry-sdk-metrics")

compileOnly("io.micrometer:micrometer-core:1.1.0")
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")

annotationProcessor("com.google.auto.service:auto-service")
compileOnly("com.google.auto.service:auto-service-annotations")

annotationProcessor("com.google.auto.value:auto-value")
compileOnly("com.google.auto.value:auto-value-annotations")

testImplementation("io.micrometer:micrometer-core:1.8.5")
}

testing {
suites {
val integrationTest by registering(JvmTestSuite::class) {
dependencies {
implementation("io.micrometer:micrometer-registry-prometheus:1.8.5")
}
}
}
}
Loading