All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zipkin2.reporter.InMemoryReporterMetrics Maven / Gradle / Ivy

/*
 * Copyright 2016-2024 The OpenZipkin Authors
 *
 * 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 zipkin2.reporter;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public final class InMemoryReporterMetrics implements ReporterMetrics {
  enum MetricKey {
    messages,
    messageBytes,
    spans,
    spanBytes,
    spansDropped,
    spansPending,
    spanBytesPending;
  }

  private final ConcurrentHashMap metrics =
      new ConcurrentHashMap();
  private final ConcurrentHashMap, AtomicLong> messagesDropped =
      new ConcurrentHashMap, AtomicLong>();

  @Override public void incrementMessages() {
    increment(MetricKey.messages, 1);
  }

  public long messages() {
    return get(MetricKey.messages);
  }

  @Override public void incrementMessagesDropped(Throwable cause) {
    increment(messagesDropped, cause.getClass(), 1);
  }

  public Map, Long> messagesDroppedByCause() {
    Map, Long> result =
      new LinkedHashMap, Long>(messagesDropped.size());
    for (Map.Entry, AtomicLong> kv : messagesDropped.entrySet()) {
      result.put(kv.getKey(), kv.getValue().longValue());
    }
    return result;
  }

  public long messagesDropped() {
    long result = 0L;
    for (AtomicLong count : messagesDropped.values()) {
      result += count.longValue();
    }
    return result;
  }

  @Override public void incrementMessageBytes(int quantity) {
    increment(MetricKey.messageBytes, quantity);
  }

  public long messageBytes() {
    return get(MetricKey.messageBytes);
  }

  @Override public void incrementSpans(int quantity) {
    increment(MetricKey.spans, quantity);
  }

  public long spans() {
    return get(MetricKey.spans);
  }

  @Override public void incrementSpanBytes(int quantity) {
    increment(MetricKey.spanBytes, quantity);
  }

  public long spanBytes() {
    return get(MetricKey.spanBytes);
  }

  @Override
  public void incrementSpansDropped(int quantity) {
    increment(MetricKey.spansDropped, quantity);
  }

  public long spansDropped() {
    return get(MetricKey.spansDropped);
  }

  @Override public void updateQueuedSpans(int update) {
    update(MetricKey.spansPending, update);
  }

  public long queuedSpans() {
    return get(MetricKey.spansPending);
  }

  @Override public void updateQueuedBytes(int update) {
    update(MetricKey.spanBytesPending, update);
  }

  public long queuedBytes() {
    return get(MetricKey.spanBytesPending);
  }

  public void clear() {
    metrics.clear();
  }

  private long get(MetricKey key) {
    AtomicLong atomic = metrics.get(key);
    return atomic == null ? 0 : atomic.get();
  }

  private void increment(MetricKey key, int quantity) {
    increment(metrics, key, quantity);
  }

  static  void increment(ConcurrentHashMap metrics, K key, int quantity) {
    if (quantity == 0) return;
    while (true) {
      AtomicLong metric = metrics.get(key);
      if (metric == null) {
        metric = metrics.putIfAbsent(key, new AtomicLong(quantity));
        if (metric == null) return; // won race creating the entry
      }

      while (true) {
        long oldValue = metric.get();
        long update = oldValue + quantity;
        if (metric.compareAndSet(oldValue, update)) return; // won race updating
      }
    }
  }

  private void update(MetricKey key, int update) {
    AtomicLong metric = metrics.get(key);
    if (metric == null) {
      metric = metrics.putIfAbsent(key, new AtomicLong(update));
      if (metric == null) return; // won race creating the entry
    }
    metric.set(update);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy