Skip to content

Commit

Permalink
First pass, impl counter and gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
HaloFour committed Aug 30, 2021
1 parent 8d5794a commit f7ef8c9
Show file tree
Hide file tree
Showing 13 changed files with 657 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ val DEPENDENCY_BOMS = listOf(
"org.junit:junit-bom:5.7.2",
"com.linecorp.armeria:armeria-bom:1.9.1",
"io.grpc:grpc-bom:1.39.0",
"io.opentelemetry:opentelemetry-bom:1.4.1",
"io.opentelemetry:opentelemetry-bom-alpha:1.4.1-alpha",
"io.opentelemetry:opentelemetry-bom:1.5.0",
"io.opentelemetry:opentelemetry-bom-alpha:1.5.0-alpha",
"org.testcontainers:testcontainers-bom:1.16.0"
)

Expand Down
12 changes: 12 additions & 0 deletions micrometer-shim/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id("otel.java-conventions")
id("otel.publish-conventions")
}

description = "OpenTelemetry Metrics Micrometer Shim"

dependencies {
api("io.opentelemetry:opentelemetry-api")
api("io.opentelemetry:opentelemetry-api-metrics")
implementation("io.micrometer:micrometer-core:1.7.3")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.metrics.micrometer;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Tags;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.BoundDoubleCounter;
import io.opentelemetry.api.metrics.DoubleCounterBuilder;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import io.opentelemetry.context.Context;
import java.util.function.Consumer;

final class DoubleCounter implements io.opentelemetry.api.metrics.DoubleCounter {
private final SharedMeterState state;
private final Reader<Counter.Builder> factory;

private DoubleCounter(SharedMeterState state, Reader<Counter.Builder> factory) {
this.state = state;
this.factory = factory;
}

@Override
public void add(double value) {
add(value, Attributes.empty(), Context.current());
}

@Override
public void add(double value, Attributes attributes) {
add(value, attributes, Context.current());
}

@Override
public void add(double value, Attributes attributes, Context context) {
BoundDoubleCounter bound = bind(attributes);
try {
bound.add(value, context);
} finally {
bound.unbind();
}
}

@Override
public BoundDoubleCounter bind(Attributes attributes) {
Tags tags = TagUtils.attributesToTags(attributes);
Counter counter = factory.get().tags(tags).register(state.meterRegistry());
return new Bound(counter);
}

static DoubleCounterBuilder newBuilder(SharedMeterState state, String name) {
return new Builder(state, () -> Counter.builder(name));
}

static DoubleCounterBuilder newBuilder(SharedMeterState state, Reader<Counter.Builder> factory) {
return new Builder(state, factory);
}

static final class Builder implements DoubleCounterBuilder {
private final SharedMeterState state;
private final Reader<Counter.Builder> factory;

Builder(SharedMeterState state, Reader<Counter.Builder> factory) {
this.state = state;
this.factory = factory;
}

@Override
public Builder setDescription(String description) {
return new Builder(state, factory.andThen(builder -> builder.description(description)));
}

@Override
public Builder setUnit(String unit) {
return new Builder(state, factory.andThen(builder -> builder.baseUnit(unit)));
}

@Override
public LongCounterBuilder ofLongs() {
return LongCounter.newBuilder(state, factory);
}

@Override
public DoubleCounter build() {
return new DoubleCounter(state, factory);
}

@Override
public void buildWithCallback(Consumer<ObservableDoubleMeasurement> callback) {
DoubleCounter counter = build();
callback.accept(
new ObservableDoubleMeasurement() {
@Override
public void observe(double value) {
counter.add(value);
}

@Override
public void observe(double value, Attributes attributes) {
counter.add(value, attributes);
}
});
}
}

static final class Bound implements BoundDoubleCounter {
private final Counter counter;

Bound(Counter counter) {
this.counter = counter;
}

@Override
public void add(double value) {
add(value, Context.current());
}

@Override
public void add(double value, Context context) {
counter.increment(value);
}

@Override
public void unbind() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.metrics.micrometer;

import io.micrometer.core.instrument.Gauge;
import io.opentelemetry.api.metrics.LongGaugeBuilder;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

final class DoubleGauge {

static DoubleGaugeBuilder newBuilder(SharedMeterState state, String name) {
return newBuilder(state, supplier -> Gauge.builder(name, supplier));
}

public static DoubleGaugeBuilder newBuilder(
SharedMeterState state, Function<Supplier<Number>, Gauge.Builder<Supplier<Number>>> factory) {
return new DoubleGaugeBuilder(state, factory);
}

static final class DoubleGaugeBuilder implements io.opentelemetry.api.metrics.DoubleGaugeBuilder {
private final SharedMeterState state;
private final Function<Supplier<Number>, Gauge.Builder<Supplier<Number>>> factory;

DoubleGaugeBuilder(
SharedMeterState state,
Function<Supplier<Number>, Gauge.Builder<Supplier<Number>>> factory) {
this.state = state;
this.factory = factory;
}

@Override
public DoubleGaugeBuilder setDescription(String description) {
return new DoubleGaugeBuilder(
state, factory.andThen(builder -> builder.description(description)));
}

@Override
public DoubleGaugeBuilder setUnit(String unit) {
return new DoubleGaugeBuilder(state, factory.andThen(builder -> builder.baseUnit(unit)));
}

@Override
public LongGaugeBuilder ofLongs() {
return LongGauge.newBuilder(state, factory);
}

@Override
public void buildWithCallback(Consumer<ObservableDoubleMeasurement> callback) {
callback.accept(new ObservableGaugeMeasurement(state, factory));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.metrics.micrometer;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Tag;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.BoundLongCounter;
import io.opentelemetry.api.metrics.DoubleCounterBuilder;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
import io.opentelemetry.context.Context;
import java.util.function.Consumer;

final class LongCounter implements io.opentelemetry.api.metrics.LongCounter {
private final SharedMeterState state;
private final Reader<Counter.Builder> factory;

private LongCounter(SharedMeterState state, Reader<Counter.Builder> factory) {
this.state = state;
this.factory = factory;
}

@Override
public void add(long value) {
add(value, Attributes.empty(), Context.current());
}

@Override
public void add(long value, Attributes attributes) {
add(value, attributes, Context.current());
}

@Override
public void add(long value, Attributes attributes, Context context) {
BoundLongCounter bound = bind(attributes);
try {
bound.add(value, context);
} finally {
bound.unbind();
}
}

@Override
public BoundLongCounter bind(Attributes attributes) {
Iterable<Tag> tags = TagUtils.attributesToTags(attributes);
Counter counter = factory.get().tags(tags).register(state.meterRegistry());
return new Bound(counter);
}

static LongCounterBuilder newBuilder(SharedMeterState state, String name) {
return new Builder(state, () -> Counter.builder(name));
}

static LongCounterBuilder newBuilder(SharedMeterState state, Reader<Counter.Builder> factory) {
return new Builder(state, factory);
}

static final class Builder implements LongCounterBuilder {
private final SharedMeterState state;
private final Reader<Counter.Builder> factory;

Builder(SharedMeterState state, Reader<Counter.Builder> factory) {
this.state = state;
this.factory = factory;
}

@Override
public Builder setDescription(String description) {
return new Builder(state, factory.andThen(builder -> builder.description(description)));
}

@Override
public Builder setUnit(String unit) {
return new Builder(state, factory.andThen(builder -> builder.baseUnit(unit)));
}

@Override
public DoubleCounterBuilder ofDoubles() {
return null;
}

@Override
public LongCounter build() {
return new LongCounter(state, factory);
}

@Override
public void buildWithCallback(Consumer<ObservableLongMeasurement> callback) {
LongCounter counter = build();
callback.accept(
new ObservableLongMeasurement() {
@Override
public void observe(long value) {
counter.add(value);
}

@Override
public void observe(long value, Attributes attributes) {
counter.add(value, attributes);
}
});
}
}

static final class Bound implements BoundLongCounter {
private final Counter counter;

Bound(Counter counter) {
this.counter = counter;
}

@Override
public void add(long value) {
add(value, Context.current());
}

@Override
public void add(long value, Context context) {
counter.increment(value);
}

@Override
public void unbind() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.metrics.micrometer;

import io.micrometer.core.instrument.Gauge;
import io.opentelemetry.api.metrics.DoubleGaugeBuilder;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

final class LongGauge {

static LongGaugeBuilder newBuilder(SharedMeterState state, String name) {
return newBuilder(state, supplier -> Gauge.builder(name, supplier));
}

public static LongGaugeBuilder newBuilder(
SharedMeterState state, Function<Supplier<Number>, Gauge.Builder<Supplier<Number>>> factory) {
return new LongGaugeBuilder(state, factory);
}

static final class LongGaugeBuilder implements io.opentelemetry.api.metrics.LongGaugeBuilder {
private final SharedMeterState state;
private final Function<Supplier<Number>, Gauge.Builder<Supplier<Number>>> factory;

LongGaugeBuilder(
SharedMeterState state,
Function<Supplier<Number>, Gauge.Builder<Supplier<Number>>> factory) {
this.state = state;
this.factory = factory;
}

@Override
public LongGaugeBuilder setDescription(String description) {
return new LongGaugeBuilder(
state, factory.andThen(builder -> builder.description(description)));
}

@Override
public LongGaugeBuilder setUnit(String unit) {
return new LongGaugeBuilder(state, factory.andThen(builder -> builder.baseUnit(unit)));
}

@Override
public DoubleGaugeBuilder ofDoubles() {
return DoubleGauge.newBuilder(state, factory);
}

@Override
public void buildWithCallback(Consumer<ObservableLongMeasurement> callback) {
callback.accept(new ObservableGaugeMeasurement(state, factory));
}
}
}
Loading

0 comments on commit f7ef8c9

Please sign in to comment.