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

com.signalfx.metrics.SourceNameHelper Maven / Gradle / Ivy

There is a newer version: 1.0.43
Show newest version
package com.signalfx.metrics;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import com.signalfx.shaded.apache.commons.io.IOUtils;

import com.signalfx.metrics.aws.AWSInstanceInfo;

public final class SourceNameHelper {
    private SourceNameHelper() {
    }

    public static String getDefaultSourceName() {
        String sourceName = System.getProperty("com.signalfx.sourceName");
        if (sourceName != null && !sourceName.isEmpty()) {
            return sourceName;
        }
        sourceName = System.getenv("SIGNALFX_SOURCE_NAME");
        if (sourceName != null && !sourceName.isEmpty()) {
            return sourceName;
        }
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
        }
        sourceName = System.getenv("COMPUTERNAME");
        if (sourceName != null && !sourceName.isEmpty()) {
            return sourceName;
        }
        sourceName = System.getenv("HOSTNAME");
        if (sourceName != null && !sourceName.isEmpty()) {
            return sourceName;
        }
        throw new RuntimeException(
                "Unable to find a default source name.  Please set one with usingDefaultSource()");
    }

    /**
     * A helper to get the AWS instance ID.  Useful as an alternative default source name.
     * @return AWS instance ID
     * @throws IOException If there are any issues reading the local AWS endpoint.  For example,
     *                     if the host is not on AWS.
     * @deprecated Use {@link AWSInstanceInfo#get()} and set as a dimension on a metric or as an
     *             unique dimension on a SignalFxReporter
     */

    @Deprecated
    public static String getAwsInstanceId() throws IOException {
        return IOUtils.toString(new URL("http://169.254.169.254/latest/meta-data/instance-id"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy