Skip to content

Commit

Permalink
Add addAllAttributes() to ReadWriteLogRecord. (#5825)
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk authored Sep 26, 2023
1 parent 0f6b894 commit 3f47934
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-logs.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.logs.ReadWriteLogRecord (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.logs.ReadWriteLogRecord setAllAttributes(io.opentelemetry.api.common.Attributes)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.sdk.logs;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.logs.data.LogRecordData;

/**
Expand All @@ -25,6 +26,24 @@ public interface ReadWriteLogRecord {

// TODO: add additional setters

/**
* Sets attributes to the {@link ReadWriteLogRecord}. If the {@link ReadWriteLogRecord} previously
* contained a mapping for any of the keys, the old values are replaced by the specified values.
*
* @param attributes the attributes
* @return this.
* @since 1.31.0
*/
@SuppressWarnings("unchecked")
default ReadWriteLogRecord setAllAttributes(Attributes attributes) {
if (attributes == null || attributes.isEmpty()) {
return this;
}
attributes.forEach(
(attributeKey, value) -> this.setAttribute((AttributeKey<Object>) attributeKey, value));
return this;
}

/** Return an immutable {@link LogRecordData} instance representing this log record. */
LogRecordData toLogRecordData();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.logs;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.AttributesMap;
import io.opentelemetry.sdk.logs.data.Body;
import io.opentelemetry.sdk.resources.Resource;
import org.junit.jupiter.api.Test;

class ReadWriteLogRecordTest {

@Test
void addAllAttributes() {
Attributes newAttributes = Attributes.of(stringKey("foo"), "bar", stringKey("bar"), "buzz");
SdkReadWriteLogRecord logRecord = buildLogRecord();

logRecord.setAllAttributes(newAttributes);

Attributes result = logRecord.toLogRecordData().getAttributes();
assertThat(result.get(stringKey("foo"))).isEqualTo("bar");
assertThat(result.get(stringKey("bar"))).isEqualTo("buzz");
assertThat(result.get(stringKey("untouched"))).isEqualTo("yes");
}

@Test
void addAllHandlesNull() {
SdkReadWriteLogRecord logRecord = buildLogRecord();
Attributes originalAttributes = logRecord.toLogRecordData().getAttributes();
ReadWriteLogRecord result = logRecord.setAllAttributes(null);
assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes);
}

@Test
void allHandlesEmpty() {
SdkReadWriteLogRecord logRecord = buildLogRecord();
Attributes originalAttributes = logRecord.toLogRecordData().getAttributes();
ReadWriteLogRecord result = logRecord.setAllAttributes(Attributes.empty());
assertThat(result.toLogRecordData().getAttributes()).isEqualTo(originalAttributes);
}

SdkReadWriteLogRecord buildLogRecord() {
Body body = Body.string("bod");
AttributesMap initialAttributes = AttributesMap.create(100, 200);
initialAttributes.put(stringKey("foo"), "aaiosjfjioasdiojfjioasojifja");
initialAttributes.put(stringKey("untouched"), "yes");
LogLimits limits = LogLimits.getDefault();
Resource resource = Resource.empty();
InstrumentationScopeInfo scope = InstrumentationScopeInfo.create("test");
SpanContext spanContext = SpanContext.getInvalid();

return SdkReadWriteLogRecord.create(
limits,
resource,
scope,
0L,
0L,
spanContext,
Severity.DEBUG,
"buggin",
body,
initialAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,18 @@ void stressTest_WithDifferentLabelSet() {
.hasValue(20_000)
.hasAttributes(attributeEntry(keys[3], values[3])))));
}

@Test
void testToString() {
String expected =
"SdkDoubleUpDownCounter{descriptor=InstrumentDescriptor{name=testUpDownCounter, description=description, unit=ms, type=UP_DOWN_COUNTER, valueType=DOUBLE, advice=Advice{explicitBucketBoundaries=null, attributes=null}}}";
DoubleUpDownCounter counter =
sdkMeter
.upDownCounterBuilder("testUpDownCounter")
.ofDoubles()
.setDescription("description")
.setUnit("ms")
.build();
assertThat(counter).hasToString(expected);
}
}

0 comments on commit 3f47934

Please sign in to comment.