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

io.liftwizard.logging.metrics.structured.log4j.StructuredLog4jReporter Maven / Gradle / Ivy

There is a newer version: 2.1.19
Show newest version
/*
 * Copyright 2024 Craig Motlin
 *
 * 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 io.liftwizard.logging.metrics.structured.log4j;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Counting;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metered;
import com.codahale.metrics.MetricAttribute;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import io.liftwizard.logging.metrics.structured.log4j.proxy.AbstractLoggerProxy;

/**
 * @see com.codahale.metrics.Slf4jReporter
 */
public class StructuredLog4jReporter
        extends ScheduledReporter
{
    private final AbstractLoggerProxy loggerProxy;
    private final String              prefix;

    public StructuredLog4jReporter(
            MetricRegistry registry,
            AbstractLoggerProxy loggerProxy,
            String prefix,
            TimeUnit rateUnit,
            TimeUnit durationUnit,
            MetricFilter filter,
            ScheduledExecutorService executor,
            boolean shutdownExecutorOnStop,
            Set disabledMetricAttributes)
    {
        super(
                registry,
                "structured-logger-reporter",
                filter,
                rateUnit,
                durationUnit,
                executor,
                shutdownExecutorOnStop,
                disabledMetricAttributes);
        this.loggerProxy = loggerProxy;
        this.prefix      = prefix;
    }

    /**
     * Returns a new {@link Builder} for {@link StructuredLog4jReporter}.
     *
     * @param registry the registry to report
     * @return a {@link Builder} instance for a {@link StructuredLog4jReporter}
     */
    public static Builder forRegistry(MetricRegistry registry)
    {
        return new Builder(registry);
    }

    @Override
    public void report(
            SortedMap gauges,
            SortedMap counters,
            SortedMap histograms,
            SortedMap meters,
            SortedMap timers)
    {
        if (!this.loggerProxy.isEnabled())
        {
            return;
        }

        gauges.forEach(this::logGauge);
        counters.forEach(this::logCounter);
        histograms.forEach(this::logHistogram);
        meters.forEach(this::logMeter);
        timers.forEach(this::logTimer);
    }

    private void logTimer(String name, Timer timer)
    {
        Map map = new LinkedHashMap<>();
        map.put("metric_type", "TIMER");
        map.put("metric_name", this.prefix(name));
        Snapshot snapshot = timer.getSnapshot();
        this.appendCountIfEnabled(map, timer);
        this.appendLongDurationIfEnabled(map, MetricAttribute.MIN, snapshot::getMin);
        this.appendLongDurationIfEnabled(map, MetricAttribute.MAX, snapshot::getMax);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.MEAN, snapshot::getMean);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.STDDEV, snapshot::getStdDev);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.P50, snapshot::getMedian);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.P75, snapshot::get75thPercentile);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.P95, snapshot::get95thPercentile);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.P98, snapshot::get98thPercentile);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.P99, snapshot::get99thPercentile);
        this.appendDoubleDurationIfEnabled(map, MetricAttribute.P999, snapshot::get999thPercentile);
        this.appendMetered(map, timer);
        map.put("metric_rate_unit", this.getRateUnit());
        map.put("metric_duration_unit", this.getDurationUnit());
        this.log(map);
    }

    private void logMeter(String name, Metered meter)
    {
        Map map = new LinkedHashMap<>();
        map.put("metric_type", "METER");
        map.put("metric_name", this.prefix(name));
        this.appendCountIfEnabled(map, meter);
        this.appendMetered(map, meter);
        map.put("metric_rate_unit", this.getRateUnit());
        this.log(map);
    }

    private void logHistogram(String name, Histogram histogram)
    {
        Map map = new LinkedHashMap<>();
        map.put("metric_type", "HISTOGRAM");
        map.put("metric_name", this.prefix(name));
        Snapshot snapshot = histogram.getSnapshot();
        this.appendCountIfEnabled(map, histogram);
        this.appendLongIfEnabled(map, MetricAttribute.MIN, snapshot::getMin);
        this.appendLongIfEnabled(map, MetricAttribute.MAX, snapshot::getMax);
        this.appendDoubleIfEnabled(map, MetricAttribute.MEAN, snapshot::getMean);
        this.appendDoubleIfEnabled(map, MetricAttribute.STDDEV, snapshot::getStdDev);
        this.appendDoubleIfEnabled(map, MetricAttribute.P50, snapshot::getMedian);
        this.appendDoubleIfEnabled(map, MetricAttribute.P75, snapshot::get75thPercentile);
        this.appendDoubleIfEnabled(map, MetricAttribute.P95, snapshot::get95thPercentile);
        this.appendDoubleIfEnabled(map, MetricAttribute.P98, snapshot::get98thPercentile);
        this.appendDoubleIfEnabled(map, MetricAttribute.P99, snapshot::get99thPercentile);
        this.appendDoubleIfEnabled(map, MetricAttribute.P999, snapshot::get999thPercentile);
        this.log(map);
    }

    private void logCounter(String name, Counter counter)
    {
        Map map = new LinkedHashMap<>();
        map.put("metric_type", "COUNTER");
        map.put("metric_name", this.prefix(name));
        map.put(MetricAttribute.COUNT.getCode(), counter.getCount());
        this.log(map);
    }

    private void logGauge(String name, Gauge gauge)
    {
        Object value = gauge.getValue();
        if (!(value instanceof Number))
        {
            return;
        }

        Map map = new LinkedHashMap<>();
        map.put("metric_type", "GAUGE");
        map.put("metric_name", this.prefix(name));
        map.put("metric_value", value);
        this.log(map);
    }

    private void appendLongDurationIfEnabled(
            Map map,
            MetricAttribute metricAttribute,
            Supplier durationSupplier)
    {
        if (!this.getDisabledMetricAttributes().contains(metricAttribute))
        {
            map.put(metricAttribute.getCode(), this.convertDuration(durationSupplier.get()));
        }
    }

    private void appendDoubleDurationIfEnabled(
            Map map,
            MetricAttribute metricAttribute,
            Supplier durationSupplier)
    {
        if (!this.getDisabledMetricAttributes().contains(metricAttribute))
        {
            map.put(metricAttribute.getCode(), this.convertDuration(durationSupplier.get()));
        }
    }

    private void appendLongIfEnabled(
            Map map,
            MetricAttribute metricAttribute,
            Supplier valueSupplier)
    {
        if (!this.getDisabledMetricAttributes().contains(metricAttribute))
        {
            map.put(metricAttribute.getCode(), valueSupplier.get());
        }
    }

    private void appendDoubleIfEnabled(
            Map map,
            MetricAttribute metricAttribute,
            Supplier valueSupplier)
    {
        if (!this.getDisabledMetricAttributes().contains(metricAttribute))
        {
            map.put(metricAttribute.getCode(), valueSupplier.get());
        }
    }

    private void appendCountIfEnabled(Map map, Counting counting)
    {
        if (!this.getDisabledMetricAttributes().contains(MetricAttribute.COUNT))
        {
            map.put(MetricAttribute.COUNT.getCode(), counting.getCount());
        }
    }

    private void appendMetered(Map map, Metered meter)
    {
        this.appendRateIfEnabled(map, MetricAttribute.M1_RATE, meter::getOneMinuteRate);
        this.appendRateIfEnabled(map, MetricAttribute.M5_RATE, meter::getFiveMinuteRate);
        this.appendRateIfEnabled(map, MetricAttribute.M15_RATE, meter::getFifteenMinuteRate);
        this.appendRateIfEnabled(map, MetricAttribute.MEAN_RATE, meter::getMeanRate);
    }

    private void appendRateIfEnabled(
            Map map,
            MetricAttribute metricAttribute,
            Supplier rateSupplier)
    {
        if (!this.getDisabledMetricAttributes().contains(metricAttribute))
        {
            map.put(metricAttribute.getCode(), this.convertRate(rateSupplier.get()));
        }
    }

    @Override
    protected String getRateUnit()
    {
        return "events/" + super.getRateUnit();
    }

    private String prefix(String... components)
    {
        return MetricRegistry.name(this.prefix, components);
    }

    private void log(Map map)
    {
        this.loggerProxy.log(map);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy