Search in sources :

Example 1 with AttributesBuilder

use of io.opentelemetry.api.common.AttributesBuilder in project honeycomb-opentelemetry-java by honeycombio.

the class DistroMetadataResourceProvider method createResource.

@Override
public Resource createResource(ConfigProperties config) {
    AttributesBuilder attributesBuilder = Attributes.builder();
    DistroMetadata.getMetadata().forEach(attributesBuilder::put);
    return Resource.create(attributesBuilder.build());
}
Also used : AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

Example 2 with AttributesBuilder

use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.

the class LambdaResource method buildResource.

// Visible for testing
static Resource buildResource(Map<String, String> environmentVariables) {
    String region = environmentVariables.getOrDefault("AWS_REGION", "");
    String functionName = environmentVariables.getOrDefault("AWS_LAMBDA_FUNCTION_NAME", "");
    String functionVersion = environmentVariables.getOrDefault("AWS_LAMBDA_FUNCTION_VERSION", "");
    if (!isLambda(functionName, functionVersion)) {
        return Resource.empty();
    }
    AttributesBuilder builder = Attributes.builder().put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.AWS);
    builder.put(ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.AWS_LAMBDA);
    if (!region.isEmpty()) {
        builder.put(ResourceAttributes.CLOUD_REGION, region);
    }
    if (!functionName.isEmpty()) {
        builder.put(ResourceAttributes.FAAS_NAME, functionName);
    }
    if (!functionVersion.isEmpty()) {
        builder.put(ResourceAttributes.FAAS_VERSION, functionVersion);
    }
    return Resource.create(builder.build(), ResourceAttributes.SCHEMA_URL);
}
Also used : AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

Example 3 with AttributesBuilder

use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.

the class Ec2Resource method buildResource.

// Visible for testing
static Resource buildResource(String endpoint) {
    String urlBase = "http://" + endpoint;
    URL identityDocumentUrl;
    URL hostnameUrl;
    URL tokenUrl;
    try {
        identityDocumentUrl = new URL(urlBase + "/latest/dynamic/instance-identity/document");
        hostnameUrl = new URL(urlBase + "/latest/meta-data/hostname");
        tokenUrl = new URL(urlBase + "/latest/api/token");
    } catch (MalformedURLException e) {
        // Can only happen when overriding the endpoint in testing so just throw.
        throw new IllegalArgumentException("Illegal endpoint: " + endpoint, e);
    }
    String token = fetchToken(tokenUrl);
    // If token is empty, either IMDSv2 isn't enabled or an unexpected failure happened. We can
    // still get data if IMDSv1 is enabled.
    String identity = fetchIdentity(identityDocumentUrl, token);
    if (identity.isEmpty()) {
        // If no identity document, assume we are not actually running on EC2.
        return Resource.empty();
    }
    String hostname = fetchHostname(hostnameUrl, token);
    AttributesBuilder attrBuilders = Attributes.builder();
    attrBuilders.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.AWS);
    attrBuilders.put(ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.AWS_EC2);
    try (JsonParser parser = JSON_FACTORY.createParser(identity)) {
        parser.nextToken();
        if (!parser.isExpectedStartObjectToken()) {
            throw new IOException("Invalid JSON:" + identity);
        }
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String value = parser.nextTextValue();
            switch(parser.getCurrentName()) {
                case "instanceId":
                    attrBuilders.put(ResourceAttributes.HOST_ID, value);
                    break;
                case "availabilityZone":
                    attrBuilders.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, value);
                    break;
                case "instanceType":
                    attrBuilders.put(ResourceAttributes.HOST_TYPE, value);
                    break;
                case "imageId":
                    attrBuilders.put(ResourceAttributes.HOST_IMAGE_ID, value);
                    break;
                case "accountId":
                    attrBuilders.put(ResourceAttributes.CLOUD_ACCOUNT_ID, value);
                    break;
                case "region":
                    attrBuilders.put(ResourceAttributes.CLOUD_REGION, value);
                    break;
                default:
                    parser.skipChildren();
            }
        }
    } catch (IOException e) {
        logger.log(Level.WARNING, "Could not parse identity document, resource not filled.", e);
        return Resource.empty();
    }
    attrBuilders.put(ResourceAttributes.HOST_NAME, hostname);
    return Resource.create(attrBuilders.build(), ResourceAttributes.SCHEMA_URL);
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) URL(java.net.URL) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 4 with AttributesBuilder

