Skip to content

Commit

Permalink
implement async long and double counters
Browse files Browse the repository at this point in the history
  • Loading branch information
ywangd committed Aug 27, 2024
1 parent e633802 commit b9c0bd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,29 @@ public void incrementBy(long inc, Map<String, Object> attributes) {

public static class RecordingAsyncLongCounter extends CallbackRecordingInstrument implements LongAsyncCounter {

public RecordingAsyncLongCounter(String name, Supplier<LongWithAttributes> observer, MetricRecorder<Instrument> recorder) {
public RecordingAsyncLongCounter(
String name,
Supplier<Collection<LongWithAttributes>> observer,
MetricRecorder<Instrument> recorder
) {
super(name, () -> {
var observation = observer.get();
return Collections.singletonList(new Tuple<>(observation.value(), observation.attributes()));
return observation.stream().map(o -> new Tuple<>((Number) o.value(), o.attributes())).toList();
}, recorder);
}

}

public static class RecordingAsyncDoubleCounter extends CallbackRecordingInstrument implements DoubleAsyncCounter {

public RecordingAsyncDoubleCounter(String name, Supplier<DoubleWithAttributes> observer, MetricRecorder<Instrument> recorder) {
public RecordingAsyncDoubleCounter(
String name,
Supplier<Collection<DoubleWithAttributes>> observer,
MetricRecorder<Instrument> recorder
) {
super(name, () -> {
var observation = observer.get();
return Collections.singletonList(new Tuple<>(observation.value(), observation.attributes()));
return observation.stream().map(o -> new Tuple<>((Number) o.value(), o.attributes())).toList();
}, recorder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ public LongCounter registerLongCounter(String name, String description, String u

@Override
public LongAsyncCounter registerLongAsyncCounter(String name, String description, String unit, Supplier<LongWithAttributes> observer) {
LongAsyncCounter instrument = new RecordingInstruments.RecordingAsyncLongCounter(name, observer, recorder);
recorder.register(instrument, InstrumentType.fromInstrument(instrument), name, description, unit);
return instrument;
return registerLongsAsyncCounter(name, description, unit, () -> Collections.singleton(observer.get()));
}

@Override
Expand All @@ -139,7 +137,9 @@ public LongAsyncCounter registerLongsAsyncCounter(
String unit,
Supplier<Collection<LongWithAttributes>> observer
) {
throw new UnsupportedOperationException("not implemented");
LongAsyncCounter instrument = new RecordingInstruments.RecordingAsyncLongCounter(name, observer, recorder);
recorder.register(instrument, InstrumentType.fromInstrument(instrument), name, description, unit);
return instrument;
}

@Override
Expand All @@ -154,9 +154,7 @@ public DoubleAsyncCounter registerDoubleAsyncCounter(
String unit,
Supplier<DoubleWithAttributes> observer
) {
DoubleAsyncCounter instrument = new RecordingInstruments.RecordingAsyncDoubleCounter(name, observer, recorder);
recorder.register(instrument, InstrumentType.fromInstrument(instrument), name, description, unit);
return instrument;
return registerDoublesAsyncCounter(name, description, unit, () -> Collections.singleton(observer.get()));
}

@Override
Expand All @@ -166,7 +164,9 @@ public DoubleAsyncCounter registerDoublesAsyncCounter(
String unit,
Supplier<Collection<DoubleWithAttributes>> observer
) {
throw new UnsupportedOperationException("not implemented");
DoubleAsyncCounter instrument = new RecordingInstruments.RecordingAsyncDoubleCounter(name, observer, recorder);
recorder.register(instrument, InstrumentType.fromInstrument(instrument), name, description, unit);
return instrument;
}

@Override
Expand Down

0 comments on commit b9c0bd0

Please sign in to comment.