Skip to content

Commit

Permalink
Change the codec's name to otel_logs (opensearch-project#5028)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Li <[email protected]>
  • Loading branch information
danhli committed Oct 9, 2024
1 parent ac291d6 commit ae110fd
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 36 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.plugins.otel.codec;

import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin;
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor;
import org.opensearch.dataprepper.model.codec.InputCodec;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.record.Record;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.function.Consumer;

@DataPrepperPlugin(name = "otel_logs", pluginType = InputCodec.class, pluginConfigurationType = OTelLogsInputCodecConfig.class)
public class OTelLogsInputCodec implements InputCodec {
private final OTelLogsInputCodecConfig config;

@DataPrepperPluginConstructor
public OTelLogsInputCodec(final OTelLogsInputCodecConfig config) {
Objects.requireNonNull(config);
this.config = config;
}
public void parse(InputStream inputStream, Consumer<Record<Event>> eventConsumer) throws IOException {
if (OTelLogsInputCodecConfig.JSON_FORMAT.equals(config.getFormat())) {
OTelLogsJsonDecoder decoder = new OTelLogsJsonDecoder();
decoder.parse(inputStream, null, eventConsumer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.plugins.otel.codec;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.AssertTrue;

/**
* Configuration class for {@link OTelLogsInputCodec}.
*/
public class OTelLogsInputCodecConfig {
static final String JSON_FORMAT = "json";

@JsonProperty("format")
private String format = JSON_FORMAT;

/**
* The format of the OTel logs.
* "json" by default.
* @return The OTel logs format.
*/
public String getFormat() {
return format;
}

@AssertTrue(message = "format must be json.")
boolean isValidFormat() {
return JSON_FORMAT.equals(format);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import java.util.function.Consumer;
import java.time.Instant;


public class OTLPJsonLogsDecoder implements ByteDecoder {
public class OTelLogsJsonDecoder implements ByteDecoder {
private final OTelProtoCodec.OTelProtoDecoder otelProtoDecoder;
public OTLPJsonLogsDecoder() {

public OTelLogsJsonDecoder() {
otelProtoDecoder = new OTelProtoCodec.OTelProtoDecoder();
}

public void parse(InputStream inputStream, Instant timeReceivedMs, Consumer<Record<Event>> eventConsumer) throws IOException {
Reader reader = new InputStreamReader(inputStream);
ExportLogsServiceRequest.Builder builder = ExportLogsServiceRequest.newBuilder();
Expand All @@ -37,5 +38,4 @@ public void parse(InputStream inputStream, Instant timeReceivedMs, Consumer<Reco
eventConsumer.accept(new Record<>(log));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@

import java.io.InputStream;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.opensearch.dataprepper.model.log.OpenTelemetryLog;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class OTLPJsonLogsCodecTest {
public class OTelLogsInputCodecTest {
private static final String TEST_REQUEST_LOGS_FILE = "test-request-multiple-logs.json";

public OTLPJsonLogsCodec createObjectUnderTest() {
return new OTLPJsonLogsCodec();

@Mock
private OTelLogsInputCodecConfig config;
@Mock
private OTelLogsInputCodec otelLogsCodec;

@BeforeEach
void setup() {
config = new OTelLogsInputCodecConfig();
otelLogsCodec = createObjectUnderTest();
}

public OTelLogsInputCodec createObjectUnderTest() {
return new OTelLogsInputCodec(config);
}

private void validateLog(OpenTelemetryLog logRecord) {
Expand All @@ -38,8 +52,8 @@ private void validateLog(OpenTelemetryLog logRecord) {

@Test
public void testParse() throws Exception {
InputStream inputStream = OTLPJsonLogsCodecTest.class.getClassLoader().getResourceAsStream(TEST_REQUEST_LOGS_FILE);
createObjectUnderTest().parse(inputStream, (record) -> {
InputStream inputStream = OTelLogsInputCodecTest.class.getClassLoader().getResourceAsStream(TEST_REQUEST_LOGS_FILE);
otelLogsCodec.parse(inputStream, (record) -> {
validateLog((OpenTelemetryLog)record.getData());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class OTLPJsonLogsDecoderTest {
public class OTelLogsJsonDecoderTest {
private static final String TEST_REQUEST_LOGS_FILE = "test-request-multiple-logs.json";

public OTLPJsonLogsDecoder createObjectUnderTest() {
return new OTLPJsonLogsDecoder();
public OTelLogsJsonDecoder createObjectUnderTest() {
return new OTelLogsJsonDecoder();
}

private void validateLog(OpenTelemetryLog logRecord) {
Expand All @@ -39,7 +39,7 @@ private void validateLog(OpenTelemetryLog logRecord) {

@Test
public void testParse() throws Exception {
InputStream inputStream = OTLPJsonLogsDecoderTest.class.getClassLoader().getResourceAsStream(TEST_REQUEST_LOGS_FILE);
InputStream inputStream = OTelLogsJsonDecoderTest.class.getClassLoader().getResourceAsStream(TEST_REQUEST_LOGS_FILE);
createObjectUnderTest().parse(inputStream, Instant.now(), (record) -> {
validateLog((OpenTelemetryLog)record.getData());
});
Expand Down

0 comments on commit ae110fd

Please sign in to comment.