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

com.github.dikhan.dropwizard.headermetric.TraceHeadersBundleConfigHelper Maven / Gradle / Ivy

Go to download

Create metrics and send it to the preferable reporter if request header matches any of the headers configured to be measured.

There is a newer version: 3.0.0
Show newest version
package com.github.dikhan.dropwizard.headermetric;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

import io.dropwizard.Configuration;

/**
 * A helper class to be able to reuse the code within the {@link TraceHeadersBundle} and
 * {@link com.github.dikhan.dropwizard.headermetric.filters.HeaderMetricFilter} classes
 *
 * @author Daniel I. Khan Ramiro
 */
public class TraceHeadersBundleConfigHelper {

    private final Logger log = LoggerFactory.getLogger(TraceHeadersBundleConfigHelper.class);

    private final T configuration;
    private final TraceHeadersBundle traceHeadersBundle;
    private final MultivaluedMap headersAndValuesToLookUp;

    public TraceHeadersBundleConfigHelper(T configuration, TraceHeadersBundle traceHeadersBundle) {
        this.configuration = configuration;
        this.traceHeadersBundle = traceHeadersBundle;
        this.headersAndValuesToLookUp = createMultivaluedMapFromHeadersToTraceJson();
    }

    public String getHeaderMetricName(String endPointHit, String header, String headerValue) {
        String metricPrefix = traceHeadersBundle.getTraceHeadersBundleConfiguration(configuration).getMetricPrefix();
        if (StringUtils.isBlank(metricPrefix)) {
            header = String.format("%s-%s-%s", endPointHit, header, headerValue);
        } else {
            header = String.format("%s-%s-%s-%s", metricPrefix, endPointHit, header, headerValue);
        }
        return header;
    }

    public MultivaluedMap getHeadersAndValuesToLookUp() {
        return headersAndValuesToLookUp;
    }

    private MultivaluedMap createMultivaluedMapFromHeadersToTraceJson() {
        log.info("Unmarshalling traceHeaders[headersToTraceJson] into a multivalued map");
        MultivaluedHashMap headersAndValuesToLookUp = new MultivaluedHashMap<>();
        JsonNode jsonNode = traceHeadersBundle.getTraceHeadersBundleConfiguration(configuration)
                .getHeadersToTraceJsonNode();
        jsonNode.fields().forEachRemaining(entry -> {
            String key = entry.getKey().toLowerCase();
            if (entry.getValue().isArray()) {
                log.info("Key[" + entry.getKey() + "] has an array of values: " + entry.getValue());
                entry.getValue().elements().forEachRemaining(jsonElement -> {
                    String value = jsonElement.asText().toLowerCase();
                    addToMap(headersAndValuesToLookUp, key, value);
                });
            } else {
                String value = entry.getValue().asText().toLowerCase();
                addToMap(headersAndValuesToLookUp, key, value);
            }
        });
        return headersAndValuesToLookUp;
    }

    private void addToMap(MultivaluedHashMap headersAndValuesToLookUp, String key, String value) {
        headersAndValuesToLookUp.add(key, value);
        log.info("Added new header from headersToTraceJson property Json [" + key + ": " + value + "]");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy