mirror of
https://github.com/google/nomulus.git
synced 2025-05-15 08:57:12 +02:00
Add StackDriver implementation, in monitoring/metrics package
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=130287319
This commit is contained in:
parent
57ec8b3ae3
commit
64abebec82
21 changed files with 1476 additions and 0 deletions
97
java/google/registry/monitoring/metrics/Counter.java
Normal file
97
java/google/registry/monitoring/metrics/Counter.java
Normal file
|
@ -0,0 +1,97 @@
|
|||
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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 google.registry.monitoring.metrics;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.util.concurrent.AtomicLongMap;
|
||||
import google.registry.monitoring.metrics.MetricSchema.Kind;
|
||||
import java.util.Map.Entry;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import org.joda.time.Instant;
|
||||
|
||||
/**
|
||||
* A metric which stores Long values. It is stateful and can be changed in increments.
|
||||
*
|
||||
* <p>This metric is generally suitable for counters, such as requests served or errors generated.
|
||||
*/
|
||||
@ThreadSafe
|
||||
public final class Counter extends AbstractMetric<Long>
|
||||
implements SettableMetric<Long>, IncrementableMetric {
|
||||
|
||||
private static final String LABEL_COUNT_ERROR =
|
||||
"The count of labelValues must be equal to the underlying "
|
||||
+ "MetricDescriptor's count of labels.";
|
||||
|
||||
private final AtomicLongMap<ImmutableList<String>> values = AtomicLongMap.create();
|
||||
|
||||
Counter(
|
||||
String name,
|
||||
String description,
|
||||
String valueDisplayName,
|
||||
ImmutableSet<LabelDescriptor> labels) {
|
||||
super(name, description, valueDisplayName, Kind.CUMULATIVE, labels, Long.class);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void incrementBy(Number offset, ImmutableList<String> labelValues) {
|
||||
values.addAndGet(labelValues, offset.longValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void incrementBy(long offset, String... labelValues) {
|
||||
checkArgument(labelValues.length == this.getMetricSchema().labels().size(), LABEL_COUNT_ERROR);
|
||||
|
||||
incrementBy(offset, ImmutableList.copyOf(labelValues));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a snapshot of the metric's values. The timestamp of each {@link MetricPoint} will be
|
||||
* the last modification time for that tuple of label values.
|
||||
*/
|
||||
@Override
|
||||
public final ImmutableList<MetricPoint<Long>> getTimestampedValues() {
|
||||
return getTimestampedValues(Instant.now());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int getCardinality() {
|
||||
return values.size();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
final ImmutableList<MetricPoint<Long>> getTimestampedValues(Instant timestamp) {
|
||||
ImmutableList.Builder<MetricPoint<Long>> timestampedValues = new ImmutableList.Builder<>();
|
||||
for (Entry<ImmutableList<String>, Long> entry : values.asMap().entrySet()) {
|
||||
timestampedValues.add(MetricPoint.create(this, entry.getKey(), timestamp, entry.getValue()));
|
||||
}
|
||||
return timestampedValues.build();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
final void set(Long value, ImmutableList<String> labelValues) {
|
||||
this.values.put(labelValues, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void set(Long value, String... labelValues) {
|
||||
checkArgument(labelValues.length == this.getMetricSchema().labels().size(), LABEL_COUNT_ERROR);
|
||||
|
||||
set(value, ImmutableList.copyOf(labelValues));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue