// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static final int DEFAULT_CONCURRENCY_LEVEL = 16;
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int concurrencyLevel) {
//   return new ConcurrentHashMap<>(HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, concurrencyLevel);
// }
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricSchema.java
// public enum Kind {
//   CUMULATIVE,
//   GAUGE,
// }


import static com.google.monitoring.metrics.MetricsUtils.DEFAULT_CONCURRENCY_LEVEL;
import static com.google.monitoring.metrics.MetricsUtils.newConcurrentHashMap;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.Striped;
import com.google.monitoring.metrics.MetricSchema.Kind;
import java.time.Instant;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
// Copyright 2017 Google LLC. 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 com.google.monitoring.metrics;



/**
 * A metric which stores {@link Distribution} values. The values are stateful and meant to be
 * updated incrementally via the {@link EventMetric#record(double, String...)} method.
 *
 * <p>This metric class is generally suitable for recording aggregations of data about a
 * quantitative aspect of an event. For example, this metric would be suitable for recording the
 * latency distribution for a request over the network.
 *
 * <p>The {@link MutableDistribution} values tracked by this metric can be reset with {@link
 * EventMetric#reset()}.
 */
public class EventMetric extends AbstractMetric<Distribution> {

  /**
   * Default {@link DistributionFitter} suitable for latency measurements.
   *
   * <p>The finite range of this fitter is from 1 to 4^16 (4294967296).
   */
  public static final DistributionFitter DEFAULT_FITTER = ExponentialFitter.create(16, 4.0, 1.0);

  private final ConcurrentHashMap<ImmutableList<String>, Instant> valueStartTimestamps =
      newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL);
  private final ConcurrentHashMap<ImmutableList<String>, MutableDistribution> values =
      newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL);

  private final DistributionFitter distributionFitter;

  /**
   * A fine-grained lock to ensure that {@code values} and {@code valueStartTimestamps} are modified
   * and read in a critical section. The initialization parameter is the concurrency level, set to
   * match the default concurrency level of {@link ConcurrentHashMap}.
   *
   * @see Striped
   */
  private final Striped<Lock> valueLocks = Striped.lock(DEFAULT_CONCURRENCY_LEVEL);

  EventMetric(
      String name,
      String description,
      String valueDisplayName,
      DistributionFitter distributionFitter,
      ImmutableSet<LabelDescriptor> labels) {
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static final int DEFAULT_CONCURRENCY_LEVEL = 16;
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricsUtils.java
// static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int concurrencyLevel) {
//   return new ConcurrentHashMap<>(HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, concurrencyLevel);
// }
// 
// Path: metrics/src/main/java/com/google/monitoring/metrics/MetricSchema.java
// public enum Kind {
//   CUMULATIVE,
//   GAUGE,
// }


// Path: metrics/src/main/java/com/google/monitoring/metrics/EventMetric.java
import static com.google.monitoring.metrics.MetricsUtils.DEFAULT_CONCURRENCY_LEVEL;
import static com.google.monitoring.metrics.MetricsUtils.newConcurrentHashMap;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.Striped;
import com.google.monitoring.metrics.MetricSchema.Kind;
import java.time.Instant;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;

// Copyright 2017 Google LLC. 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 com.google.monitoring.metrics;



/**
 * A metric which stores {@link Distribution} values. The values are stateful and meant to be
 * updated incrementally via the {@link EventMetric#record(double, String...)} method.
 *
 * <p>This metric class is generally suitable for recording aggregations of data about a
 * quantitative aspect of an event. For example, this metric would be suitable for recording the
 * latency distribution for a request over the network.
 *
 * <p>The {@link MutableDistribution} values tracked by this metric can be reset with {@link
 * EventMetric#reset()}.
 */
public class EventMetric extends AbstractMetric<Distribution> {

  /**
   * Default {@link DistributionFitter} suitable for latency measurements.
   *
   * <p>The finite range of this fitter is from 1 to 4^16 (4294967296).
   */
  public static final DistributionFitter DEFAULT_FITTER = ExponentialFitter.create(16, 4.0, 1.0);

  private final ConcurrentHashMap<ImmutableList<String>, Instant> valueStartTimestamps =
      newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL);
  private final ConcurrentHashMap<ImmutableList<String>, MutableDistribution> values =
      newConcurrentHashMap(DEFAULT_CONCURRENCY_LEVEL);

  private final DistributionFitter distributionFitter;

  /**
   * A fine-grained lock to ensure that {@code values} and {@code valueStartTimestamps} are modified
   * and read in a critical section. The initialization parameter is the concurrency level, set to
   * match the default concurrency level of {@link ConcurrentHashMap}.
   *
   * @see Striped
   */
  private final Striped<Lock> valueLocks = Striped.lock(DEFAULT_CONCURRENCY_LEVEL);

  EventMetric(
      String name,
      String description,
      String valueDisplayName,
      DistributionFitter distributionFitter,
      ImmutableSet<LabelDescriptor> labels) {
    super(name, description, valueDisplayName, Kind.CUMULATIVE, labels, Distribution.class);