<commit_before>package com.amediamanager.metrics;

import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;

import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.MetricDatum;
import com.amazonaws.services.cloudwatch.model.StandardUnit;

public class MetricAspect {

    protected final MetricBatcher metricBatcher;

    public MetricAspect(final MetricBatcher metricBatcher) {
        this.metricBatcher = metricBatcher;
    }

    @Pointcut("execution(public * com.amazonaws.services..*.*(..))")
    public final void sdkClients() {
    }

    @Around("sdkClients()")
    public final Object logMetrics(ProceedingJoinPoint pjp) throws Throwable {
        final String service = pjp.getSignature().getDeclaringType().getSimpleName();
        final String operation = pjp.getSignature().getName();
        final long startTime = System.currentTimeMillis();

        Throwable exception = null;

        try {
            return pjp.proceed();
        } catch (Exception e) {
            exception = e;
            throw e;
        } finally {
            emitMetrics(service, operation, startTime, exception);
        }
    }

    protected void emitMetrics(String service, String operation, long startTime, Throwable exception) {
        final MetricDatum latency = newDatum(service, operation, startTime)
                                        .withMetricName("Latency")
                                        .withUnit(StandardUnit.Milliseconds)
                                        .withValue((double)System.currentTimeMillis() - startTime);
        metricBatcher.addDatum("AMM", latency);

        final MetricDatum success = newDatum(service, operation, startTime)
                                        .withMetricName("Success")
                                        .withValue(exception == null ? 1.0 : 0.0)
                                        .withUnit(StandardUnit.Count);
        metricBatcher.addDatum("AMM", success);
    }

    private MetricDatum newDatum(String service, String operation, long startTime) {
        return new MetricDatum().withDimensions(new Dimension().withName("Service")
                                                               .withValue(service),
                                                new Dimension().withName("Operation")
                                                               .withValue(operation))
                                .withTimestamp(new Date(startTime));
    }

}<commit_msg>Rename 'Service' dimension to 'Svc' as CW doesn't recognize the former.<commit_after>package com.amediamanager.metrics;

import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;

import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.MetricDatum;
import com.amazonaws.services.cloudwatch.model.StandardUnit;

public class MetricAspect {

    protected final MetricBatcher metricBatcher;

    public MetricAspect(final MetricBatcher metricBatcher) {
        this.metricBatcher = metricBatcher;
    }

    @Pointcut("execution(public * com.amazonaws.services..*.*(..))")
    public final void sdkClients() {
    }

    @Around("sdkClients()")
    public final Object logMetrics(ProceedingJoinPoint pjp) throws Throwable {
        final String service = pjp.getSignature().getDeclaringType().getSimpleName();
        final String operation = pjp.getSignature().getName();
        final long startTime = System.currentTimeMillis();

        Throwable exception = null;

        try {
            return pjp.proceed();
        } catch (Exception e) {
            exception = e;
            throw e;
        } finally {
            emitMetrics(service, operation, startTime, exception);
        }
    }

    protected void emitMetrics(String service, String operation, long startTime, Throwable exception) {
        final MetricDatum latency = newDatum(service, operation, startTime)
                                        .withMetricName("Latency")
                                        .withUnit(StandardUnit.Milliseconds)
                                        .withValue((double)System.currentTimeMillis() - startTime);
        metricBatcher.addDatum("AMM", latency);

        final MetricDatum success = newDatum(service, operation, startTime)
                                        .withMetricName("Success")
                                        .withValue(exception == null ? 1.0 : 0.0)
                                        .withUnit(StandardUnit.Count);
        metricBatcher.addDatum("AMM", success);
    }

    private MetricDatum newDatum(String service, String operation, long startTime) {
        return new MetricDatum().withDimensions(new Dimension().withName("Svc")
                                                               .withValue(service),
                                                new Dimension().withName("Operation")
                                                               .withValue(operation))
                                .withTimestamp(new Date(startTime));
    }

}
{'fraction_non_alphanumeric': 0.09156261823685206, 'fraction_numerical': 0.001513431706394249, 'mean_word_length': 1.9372222222222222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1690227', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1245, 'n_words': 264}