Search in sources :

Example 6 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)

Example 7 with AttributesBuilder

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);
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) IgnoreJRERequirement(org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement)

Example 8 with AttributesBuilder

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;
}
Also used : StringWriter(java.io.StringWriter) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) PrintWriter(java.io.PrintWriter)

Example 9 with AttributesBuilder

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();
}
Also used : AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Map(java.util.Map)

Example 10 with AttributesBuilder

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();
}
Also used : AttributeKey(io.opentelemetry.api.common.AttributeKey) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Map(java.util.Map)

Aggregations

AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)105 Test (org.junit.jupiter.api.Test)39 HashMap (java.util.HashMap)35 Map (java.util.Map)33 Context (io.opentelemetry.context.Context)25 Attributes (io.opentelemetry.api.common.Attributes)15 Test (org.junit.Test)14 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)9 LogBuilder (io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)5 AttributeKey (io.opentelemetry.api.common.AttributeKey)4 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 Request (okhttp3.Request)4 Request (com.android.volley.Request)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 Response (okhttp3.Response)3 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 HttpResponse (com.android.volley.toolbox.HttpResponse)2