use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.

the class EcsResource method buildResource.

// Visible for testing
static Resource buildResource(Map<String, String> sysEnv, DockerHelper dockerHelper) {
    if (!isOnEcs(sysEnv)) {
        return Resource.empty();
    }
    AttributesBuilder attrBuilders = Attributes.builder();
    attrBuilders.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.AWS);
    attrBuilders.put(ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.AWS_ECS);
    try {
        String hostName = InetAddress.getLocalHost().getHostName();
        attrBuilders.put(ResourceAttributes.CONTAINER_NAME, hostName);
    } catch (UnknownHostException e) {
        logger.log(Level.WARNING, "Could not get docker container name from hostname.", e);
    }
    String containerId = dockerHelper.getContainerId();
    if (containerId != null && !containerId.isEmpty()) {
        attrBuilders.put(ResourceAttributes.CONTAINER_ID, containerId);
    }
    return Resource.create(attrBuilders.build(), ResourceAttributes.SCHEMA_URL);
}
Also used : UnknownHostException(java.net.UnknownHostException) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

Example 5 with AttributesBuilder

use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.

the class BeanstalkResource method buildResource.

// Visible for testing
static Resource buildResource(String configPath) {
    File configFile = new File(configPath);
    if (!configFile.exists()) {
        return Resource.empty();
    }
    AttributesBuilder attrBuilders = Attributes.builder();
    try (JsonParser parser = JSON_FACTORY.createParser(configFile)) {
        parser.nextToken();
        if (!parser.isExpectedStartObjectToken()) {
            logger.log(Level.WARNING, "Invalid Beanstalk config: ", configPath);
            return Resource.create(attrBuilders.build(), ResourceAttributes.SCHEMA_URL);
        }
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            parser.nextValue();
            String value = parser.getText();
            switch(parser.getCurrentName()) {
                case DEVELOPMENT_ID:
                    attrBuilders.put(ResourceAttributes.SERVICE_INSTANCE_ID, value);
                    break;
                case VERSION_LABEL:
                    attrBuilders.put(ResourceAttributes.SERVICE_VERSION, value);
                    break;
                case ENVIRONMENT_NAME:
                    attrBuilders.put(ResourceAttributes.SERVICE_NAMESPACE, value);
                    break;
                default:
                    parser.skipChildren();
            }
        }
    } catch (IOException e) {
        logger.log(Level.WARNING, "Could not parse Beanstalk config.", e);
        return Resource.empty();
    }
    attrBuilders.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.AWS);
    attrBuilders.put(ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.AWS_ELASTIC_BEANSTALK);
    return Resource.create(attrBuilders.build(), ResourceAttributes.SCHEMA_URL);
}
Also used : IOException(java.io.IOException) File(java.io.File) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)83 Test (org.junit.jupiter.api.Test)39 HashMap (java.util.HashMap)35 Map (java.util.Map)31 Context (io.opentelemetry.context.Context)25 Test (org.junit.Test)7 Attributes (io.opentelemetry.api.common.Attributes)6 LogBuilder (io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)5 AttributeKey (io.opentelemetry.api.common.AttributeKey)4 InetSocketAddress (java.net.InetSocketAddress)4 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 Level (ch.qos.logback.classic.Level)1 ThrowableProxy (ch.qos.logback.classic.spi.ThrowableProxy)1