Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addAllAttributes() to ReadWriteLogRecord. #5825

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,7 +24,15 @@ public interface ReadWriteLogRecord {
*/
<T> ReadWriteLogRecord setAttribute(AttributeKey<T> key, T value);

// TODO: add additional setters
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
@SuppressWarnings("unchecked")
default ReadWriteLogRecord setAllAttributes(Attributes attributes) {
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
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,54 @@
/*
* 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() {
Body body = Body.string("bod");
AttributesMap initialAttributes = AttributesMap.create(100, 200);
initialAttributes.put(stringKey("foo"), "aaiosjfjioasdiojfjioasojifja");
initialAttributes.put(stringKey("untouched"), "yes");
Attributes newAttributes = Attributes.of(stringKey("foo"), "bar", stringKey("bar"), "buzz");
LogLimits limits = LogLimits.getDefault();
Resource resource = Resource.empty();
InstrumentationScopeInfo scope = InstrumentationScopeInfo.create("test");
SpanContext spanContext = SpanContext.getInvalid();

SdkReadWriteLogRecord logRecord =
SdkReadWriteLogRecord.create(
limits,
resource,
scope,
0L,
0L,
spanContext,
Severity.DEBUG,
"buggin",
body,
initialAttributes);

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");
}
}
Loading