-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
598 additions
and
2 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
126 changes: 126 additions & 0 deletions
126
micrometer-shim/src/main/java/io/opentelemetry/contrib/metrics/micrometer/DoubleCounter.java
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,126 @@ | ||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import java.util.function.Consumer; | ||
|
||
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; | ||
|
||
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); | ||
} | ||
|
||
final static 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); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
final static 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() {} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
micrometer-shim/src/main/java/io/opentelemetry/contrib/metrics/micrometer/DoubleGauge.java
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,50 @@ | ||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.opentelemetry.api.metrics.LongGaugeBuilder; | ||
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; | ||
|
||
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)); | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
micrometer-shim/src/main/java/io/opentelemetry/contrib/metrics/micrometer/LongCounter.java
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,126 @@ | ||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import java.util.function.Consumer; | ||
|
||
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; | ||
|
||
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); | ||
} | ||
|
||
final static 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); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
final static 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() {} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
micrometer-shim/src/main/java/io/opentelemetry/contrib/metrics/micrometer/LongGauge.java
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,50 @@ | ||
package io.opentelemetry.contrib.metrics.micrometer; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.opentelemetry.api.metrics.DoubleGaugeBuilder; | ||
import io.opentelemetry.api.metrics.ObservableLongMeasurement; | ||
|
||
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.