Metrics SDK odds and ends (#4273)

* Metrics SDK odds and ends

* Make InstrumentSelectorBuilder contructor package private

* Fix build

* Improve documentation for SdkMeterProviderBuilder defaults
This commit is contained in:
jack-berg 2022-03-21 13:45:51 -05:00 committed by GitHub
parent 211db4478e
commit 59697eedc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 55 additions and 21 deletions

View File

@ -61,10 +61,10 @@ public final class SdkMeterProvider implements MeterProvider, Closeable {
Clock clock, Clock clock,
Resource resource, Resource resource,
ViewRegistry viewRegistry, ViewRegistry viewRegistry,
ExemplarFilter exemplarSampler, ExemplarFilter exemplarFilter,
long minimumCollectionIntervalNanos) { long minimumCollectionIntervalNanos) {
this.sharedState = this.sharedState =
MeterProviderSharedState.create(clock, resource, viewRegistry, exemplarSampler); MeterProviderSharedState.create(clock, resource, viewRegistry, exemplarFilter);
this.registry = this.registry =
new ComponentRegistry<>( new ComponentRegistry<>(
instrumentationLibraryInfo -> new SdkMeter(sharedState, instrumentationLibraryInfo)); instrumentationLibraryInfo -> new SdkMeter(sharedState, instrumentationLibraryInfo));

View File

@ -25,13 +25,27 @@ import java.util.concurrent.TimeUnit;
/** Builder class for the {@link SdkMeterProvider}. */ /** Builder class for the {@link SdkMeterProvider}. */
public final class SdkMeterProviderBuilder { public final class SdkMeterProviderBuilder {
/**
* By default, the exemplar filter is set to sample with traces.
*
* @see #setExemplarFilter(ExemplarFilter)
*/
private static final ExemplarFilter DEFAULT_EXEMPLAR_FILTER = ExemplarFilter.sampleWithTraces();
/**
* By default, the minimum collection interval is 100ns.
*
* @see #setMinimumCollectionInterval(Duration)
*/
private static final long DEFAULT_MIN_COLLECTION_INTERVAL_NANOS =
TimeUnit.MILLISECONDS.toNanos(100);
private Clock clock = Clock.getDefault(); private Clock clock = Clock.getDefault();
private Resource resource = Resource.getDefault(); private Resource resource = Resource.getDefault();
private final ViewRegistryBuilder viewRegistryBuilder = ViewRegistry.builder(); private final ViewRegistryBuilder viewRegistryBuilder = ViewRegistry.builder();
private final List<MetricReaderFactory> metricReaders = new ArrayList<>(); private final List<MetricReaderFactory> metricReaders = new ArrayList<>();
// Default the sampling strategy. private ExemplarFilter exemplarFilter = DEFAULT_EXEMPLAR_FILTER;
private ExemplarFilter exemplarFilter = ExemplarFilter.sampleWithTraces(); private long minimumCollectionIntervalNanos = DEFAULT_MIN_COLLECTION_INTERVAL_NANOS;
private long minimumCollectionIntervalNanos = TimeUnit.MILLISECONDS.toNanos(100);
SdkMeterProviderBuilder() {} SdkMeterProviderBuilder() {}

View File

@ -20,7 +20,7 @@ public interface ExemplarData {
/** /**
* The set of key/value pairs that were filtered out by the aggregator, but recorded alongside the * The set of key/value pairs that were filtered out by the aggregator, but recorded alongside the
* original measurement. Only key/value pairs that were filtered out by the aggregator should be * original measurement. Only key/value pairs that were filtered out by the aggregator should be
* included * included.
*/ */
Attributes getFilteredAttributes(); Attributes getFilteredAttributes();

View File

@ -16,7 +16,7 @@ import java.util.function.Supplier;
/** /**
* A Reservoir sampler with fixed size that stores the given number of exemplars. * A Reservoir sampler with fixed size that stores the given number of exemplars.
* *
* <p>This implementation uses a un-unweighted/naive algorithm for sampler where the probability of * <p>This implementation uses an un-unweighted/naive algorithm for sampler where the probability of
* sampling decrease as the number of observations continue. The collectAndReset method resets the * sampling decrease as the number of observations continue. The collectAndReset method resets the
* count of observations, making the probability of sampling effectively 1.0. * count of observations, making the probability of sampling effectively 1.0.
* *
@ -33,7 +33,7 @@ final class FixedSizeExemplarReservoir extends AbstractFixedSizeExemplarReservoi
* @param size The number of exemplars to preserve. * @param size The number of exemplars to preserve.
* @param randomSupplier The random number generator to use for sampling. * @param randomSupplier The random number generator to use for sampling.
*/ */
public FixedSizeExemplarReservoir(Clock clock, int size, Supplier<Random> randomSupplier) { FixedSizeExemplarReservoir(Clock clock, int size, Supplier<Random> randomSupplier) {
super(clock, size); super(clock, size);
this.randomSupplier = randomSupplier; this.randomSupplier = randomSupplier;
} }

View File

@ -33,6 +33,8 @@ public abstract class InstrumentDescriptor {
return new AutoValue_InstrumentDescriptor(name, description, unit, type, valueType); return new AutoValue_InstrumentDescriptor(name, description, unit, type, valueType);
} }
InstrumentDescriptor() {}
public abstract String getName(); public abstract String getName();
public abstract String getDescription(); public abstract String getDescription();

View File

@ -48,6 +48,8 @@ public abstract class MetricDescriptor {
return new AutoValue_MetricDescriptor(name, description, view, instrument); return new AutoValue_MetricDescriptor(name, description, view, instrument);
} }
MetricDescriptor() {}
/** /**
* The name of the descriptor, equal to {@link View#getName()} if not null, else {@link * The name of the descriptor, equal to {@link View#getName()} if not null, else {@link
* InstrumentDescriptor#getName()}. * InstrumentDescriptor#getName()}.

View File

@ -21,6 +21,15 @@ import javax.annotation.concurrent.Immutable;
@AutoValue @AutoValue
@Immutable @Immutable
public abstract class CollectionInfo { public abstract class CollectionInfo {
/** Construct a new collection info object storing information for collection against a reader. */
public static CollectionInfo create(
CollectionHandle handle, Set<CollectionHandle> allCollectors, MetricReader reader) {
return new AutoValue_CollectionInfo(handle, allCollectors, reader);
}
CollectionInfo() {}
/** The current collection. */ /** The current collection. */
public abstract CollectionHandle getCollector(); public abstract CollectionHandle getCollector();
/** The set of all possible collectors. */ /** The set of all possible collectors. */
@ -33,10 +42,4 @@ public abstract class CollectionInfo {
public final AggregationTemporality getPreferredAggregation() { public final AggregationTemporality getPreferredAggregation() {
return getReader().getPreferredTemporality(); return getReader().getPreferredTemporality();
} }
/** Construct a new collection info object storing information for collection against a reader. */
public static CollectionInfo create(
CollectionHandle handle, Set<CollectionHandle> allCollectors, MetricReader reader) {
return new AutoValue_CollectionInfo(handle, allCollectors, reader);
}
} }

View File

@ -27,6 +27,8 @@ public abstract class MeterProviderSharedState {
clock, resource, viewRegistry, clock.now(), exemplarFilter); clock, resource, viewRegistry, clock.now(), exemplarFilter);
} }
MeterProviderSharedState() {}
/** Returns the clock used for measurements. */ /** Returns the clock used for measurements. */
public abstract Clock getClock(); public abstract Clock getClock();

