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