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

Very basic Aggregation-configuration API in the SDK. #2037

Merged
merged 37 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
631d00f
Create a very basic view API in the SDK.
Jul 14, 2020
9bbbe44
fix formatting
Jul 14, 2020
6063ff4
move the ViewRegistry up one package, and clean up the visibility of …
Jul 14, 2020
8432091
Support matching by instrument name
Jul 14, 2020
cbe903d
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/ViewRegistry.java
jkwatson Jul 16, 2020
d42a9de
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/view/ViewSpecif…
jkwatson Jul 16, 2020
7742cf9
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/view/ViewSpecif…
jkwatson Jul 16, 2020
ed28843
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/view/ViewSpecif…
jkwatson Jul 16, 2020
ab50fc9
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/view/ViewSpecif…
jkwatson Jul 16, 2020
856ca82
fix formatting issues from GH
Jul 16, 2020
eaa2b3d
small renaming to a big name
Jul 20, 2020
2d07c94
small renaming to a big name
Jul 20, 2020
230627d
re-order matching check and fix a merge issue
Jul 30, 2020
36d6887
Update from upstream changes.
jkwatson Nov 6, 2020
b1bbf16
Update from upstream changes.
jkwatson Nov 6, 2020
b7f2967
Adjust defaults based on the latest behavior
jkwatson Nov 6, 2020
3a3c72f
refactor before writing tests
jkwatson Nov 11, 2020
9678531
tests for the AggregationChooser and a bugfix they uncovered
jkwatson Nov 12, 2020
77f5c7f
tests for the ViewRegistry
jkwatson Nov 12, 2020
c85e464
Javadoc for the AggregationConfiguration
jkwatson Nov 12, 2020
778a8a3
Add more javadoc.
jkwatson Nov 12, 2020
567c16e
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/Batcher…
jkwatson Nov 13, 2020
79a29fd
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/Ag…
jkwatson Nov 13, 2020
25d5464
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/Ag…
jkwatson Nov 13, 2020
55537f0
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
02a8324
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
c7e4b59
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/view/Aggregatio…
jkwatson Nov 13, 2020
fd42ee3
Update sdk/src/main/java/io/opentelemetry/sdk/metrics/view/Aggregatio…
jkwatson Nov 13, 2020
28b7636
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/Ag…
jkwatson Nov 13, 2020
444c3b9
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
5e94bc6
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
41180b2
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
bd60c11
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
d99012d
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
5d783b3
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
816c35c
fix formatting issues
jkwatson Nov 13, 2020
2f5862c
Update sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/In…
jkwatson Nov 13, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public void batch(Labels labelSet, Aggregator aggregator, boolean mappedAggregat
public List<MetricData> completeCollectionCycle() {
return batcher.completeCollectionCycle();
}

@Override
public boolean generatesDeltas() {
return batcher.generatesDeltas();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics;

import io.opentelemetry.sdk.metrics.view.AggregationConfiguration;
import io.opentelemetry.sdk.metrics.view.Aggregations;
import io.opentelemetry.sdk.metrics.view.InstrumentSelector;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

class AggregationChooser {
private static final AggregationConfiguration CUMULATIVE_SUM =
AggregationConfiguration.create(
Aggregations.sum(), AggregationConfiguration.Temporality.CUMULATIVE);
private static final AggregationConfiguration DELTA_SUMMARY =
AggregationConfiguration.create(
Aggregations.minMaxSumCount(), AggregationConfiguration.Temporality.DELTA);
private static final AggregationConfiguration CUMULATIVE_LAST_VALUE =
AggregationConfiguration.create(
Aggregations.lastValue(), AggregationConfiguration.Temporality.CUMULATIVE);
private static final AggregationConfiguration DELTA_LAST_VALUE =
AggregationConfiguration.create(
Aggregations.lastValue(), AggregationConfiguration.Temporality.DELTA);

private final Map<InstrumentSelector, AggregationConfiguration> configuration =
new ConcurrentHashMap<>();

AggregationConfiguration chooseAggregation(InstrumentDescriptor descriptor) {
List<Map.Entry<InstrumentSelector, AggregationConfiguration>> possibleMatches =
new ArrayList<>();
for (Map.Entry<InstrumentSelector, AggregationConfiguration> entry : configuration.entrySet()) {
InstrumentSelector registeredSelector = entry.getKey();
// if it matches everything, return it right away...
if (matchesOnType(descriptor, registeredSelector)
&& matchesOnName(descriptor, registeredSelector)) {
return entry.getValue();
}
// otherwise throw it into a bucket of possible matches if it matches one of the criteria
if (matchesOne(descriptor, registeredSelector)) {
possibleMatches.add(entry);
}
}

if (possibleMatches.isEmpty()) {
return getDefaultSpecification(descriptor);
}

// If no exact matches found, pick the first one that matches something:
return possibleMatches.get(0).getValue();
}

private static boolean matchesOne(InstrumentDescriptor descriptor, InstrumentSelector selector) {
if (selector.hasInstrumentNameRegex() && !matchesOnName(descriptor, selector)) {
return false;
}
if (selector.hasInstrumentType() && !matchesOnType(descriptor, selector)) {
return false;
}
return true;
}

private static boolean matchesOnType(
InstrumentDescriptor descriptor, InstrumentSelector selector) {
if (selector.instrumentType() == null) {
return false;
}
return selector.instrumentType().equals(descriptor.getType());
}

private static boolean matchesOnName(
InstrumentDescriptor descriptor, InstrumentSelector registeredSelector) {
Pattern pattern = registeredSelector.instrumentNamePattern();
if (pattern == null) {
return false;
}
return pattern.matcher(descriptor.getName()).matches();
}

private static AggregationConfiguration getDefaultSpecification(InstrumentDescriptor descriptor) {
switch (descriptor.getType()) {
case COUNTER:
case UP_DOWN_COUNTER:
return CUMULATIVE_SUM;
case VALUE_RECORDER:
return DELTA_SUMMARY;
case VALUE_OBSERVER:
return DELTA_LAST_VALUE;
case SUM_OBSERVER:
case UP_DOWN_SUM_OBSERVER:
return CUMULATIVE_LAST_VALUE;
}
throw new IllegalArgumentException("Unknown descriptor type: " + descriptor.getType());
}

void addView(InstrumentSelector selector, AggregationConfiguration specification) {
configuration.put(selector, specification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ interface Batcher {
* @return the list of metrics batched in this Batcher.
*/
List<MetricData> completeCollectionCycle();

/**
* Returns whether this batcher generate "delta" style metrics. The alternative is "cumulative".
*/
boolean generatesDeltas();
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public void batch(Labels labelSet, Aggregator aggregator, boolean mappedAggregat
public List<MetricData> completeCollectionCycle() {
return Collections.emptyList();
}

@Override
public boolean generatesDeltas() {
return false;
}
}

private static final class AllLabels implements Batcher {
Expand Down Expand Up @@ -155,6 +160,75 @@ public final List<MetricData> completeCollectionCycle() {
aggregation.getDescriptorType(descriptor.getType(), descriptor.getValueType()),
points));
}

@Override
public boolean generatesDeltas() {
return delta;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AllLabels allLabels = (AllLabels) o;

if (startEpochNanos != allLabels.startEpochNanos) {
return false;
}
if (delta != allLabels.delta) {
return false;
}
if (descriptor != null
? !descriptor.equals(allLabels.descriptor)
: allLabels.descriptor != null) {
return false;
}
if (aggregation != null
? !aggregation.equals(allLabels.aggregation)
: allLabels.aggregation != null) {
return false;
}
if (resource != null ? !resource.equals(allLabels.resource) : allLabels.resource != null) {
return false;
}
if (instrumentationLibraryInfo != null
? !instrumentationLibraryInfo.equals(allLabels.instrumentationLibraryInfo)
: allLabels.instrumentationLibraryInfo != null) {
return false;
}
if (clock != null ? !clock.equals(allLabels.clock) : allLabels.clock != null) {
return false;
}
if (aggregatorFactory != null
? !aggregatorFactory.equals(allLabels.aggregatorFactory)
: allLabels.aggregatorFactory != null) {
return false;
}
return aggregatorMap != null
? aggregatorMap.equals(allLabels.aggregatorMap)
: allLabels.aggregatorMap == null;
}

@Override
public int hashCode() {
int result = descriptor != null ? descriptor.hashCode() : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you can use Objects.hashCode instead of your own null checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had IDEA generate this code. Hate to mess with generated code in general.
We should document what we want to do with generated equals/hashcode/toString implementations, so we don't have to discuss it. :)

result = 31 * result + (aggregation != null ? aggregation.hashCode() : 0);
result = 31 * result + (resource != null ? resource.hashCode() : 0);
result =
31 * result
+ (instrumentationLibraryInfo != null ? instrumentationLibraryInfo.hashCode() : 0);
result = 31 * result + (clock != null ? clock.hashCode() : 0);
result = 31 * result + (aggregatorFactory != null ? aggregatorFactory.hashCode() : 0);
result = 31 * result + (aggregatorMap != null ? aggregatorMap.hashCode() : 0);
result = 31 * result + (int) (startEpochNanos ^ (startEpochNanos >>> 32));
result = 31 * result + (delta ? 1 : 0);
return result;
}
}

private Batchers() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import io.opentelemetry.sdk.internal.MillisClock;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricProducer;
import io.opentelemetry.sdk.metrics.view.AggregationConfiguration;
import io.opentelemetry.sdk.metrics.view.InstrumentSelector;
import io.opentelemetry.sdk.resources.Resource;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -31,11 +33,12 @@ public final class MeterSdkProvider implements MeterProvider {

private final MeterSdkComponentRegistry registry;
private final MetricProducer metricProducer;
private final ViewRegistry viewRegistry = new ViewRegistry();

private MeterSdkProvider(Clock clock, Resource resource) {
this.registry =
new MeterSdkComponentRegistry(
MeterProviderSharedState.create(clock, resource), new ViewRegistry());
MeterProviderSharedState.create(clock, resource), viewRegistry);
this.metricProducer = new MetricProducerSdk(this.registry);
}

Expand Down Expand Up @@ -135,6 +138,34 @@ public MeterSdk newComponent(InstrumentationLibraryInfo instrumentationLibraryIn
}
}

/**
* Register a view with the given {@link InstrumentSelector}.
*
* <p>Example on how to register a view:
*
* <pre>{@code
* // get a handle to the MeterSdkProvider
* MeterSdkProvider meterProvider = OpenTelemetrySdk.getMeterProvider();
*
* // create a selector to select which instruments to customize:
* InstrumentSelector instrumentSelector = InstrumentSelector.newBuilder()
* .instrumentType(InstrumentType.COUNTER)
* .build();
*
* // create a specification of how you want the metrics aggregated:
* AggregationConfiguration viewSpecification =
* AggregationConfiguration.create(Aggregations.minMaxSumCount(), Temporality.DELTA);
*
* //register the view with the MeterSdkProvider
* meterProvider.registerView(instrumentSelector, viewSpecification);
* }</pre>
*
* @see AggregationConfiguration
*/
public void registerView(InstrumentSelector selector, AggregationConfiguration specification) {
viewRegistry.registerView(selector, specification);
}

private static final class MetricProducerSdk implements MetricProducer {
private final MeterSdkComponentRegistry registry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,44 @@
package io.opentelemetry.sdk.metrics;

import io.opentelemetry.sdk.metrics.view.Aggregation;
import io.opentelemetry.sdk.metrics.view.Aggregations;
import io.opentelemetry.sdk.metrics.view.AggregationConfiguration;
import io.opentelemetry.sdk.metrics.view.AggregationConfiguration.Temporality;
import io.opentelemetry.sdk.metrics.view.InstrumentSelector;

// notes:
// specify by pieces of the descriptor.
// instrument type
// instrument value type
// instrument name (wildcards allowed?)
// instrument type
// instrument name (regex) √
// instrument value type (?)
// constant labels (?)
// units (?)

// what you can choose:
// aggregation
// aggregation √
// delta vs. cumulative √
// all labels vs. a list of labels
// delta vs. cumulative

/**
* Central location for Views to be registered. Registration of a view should eventually be done via
* the {@link io.opentelemetry.sdk.metrics.MeterSdkProvider}.
*/
class ViewRegistry {

private final AggregationChooser aggregationChooser;

ViewRegistry() {
this(new AggregationChooser());
}

// VisibleForTesting
ViewRegistry(AggregationChooser aggregationChooser) {
this.aggregationChooser = aggregationChooser;
}

void registerView(InstrumentSelector selector, AggregationConfiguration specification) {
aggregationChooser.addView(selector, specification);
}

/**
* Create a new {@link io.opentelemetry.sdk.metrics.Batcher} for use in metric recording
* aggregation.
Expand All @@ -36,39 +53,17 @@ Batcher createBatcher(
MeterSharedState meterSharedState,
InstrumentDescriptor descriptor) {

Aggregation aggregation = getRegisteredAggregation(descriptor);
AggregationConfiguration specification = aggregationChooser.chooseAggregation(descriptor);

// todo: don't just use the defaults!
switch (descriptor.getType()) {
case COUNTER:
case UP_DOWN_COUNTER:
case SUM_OBSERVER:
case UP_DOWN_SUM_OBSERVER:
return Batchers.getCumulativeAllLabels(
descriptor, meterProviderSharedState, meterSharedState, aggregation);
case VALUE_RECORDER:
// TODO: Revisit the batcher used here for value observers,
// currently this does not remove duplicate records in the same cycle.
case VALUE_OBSERVER:
return Batchers.getDeltaAllLabels(
descriptor, meterProviderSharedState, meterSharedState, aggregation);
}
throw new IllegalArgumentException("Unknown descriptor type: " + descriptor.getType());
}
Aggregation aggregation = specification.aggregation();

private static Aggregation getRegisteredAggregation(InstrumentDescriptor descriptor) {
// todo look up based on fields of the descriptor.
switch (descriptor.getType()) {
case COUNTER:
case UP_DOWN_COUNTER:
return Aggregations.sum();
case VALUE_RECORDER:
return Aggregations.minMaxSumCount();
case VALUE_OBSERVER:
case SUM_OBSERVER:
case UP_DOWN_SUM_OBSERVER:
return Aggregations.lastValue();
if (Temporality.CUMULATIVE == specification.temporality()) {
return Batchers.getCumulativeAllLabels(
descriptor, meterProviderSharedState, meterSharedState, aggregation);
} else if (Temporality.DELTA == specification.temporality()) {
return Batchers.getDeltaAllLabels(
descriptor, meterProviderSharedState, meterSharedState, aggregation);
}
throw new IllegalArgumentException("Unknown descriptor type: " + descriptor.getType());
throw new IllegalStateException("unsupported Temporality: " + specification.temporality());
}
}
Loading