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

org.jmxtrans.agent.influxdb.InfluxMetricConverter Maven / Gradle / Ivy

There is a newer version: 1.2.11
Show newest version
/*
 * Copyright (c) 2010-2016 the original author or authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.jmxtrans.agent.influxdb;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kristoffer Erlandsson
 */
public class InfluxMetricConverter {

    public static InfluxMetric convertToInfluxMetric(String metricName, Object value, List additionalTags, long timestamp) {
        List tagsFromMetricName = parseTags(metricName);
        List allTags = new ArrayList<>(additionalTags);
        allTags.addAll(tagsFromMetricName);
        return new InfluxMetric(parseMeasurement(metricName), allTags, value, timestamp);
    }

    private static String parseMeasurement(String metricName) {
        return metricName.split(",")[0].trim();
    }

    private static List parseTags(String metricName) {
        int startOfTags = metricName.indexOf(',');
        if (startOfTags < 0) {
            return new ArrayList<>();
        }
        return tagsFromCommaSeparatedString(metricName.substring(startOfTags + 1));
    }

    public static List tagsFromCommaSeparatedString(String s) {
        List tags = new ArrayList<>();
        if (s.trim().isEmpty()) {
            return tags;
        }
        String[] parts = s.split(",");
        for (String tagPart : parts) {
            tags.add(parseOneTag(tagPart));
        }
        return tags;
    }

    private static InfluxTag parseOneTag(String part) {
        String[] nameAndValue = part.trim().split("=");
        if (nameAndValue.length != 2) {
            throw new FailedToConvertToInfluxMetricException(
                    "Error when parsing influx tags from substring " + part + ", must be on format =,...");
        }
        InfluxTag tag = new InfluxTag(nameAndValue[0].trim(), nameAndValue[1].trim());
        return tag;
    }

    @SuppressWarnings("serial")
    public static class FailedToConvertToInfluxMetricException extends RuntimeException {

        public FailedToConvertToInfluxMetricException(String msg) {
            super(msg);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy