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);
}
use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.
the class ProcessResource method doBuildResource.
@IgnoreJRERequirement
private static Resource doBuildResource() {
AttributesBuilder attributes = Attributes.builder();
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
long pid = ProcessPid.getPid();
if (pid >= 0) {
attributes.put(ResourceAttributes.PROCESS_PID, pid);
}
String javaHome = null;
String osName = null;
try {
javaHome = System.getProperty("java.home");
osName = System.getProperty("os.name");
} catch (SecurityException e) {
// Ignore
}
if (javaHome != null) {
StringBuilder executablePath = new StringBuilder(javaHome);
executablePath.append(File.pathSeparatorChar).append("bin").append(File.pathSeparatorChar).append("java");
if (osName != null && osName.toLowerCase().startsWith("windows")) {
executablePath.append(".exe");
}
attributes.put(ResourceAttributes.PROCESS_EXECUTABLE_PATH, executablePath.toString());
StringBuilder commandLine = new StringBuilder(executablePath);
for (String arg : runtime.getInputArguments()) {
commandLine.append(' ').append(arg);
}
attributes.put(ResourceAttributes.PROCESS_COMMAND_LINE, commandLine.toString());
}
return Resource.create(attributes.build(), ResourceAttributes.SCHEMA_URL);
}
use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.
the class SdkSpan method recordException.
@Override
public ReadWriteSpan recordException(Throwable exception, Attributes additionalAttributes) {
if (exception == null) {
return this;
}
if (additionalAttributes == null) {
additionalAttributes = Attributes.empty();
}
long timestampNanos = clock.now();
AttributesBuilder attributes = Attributes.builder();
attributes.put(SemanticAttributes.EXCEPTION_TYPE, exception.getClass().getCanonicalName());
if (exception.getMessage() != null) {
attributes.put(SemanticAttributes.EXCEPTION_MESSAGE, exception.getMessage());
}
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
attributes.put(SemanticAttributes.EXCEPTION_STACKTRACE, writer.toString());
attributes.putAll(additionalAttributes);
addEvent(SemanticAttributes.EXCEPTION_EVENT_NAME, attributes.build(), timestampNanos, TimeUnit.NANOSECONDS);
return this;
}
use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.
the class SpanShim method convertToAttributes.
private static Attributes convertToAttributes(Map<String, ?> fields, boolean isError, boolean isRecordingException) {
AttributesBuilder attributesBuilder = Attributes.builder();
for (Map.Entry<String, ?> entry : fields.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// TODO - verify null values are NOT allowed.
if (value == null) {
continue;
}
if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long) {
attributesBuilder.put(longKey(key), ((Number) value).longValue());
} else if (value instanceof Float || value instanceof Double) {
attributesBuilder.put(doubleKey(key), ((Number) value).doubleValue());
} else if (value instanceof Boolean) {
attributesBuilder.put(booleanKey(key), (Boolean) value);
} else {
AttributeKey<String> attributeKey = null;
if (isError && !isRecordingException) {
if (key.equals(Fields.ERROR_KIND)) {
attributeKey = SemanticAttributes.EXCEPTION_TYPE;
} else if (key.equals(Fields.MESSAGE)) {
attributeKey = SemanticAttributes.EXCEPTION_MESSAGE;
} else if (key.equals(Fields.STACK)) {
attributeKey = SemanticAttributes.EXCEPTION_STACKTRACE;
}
}
if (isRecordingException && key.equals(Fields.ERROR_OBJECT)) {
// Already recorded as the exception itself so don't add as attribute.
continue;
}
if (attributeKey == null) {
attributeKey = stringKey(key);
}
attributesBuilder.put(attributeKey, value.toString());
}
}
return attributesBuilder.build();
}
use of io.opentelemetry.api.common.AttributesBuilder in project opentelemetry-java by open-telemetry.
the class AttributeUtil method applyAttributesLimit.
/**
* Apply the {@code countLimit} and {@code lengthLimit} to the attributes.
*
* <p>If all attributes fall within the limits, return as is. Else, return an attributes instance
* with the limits applied. {@code countLimit} limits the number of unique attribute keys. {@code
* lengthLimit} limits the length of attribute string and string list values.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Attributes applyAttributesLimit(Attributes attributes, int countLimit, int lengthLimit) {
if (attributes.isEmpty() || attributes.size() <= countLimit) {
if (lengthLimit == Integer.MAX_VALUE) {
return attributes;
}
boolean allValidLength = allMatch(attributes.asMap().values(), value -> isValidLength(value, lengthLimit));
if (allValidLength) {
return attributes;
}
}
AttributesBuilder result = Attributes.builder();
int i = 0;
for (Map.Entry<AttributeKey<?>, Object> entry : attributes.asMap().entrySet()) {
if (i >= countLimit) {
break;
}
result.put((AttributeKey) entry.getKey(), applyAttributeLengthLimit(entry.getValue(), lengthLimit));
i++;
}
return result.build();
}
Aggregations