View File

@ -34,6 +34,8 @@ public abstract class MeterSharedState {
return new AutoValue_MeterSharedState(instrumentationScopeInfo, new MetricStorageRegistry()); return new AutoValue_MeterSharedState(instrumentationScopeInfo, new MetricStorageRegistry());
} }
MeterSharedState() {}
// only visible for testing. // only visible for testing.
/** Returns the {@link InstrumentationScopeInfo} for this {@code Meter}. */ /** Returns the {@link InstrumentationScopeInfo} for this {@code Meter}. */
public abstract InstrumentationScopeInfo getInstrumentationScopeInfo(); public abstract InstrumentationScopeInfo getInstrumentationScopeInfo();

View File

@ -22,6 +22,8 @@ import javax.annotation.concurrent.Immutable;
@Immutable @Immutable
public abstract class ImmutableView implements View { public abstract class ImmutableView implements View {
ImmutableView() {}
/** Returns the {@link AttributesProcessor} for the {@link View}. */ /** Returns the {@link AttributesProcessor} for the {@link View}. */
public static AttributesProcessor getAttributesProcessor(View view) { public static AttributesProcessor getAttributesProcessor(View view) {
if (view instanceof ImmutableView) { if (view instanceof ImmutableView) {

View File

@ -19,12 +19,15 @@ import javax.annotation.concurrent.Immutable;
@AutoValue @AutoValue
@Immutable @Immutable
abstract class RegisteredView { abstract class RegisteredView {
/** Instrument fitler for applying this view. */
abstract InstrumentSelector getInstrumentSelector();
/** The view to apply. */
abstract View getView();
static RegisteredView create(InstrumentSelector selector, View view) { static RegisteredView create(InstrumentSelector selector, View view) {
return new AutoValue_RegisteredView(selector, view); return new AutoValue_RegisteredView(selector, view);
} }
RegisteredView() {}
/** Instrument fitler for applying this view. */
abstract InstrumentSelector getInstrumentSelector();
/** The view to apply. */
abstract View getView();
} }

View File

@ -38,6 +38,8 @@ public abstract class InstrumentSelector {
meterSchemaUrlFilter); meterSchemaUrlFilter);
} }
InstrumentSelector() {}
/** /**
* Returns {@link InstrumentType} that should be selected. If null, then this specifier will not * Returns {@link InstrumentType} that should be selected. If null, then this specifier will not
* be used. * be used.

View File

@ -22,6 +22,8 @@ public final class InstrumentSelectorBuilder {
private Predicate<String> meterVersionFilter = StringPredicates.ALL; private Predicate<String> meterVersionFilter = StringPredicates.ALL;
private Predicate<String> meterSchemaUrlFilter = StringPredicates.ALL; private Predicate<String> meterSchemaUrlFilter = StringPredicates.ALL;
InstrumentSelectorBuilder() {}
/** Sets a specifier for {@link InstrumentType}. */ /** Sets a specifier for {@link InstrumentType}. */
public InstrumentSelectorBuilder setType(InstrumentType instrumentType) { public InstrumentSelectorBuilder setType(InstrumentType instrumentType) {
requireNonNull(instrumentType, "instrumentType"); requireNonNull(instrumentType, "instrumentType");

View File

@ -11,7 +11,7 @@ import java.util.function.Predicate;
public interface ViewBuilder { public interface ViewBuilder {
/** /**
* sets the name of the resulting metric. * Sets the name of the resulting metric.
* *
* @param name metric name or {@code null} if the underlying instrument name should be used. * @param name metric name or {@code null} if the underlying instrument name should be used.
* @return this Builder. * @return this Builder.
@ -19,7 +19,7 @@ public interface ViewBuilder {
ViewBuilder setName(String name); ViewBuilder setName(String name);
/** /**
* sets the name of the resulting metric. * Sets the description of the resulting metric.
* *
* @param description metric description or {@code null} if the underlying instrument description * @param description metric description or {@code null} if the underlying instrument description
* should be used. * should be used.
@ -28,7 +28,7 @@ public interface ViewBuilder {
ViewBuilder setDescription(String description); ViewBuilder setDescription(String description);
/** /**
* sets {@link Aggregation}. * Sets {@link Aggregation}.
* *
* @param aggregation aggregation to use. * @param aggregation aggregation to use.
* @return this Builder. * @return this Builder.