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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations