Search in sources :

Example 11 with AttributesBuilder

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

the class Resource method merge.

/**
 * Returns a new, merged {@link Resource} by merging the current {@code Resource} with the {@code
 * other} {@code Resource}. In case of a collision, the "other" {@code Resource} takes precedence.
 *
 * @param other the {@code Resource} that will be merged with {@code this}.
 * @return the newly merged {@code Resource}.
 */
public Resource merge(@Nullable Resource other) {
    if (other == null) {
        return this;
    }
    AttributesBuilder attrBuilder = Attributes.builder();
    attrBuilder.putAll(this.getAttributes());
    attrBuilder.putAll(other.getAttributes());
    if (other.getSchemaUrl() == null) {
        return create(attrBuilder.build(), getSchemaUrl());
    }
    if (getSchemaUrl() == null) {
        return create(attrBuilder.build(), other.getSchemaUrl());
    }
    if (!other.getSchemaUrl().equals(getSchemaUrl())) {
        logger.info("Attempting to merge Resources with different schemaUrls. " + "The resulting Resource will have no schemaUrl assigned. Schema 1: " + getSchemaUrl() + " Schema 2: " + other.getSchemaUrl());
        // apply schema transformations if possible.
        return create(attrBuilder.build(), null);
    }
    return create(attrBuilder.build(), getSchemaUrl());
}
Also used : AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

Example 12 with AttributesBuilder

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

the class ResourceConfiguration method getAttributes.

// visible for testing
static Attributes getAttributes(ConfigProperties configProperties) {
    AttributesBuilder resourceAttributes = Attributes.builder();
    configProperties.getMap(ATTRIBUTE_PROPERTY).forEach(resourceAttributes::put);
    String serviceName = configProperties.getString(SERVICE_NAME_PROPERTY);
    if (serviceName != null) {
        resourceAttributes.put(ResourceAttributes.SERVICE_NAME, serviceName);
    }
    return resourceAttributes.build();
}
Also used : AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

Example 13 with AttributesBuilder

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

the class LoggingEventMapper method mapLoggingEvent.

/**
 * Map the {@link ILoggingEvent} data model onto the {@link LogBuilder}. Unmapped fields include:
 *
 * <ul>
 *   <li>Thread name - {@link ILoggingEvent#getThreadName()}
 *   <li>Marker - {@link ILoggingEvent#getMarker()}
 *   <li>Mapped diagnostic context - {@link ILoggingEvent#getMDCPropertyMap()}
 * </ul>
 */
private void mapLoggingEvent(LogBuilder builder, ILoggingEvent loggingEvent) {
    // message
    String message = loggingEvent.getFormattedMessage();
    if (message != null) {
        builder.setBody(message);
    }
    // time
    long timestamp = loggingEvent.getTimeStamp();
    builder.setEpoch(timestamp, TimeUnit.MILLISECONDS);
    // level
    Level level = loggingEvent.getLevel();
    if (level != null) {
        builder.setSeverity(levelToSeverity(level));
        builder.setSeverityText(level.levelStr);
    }
    AttributesBuilder attributes = Attributes.builder();
    // throwable
    Object throwableProxy = loggingEvent.getThrowableProxy();
    Throwable throwable = null;
    if (throwableProxy instanceof ThrowableProxy) {
        // there is only one other subclass of ch.qos.logback.classic.spi.IThrowableProxy
        // and it is only used for logging exceptions over the wire
        throwable = ((ThrowableProxy) throwableProxy).getThrowable();
    }
    if (throwable != null) {
        setThrowable(attributes, throwable);
    }
    captureMdcAttributes(attributes, loggingEvent.getMDCPropertyMap());
    if (captureExperimentalAttributes) {
        Thread currentThread = Thread.currentThread();
        attributes.put(SemanticAttributes.THREAD_NAME, currentThread.getName());
        attributes.put(SemanticAttributes.THREAD_ID, currentThread.getId());
    }
    builder.setAttributes(attributes.build());
    // span context
    builder.setContext(Context.current());
}
Also used : Level(ch.qos.logback.classic.Level) ThrowableProxy(ch.qos.logback.classic.spi.ThrowableProxy) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

Example 14 with AttributesBuilder

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

the class LoggingEventMapperTest method testDefault.

@Test
void testDefault() {
    // given
    LoggingEventMapper mapper = new LoggingEventMapper(emptyList());
    Map<String, String> contextData = new HashMap<>();
    contextData.put("key1", "value1");
    contextData.put("key2", "value2");
    AttributesBuilder attributes = Attributes.builder();
    // when
    mapper.captureMdcAttributes(attributes, contextData);
    // then
    assertThat(attributes.build()).isEmpty();
}
Also used : HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 15 with AttributesBuilder

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

the class Bridging method tagsAsAttributes.

static Attributes tagsAsAttributes(Meter.Id id, NamingConvention namingConvention) {
    Iterable<Tag> tags = id.getTagsAsIterable();
    if (!tags.iterator().hasNext()) {
        return Attributes.empty();
    }
    AttributesBuilder builder = Attributes.builder();
    for (Tag tag : tags) {
        String tagKey = namingConvention.tagKey(tag.getKey());
        String tagValue = namingConvention.tagValue(tag.getValue());
        builder.put(tagsCache.computeIfAbsent(tagKey, AttributeKey::stringKey), tagValue);
    }
    return builder.build();
}
Also used : Tag(io.micrometer.core.instrument.Tag) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder)

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