Skip to content

Commit

Permalink
Merge branch '1.1.x' into 1.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
shakuzen committed Mar 16, 2020
2 parents 161cff9 + f06fef7 commit 88ed01e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ public StackdriverMeterRegistry build() {
}
}

private class Batch {
//VisibleForTesting
class Batch {
private final TimeInterval interval = TimeInterval.newBuilder()
.setEndTime(Timestamp.newBuilder()
.setSeconds(clock.wallTime() / 1000)
Expand Down Expand Up @@ -385,7 +386,8 @@ private String metricType(Meter.Id id, @Nullable String statistic) {
return metricType.toString();
}

private Distribution distribution(HistogramSnapshot snapshot, boolean timeDomain) {
//VisibleForTesting
Distribution distribution(HistogramSnapshot snapshot, boolean timeDomain) {
CountAtBucket[] histogram = snapshot.histogramCounts();

// selected finite buckets (represented as a normal histogram)
Expand Down Expand Up @@ -416,7 +418,7 @@ private Distribution distribution(HistogramSnapshot snapshot, boolean timeDomain
}

// add the "+infinity" bucket, which does NOT have a corresponding bucket boundary
bucketCounts.add(snapshot.count() - truncatedSum.get());
bucketCounts.add(Math.max(0, snapshot.count() - truncatedSum.get()));

List<Double> bucketBoundaries = Arrays.stream(histogram)
.map(countAtBucket -> timeDomain ? countAtBucket.bucket(getBaseTimeUnit()) : countAtBucket.bucket())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2020 Pivotal Software, Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.stackdriver;

import com.google.api.Distribution;
import io.micrometer.core.Issue;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.distribution.CountAtBucket;
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
import io.micrometer.core.lang.Nullable;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link StackdriverMeterRegistry}
*/
class StackdriverMeterRegistryTest {

StackdriverMeterRegistry meterRegistry = new StackdriverMeterRegistry(new StackdriverConfig() {
@Override
public boolean enabled() {
return false;
}

@Override
public String projectId() {
return "doesnotmatter";
}

@Override
@Nullable
public String get(String key) {
return null;
}
}, new MockClock());

@Test
@Issue("#1325")
void distributionCountBucketsInfinityBucketIsNotNegative() {
StackdriverMeterRegistry.Batch batch = meterRegistry.new Batch();
// count is 4, but sum of bucket counts is 5 due to inconsistent snapshotting
HistogramSnapshot histogramSnapshot = new HistogramSnapshot(4, 14.7, 5, null, new CountAtBucket[]{new CountAtBucket(1, 2), new CountAtBucket(2, 5)}, null);
Distribution distribution = batch.distribution(histogramSnapshot, false);
List<Long> bucketCountsList = distribution.getBucketCountsList();
assertThat(bucketCountsList.get(bucketCountsList.size() - 1)).isNotNegative();
}
}

0 comments on commit 88ed01e

Please sign in to comment.