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

com.catchpoint.trace.lambda.metric.handler.plugin.MetricLambdaHandlerPlugin Maven / Gradle / Ivy

package com.catchpoint.trace.lambda.metric.handler.plugin;

import com.catchpoint.trace.common.instance.InstanceDiscovery;
import com.catchpoint.trace.common.report.MonitoringDataReporter;
import com.catchpoint.trace.common.sample.NoArgSampler;
import com.catchpoint.trace.common.sample.Sampler;
import com.catchpoint.trace.common.util.PropertyUtils;
import com.catchpoint.trace.common.util.StringUtils;
import com.catchpoint.trace.lambda.core.LambdaTags;
import com.catchpoint.trace.lambda.core.handler.LambdaContext;
import com.catchpoint.trace.lambda.core.handler.LambdaHandler;
import com.catchpoint.trace.lambda.core.handler.LambdaHandlerConfig;
import com.catchpoint.trace.lambda.core.handler.plugin.LambdaHandlerPlugin;
import com.catchpoint.trace.lambda.core.report.InvocationAwareMonitoringDataReporter;
import com.catchpoint.trace.lambda.core.util.LambdaUtils;
import com.catchpoint.trace.lambda.metric.processor.MetricProcessor;
import com.catchpoint.trace.metric.MetricSupport;
import com.catchpoint.trace.metric.model.Metric;
import com.catchpoint.trace.metric.model.MutableMetric;

import java.util.Collection;
import java.util.List;

/**
 * @author serkan
 */
public class MetricLambdaHandlerPlugin implements LambdaHandlerPlugin {

    private final boolean metricDisabled =
            PropertyUtils.getBooleanProperty("catchpoint.lambda.metric.disable", true);

    protected final List metricProcessors =
            InstanceDiscovery.instancesOf(MetricProcessor.class);
    protected volatile String traceId;
    protected volatile String transactionId;
    protected volatile String spanId;

    @Override
    public String name() {
        return "metric";
    }

    @Override
    public boolean isEnabled() {
        return !metricDisabled;
    }

    @Override
    public void onInstantiate(LambdaHandler lambdaHandler, LambdaHandlerConfig config,
                              long instantiationTimestamp) {
        MetricSupport.mark();
    }

    @Override
    public void onStart(LambdaHandler lambdaHandler, LambdaContext lambdaContext) {
        traceId = MetricSupport.getActiveTraceId();
        transactionId = MetricSupport.getActiveTransactionId();
        if (StringUtils.isNullOrEmpty(transactionId)) {
            transactionId = lambdaContext.getAwsRequestId();
        }
        spanId = MetricSupport.getActiveSpanId();

        // If it is already marked, this will be ignored
        MetricSupport.mark();

        MonitoringDataReporter metricReporter = MetricSupport.getMetricReporter();
        if (metricReporter instanceof InvocationAwareMonitoringDataReporter) {
            ((InvocationAwareMonitoringDataReporter) metricReporter).onInvocationStart(lambdaContext);
        }
    }

    @Override
    public void onInitExit(LambdaHandler lambdaHandler, LambdaHandlerConfig config) {
        MetricSupport.unmark();
    }

    @Override
    public void onTimeout(LambdaHandler lambdaHandler, Object request, LambdaContext lambdaContext,
                          long timeout, long passed) {
        reportMetrics(lambdaHandler, lambdaContext);
    }

    @Override
    public void onFinish(LambdaHandler lambdaHandler, LambdaContext lambdaContext) {
        try {
            reportMetrics(lambdaHandler, lambdaContext);
        } finally {
            MetricSupport.unmark();
        }
    }

    protected void reportMetrics(LambdaHandler lambdaHandler, LambdaContext lambdaContext) {
        boolean collectMetrics = true;
        Sampler sampler = MetricSupport.getMetricSampler();
        if (sampler != null) {
            if (sampler instanceof NoArgSampler) {
                collectMetrics = ((NoArgSampler) sampler).isSampled();
            } else {
                collectMetrics = sampler.isSampled(null);
            }
        }
        if (collectMetrics) {
            Collection providedMetrics = MetricSupport.collectProvidedMetrics();
            if (providedMetrics != null) {
                for (Metric providedMetric : providedMetrics) {
                    if (providedMetric instanceof MutableMetric) {
                        ((MutableMetric) providedMetric).setTag(LambdaTags.REGION, LambdaUtils.getRegion());
                        if (StringUtils.isNullOrEmpty(providedMetric.getTraceId())) {
                            ((MutableMetric) providedMetric).setTraceId(traceId);
                        }
                        if (StringUtils.isNullOrEmpty(providedMetric.getTransactionId())) {
                            ((MutableMetric) providedMetric).setTransactionId(transactionId);
                        }
                        if (StringUtils.isNullOrEmpty(providedMetric.getSpanId())) {
                            ((MutableMetric) providedMetric).setSpanId(spanId);
                        }
                    }
                    for (MetricProcessor metricProcessor : metricProcessors) {
                        metricProcessor.processMetric(lambdaContext, providedMetric);
                    }
                }
            }
            MetricSupport.reportMetrics(providedMetrics);
        }
        MonitoringDataReporter metricReporter = MetricSupport.getMetricReporter();
        if (metricReporter instanceof InvocationAwareMonitoringDataReporter) {
            ((InvocationAwareMonitoringDataReporter) metricReporter).onInvocationEnd(lambdaContext);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy