Skip to content

Commit

Permalink
Add Support for OTel Log SeverityText (opensearch-project#3280) (open…
Browse files Browse the repository at this point in the history
…search-project#3281)

Add Support for OTel Log SeverityText (opensearch-project#3280)

The OpenTelemetry Codec lacks support for the severity text.
This oversight is corrected by extracting the field from the OTLP
source data and copying it to a matching field in the JSON
document. It tightly aligns with the already supported SeverityNumber
field. This closes a gap in the OTLP logs data model mapping.
Unit tests of codec and JSON mapping are adjusted for the added
field.

Signed-off-by: Karsten Schnitter <[email protected]>
  • Loading branch information
KarstenSchnitter authored and asifsmohammed committed Sep 27, 2023
1 parent 121cf61 commit 5f29a30
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class JacksonOtelLog extends JacksonEvent implements OpenTelemetryLog {
protected static final String SPAN_ID_KEY = "spanId";
protected static final String TRACE_ID_KEY = "traceId";
protected static final String SEVERITY_NUMBER_KEY = "severityNumber";
protected static final String SEVERITY_TEXT_KEY = "severityText";
protected static final String DROPPED_ATTRIBUTES_COUNT_KEY = "droppedAttributesCount";


Expand Down Expand Up @@ -87,6 +88,11 @@ public Integer getSeverityNumber() {
return this.get(SEVERITY_NUMBER_KEY, Integer.class);
}

@Override
public String getSeverityText() {
return this.get(SEVERITY_TEXT_KEY, String.class);
}

@Override
public Integer getDroppedAttributesCount() {
return this.get(DROPPED_ATTRIBUTES_COUNT_KEY, Integer.class);
Expand Down Expand Up @@ -262,6 +268,18 @@ public Builder withSeverityNumber(final Integer severityNumber) {
return getThis();
}

/**
* Sets the severity text of this log event
*
* @param severityText sets the severity text of this log event
* @return the builder
* @since 2.5
*/
public Builder withSeverityText(final String severityText) {
data.put(SEVERITY_TEXT_KEY, severityText);
return getThis();
}

/**
* Sets the dropped attributes count of this log event
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public interface OpenTelemetryLog extends Log {
*/
Integer getSeverityNumber();

/**
* Gets the severity text of this log event.
*
* @return the severity number encoded as Integer
* @since 2.5
*/
String getSeverityText();


/**
* Gets the dropped attributes count of this log event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class JacksonOtelLogTest {
private static final String TEST_TRACE_ID = "1234";
private static final String TEST_SPAN_ID = "4321";
private static final Integer TEST_SEVERITY_NUMBER = 2;
private static final String TEST_SEVERITY_TEXT = "severity";
private static final Integer TEST_DROPPED_ATTRIBUTES_COUNT = 4;
private static final Object TEST_BODY = Map.of("log", "message");

Expand All @@ -57,6 +58,7 @@ public void setup() {
.withTraceId(TEST_TRACE_ID)
.withSpanId(TEST_SPAN_ID)
.withSeverityNumber(TEST_SEVERITY_NUMBER)
.withSeverityText(TEST_SEVERITY_TEXT)
.withDroppedAttributesCount(TEST_DROPPED_ATTRIBUTES_COUNT)
.withBody(TEST_BODY);

Expand Down Expand Up @@ -105,6 +107,12 @@ public void testGetSpanId() {
assertThat(spanId, is(equalTo(TEST_SPAN_ID)));
}

@Test
public void testGetServerityText() {
final String severityText = log.getSeverityText();
assertThat(severityText, is(equalTo(TEST_SEVERITY_TEXT)));
}

@Test
public void testGetServerityNumber() {
final Integer observedTime = log.getSeverityNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ protected List<OpenTelemetryLog> processLogsList(final List<LogRecord> logsList,
.withTraceId(OTelProtoCodec.convertByteStringToString(log.getTraceId()))
.withSpanId(OTelProtoCodec.convertByteStringToString(log.getSpanId()))
.withSeverityNumber(log.getSeverityNumberValue())
.withSeverityText(log.getSeverityText())
.withDroppedAttributesCount(log.getDroppedAttributesCount())
.withBody(OTelProtoCodec.convertAnyValue(log.getBody()))
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ private void validateLog(OpenTelemetryLog logRecord) {
assertThat(logRecord.getDroppedAttributesCount(), is(3));
assertThat(logRecord.getSchemaUrl(), is("schemaurl"));
assertThat(logRecord.getSeverityNumber(), is(5));
assertThat(logRecord.getSeverityText(), is("Severity value"));
assertThat(logRecord.getTraceId(), is("ba1a1c23b4093b63"));
assertThat(logRecord.getSpanId(), is("2cc83ac90ebc469c"));
Map<String, Object> mergedAttributes = logRecord.getAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"logRecords": [{
"timeUnixNano": "1590328800000000000",
"severityNumber": "SEVERITY_NUMBER_DEBUG",
"severityText": "Severity value",
"body": {
"stringValue": "Log value"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"logRecords": [{
"timeUnixNano": "1590328800000000000",
"severityNumber": "SEVERITY_NUMBER_DEBUG",
"severityText": "Severity value",
"body": {
"stringValue": "Log value"
},
Expand Down

0 comments on commit 5f29a30

Please sign in to comment.