Search in sources :

Example 1 with ResourceLogs

use of io.opentelemetry.proto.logs.v1.ResourceLogs in project opentelemetry-java by open-telemetry.

the class OtlpHttpLogExporterTest method exportAndAssertResult.

private static ExportLogsServiceRequest exportAndAssertResult(OtlpHttpLogExporter otlpHttpLogExporter, boolean expectedResult) {
    List<LogData> logs = Collections.singletonList(generateFakeLog());
    CompletableResultCode resultCode = otlpHttpLogExporter.export(logs);
    resultCode.join(10, TimeUnit.SECONDS);
    assertThat(resultCode.isSuccess()).isEqualTo(expectedResult);
    List<ResourceLogs> resourceLogs = Arrays.stream(ResourceLogsMarshaler.create(logs)).map(marshaler -> {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            marshaler.writeBinaryTo(bos);
            return ResourceLogs.parseFrom(bos.toByteArray());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }).collect(Collectors.toList());
    return ExportLogsServiceRequest.newBuilder().addAllResourceLogs(resourceLogs).build();
}
Also used : RecordedRequest(com.linecorp.armeria.testing.junit5.server.mock.RecordedRequest) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ExportLogsServiceResponse(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Attributes(io.opentelemetry.api.common.Attributes) InetAddress(java.net.InetAddress) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) Duration(java.time.Duration) Level(org.slf4j.event.Level) ResourceLogsMarshaler(io.opentelemetry.exporter.internal.otlp.logs.ResourceLogsMarshaler) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) GzipSource(okio.GzipSource) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) HttpResponse(com.linecorp.armeria.common.HttpResponse) MockWebServerExtension(com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension) Status(com.google.rpc.Status) HttpMethod(com.linecorp.armeria.common.HttpMethod) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) OkHttpExporter(io.opentelemetry.exporter.internal.okhttp.OkHttpExporter) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) List(java.util.List) LogCapturer(io.github.netmikey.logunit.api.LogCapturer) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) HeldCertificate(okhttp3.tls.HeldCertificate) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) AggregatedHttpRequest(com.linecorp.armeria.common.AggregatedHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Resource(io.opentelemetry.sdk.resources.Resource) MediaType(com.linecorp.armeria.common.MediaType) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RetryPolicy(io.opentelemetry.exporter.internal.retry.RetryPolicy) HttpStatus(com.linecorp.armeria.common.HttpStatus) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) LogData(io.opentelemetry.sdk.logs.data.LogData) LoggingEvent(org.slf4j.event.LoggingEvent) Severity(io.opentelemetry.sdk.logs.data.Severity) Buffer(okio.Buffer) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) RetryUtil(io.opentelemetry.exporter.internal.retry.RetryUtil) Message(com.google.protobuf.Message) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) LogData(io.opentelemetry.sdk.logs.data.LogData) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 2 with ResourceLogs

use of io.opentelemetry.proto.logs.v1.ResourceLogs in project opentelemetry-java by open-telemetry.

the class LogsRequestMarshalerTest method toProtoResourceLogs.

@Test
// test deprecated setName method
@SuppressWarnings("deprecation")
void toProtoResourceLogs() {
    ResourceLogsMarshaler[] resourceLogsMarshalers = ResourceLogsMarshaler.create(Collections.singleton(LogDataBuilder.create(Resource.builder().put("one", 1).setSchemaUrl("http://url").build(), InstrumentationLibraryInfo.create("testLib", "1.0", "http://url")).setName(NAME).setBody(BODY).setSeverity(Severity.INFO).setSeverityText("INFO").setSpanContext(SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault())).setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true)).setEpoch(12345, TimeUnit.NANOSECONDS).build()));
    assertThat(resourceLogsMarshalers).hasSize(1);
    ResourceLogs onlyResourceLogs = parse(ResourceLogs.getDefaultInstance(), resourceLogsMarshalers[0]);
    assertThat(onlyResourceLogs.getSchemaUrl()).isEqualTo("http://url");
    assertThat(onlyResourceLogs.getInstrumentationLibraryLogsCount()).isEqualTo(1);
    InstrumentationLibraryLogs instrumentationLibraryLogs = onlyResourceLogs.getInstrumentationLibraryLogs(0);
    assertThat(instrumentationLibraryLogs.getSchemaUrl()).isEqualTo("http://url");
    assertThat(instrumentationLibraryLogs.getInstrumentationLibrary()).isEqualTo(InstrumentationLibrary.newBuilder().setName("testLib").setVersion("1.0").build());
}
Also used : ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) InstrumentationLibraryLogs(io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceLogs

use of io.opentelemetry.proto.logs.v1.ResourceLogs in project opentelemetry-java by open-telemetry.

the class LogsRequestMarshalerTest method parse.

@SuppressWarnings("unchecked")
private static <T extends Message> T parse(T prototype, Marshaler marshaler) {
    byte[] serialized = toByteArray(marshaler);
    T result;
    try {
        result = (T) prototype.newBuilderForType().mergeFrom(serialized).build();
    } catch (InvalidProtocolBufferException e) {
        throw new UncheckedIOException(e);
    }
    // Our marshaler should produce the exact same length of serialized output (for example, field
    // default values are not outputted), so we check that here. The output itself may have slightly
    // different ordering, mostly due to the way we don't output oneof values in field order all the
    // tieme. If the lengths are equal and the resulting protos are equal, the marshaling is
    // guaranteed to be valid.
    assertThat(result.getSerializedSize()).isEqualTo(serialized.length);
    // We don't compare JSON strings due to some differences (particularly serializing enums as
    // numbers instead of names). This may improve in the future but what matters is what we produce
    // can be parsed.
    String json = toJson(marshaler);
    Message.Builder builder = prototype.newBuilderForType();
    try {
        JsonFormat.parser().merge(json, builder);
    } catch (InvalidProtocolBufferException e) {
        throw new UncheckedIOException(e);
    }
    // libraries currently support customizing on the parse side.
    if (result instanceof LogRecord) {
        fixSpanJsonIds((LogRecord.Builder) builder);
    }
    if (result instanceof ResourceLogs) {
        ResourceLogs.Builder fixed = (ResourceLogs.Builder) builder;
        for (InstrumentationLibraryLogs.Builder ill : fixed.getInstrumentationLibraryLogsBuilderList()) {
            for (LogRecord.Builder span : ill.getLogRecordsBuilderList()) {
                fixSpanJsonIds(span);
            }
        }
    }
    assertThat(builder.build()).isEqualTo(result);
    return result;
}
Also used : ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) Message(com.google.protobuf.Message) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) UncheckedIOException(java.io.UncheckedIOException) ByteString(com.google.protobuf.ByteString) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) InstrumentationLibraryLogs(io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs)

Example 4 with ResourceLogs

use of io.opentelemetry.proto.logs.v1.ResourceLogs in project opentelemetry-java by open-telemetry.

the class OtlpExporterIntegrationTest method testLogExporter.

// test deprecated setName method
@SuppressWarnings("deprecation")
private static void testLogExporter(LogExporter logExporter) {
    LogData logData = LogDataBuilder.create(RESOURCE, InstrumentationLibraryInfo.create(OtlpExporterIntegrationTest.class.getName(), null)).setName("log-name").setBody("log body").setAttributes(Attributes.builder().put("key", "value").build()).setSeverity(Severity.DEBUG).setSeverityText("DEBUG").setEpoch(Instant.now()).setSpanContext(SpanContext.create(IdGenerator.random().generateTraceId(), IdGenerator.random().generateSpanId(), TraceFlags.getDefault(), TraceState.getDefault())).build();
    logExporter.export(Collections.singletonList(logData));
    await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.logRequests).hasSize(1));
    ExportLogsServiceRequest request = grpcServer.logRequests.get(0);
    assertThat(request.getResourceLogsCount()).isEqualTo(1);
    ResourceLogs resourceLogs = request.getResourceLogs(0);
    assertThat(resourceLogs.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
    assertThat(resourceLogs.getInstrumentationLibraryLogsCount()).isEqualTo(1);
    InstrumentationLibraryLogs ilLogs = resourceLogs.getInstrumentationLibraryLogs(0);
    assertThat(ilLogs.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
    assertThat(ilLogs.getLogRecordsCount()).isEqualTo(1);
    io.opentelemetry.proto.logs.v1.LogRecord protoLog = ilLogs.getLogRecords(0);
    assertThat(protoLog.getName()).isEqualTo("log-name");
    assertThat(protoLog.getBody().getStringValue()).isEqualTo("log body");
    assertThat(protoLog.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
    assertThat(protoLog.getSeverityNumber().getNumber()).isEqualTo(logData.getSeverity().getSeverityNumber());
    assertThat(protoLog.getSeverityText()).isEqualTo("DEBUG");
    assertThat(TraceId.fromBytes(protoLog.getTraceId().toByteArray())).isEqualTo(logData.getSpanContext().getTraceId());
    assertThat(SpanId.fromBytes(protoLog.getSpanId().toByteArray())).isEqualTo(logData.getSpanContext().getSpanId());
    assertThat(TraceFlags.fromByte((byte) protoLog.getFlags())).isEqualTo(logData.getSpanContext().getTraceFlags());
    assertThat(protoLog.getTimeUnixNano()).isEqualTo(logData.getEpochNanos());
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) InstrumentationLibraryLogs(io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs)

Example 5 with ResourceLogs

use of io.opentelemetry.proto.logs.v1.ResourceLogs in project opentelemetry-java-instrumentation by open-telemetry.

the class AgentTestingExporterAccess method getExportedLogs.

@SuppressWarnings("unchecked")
public static List<LogData> getExportedLogs() {
    List<byte[]> exportRequests;
    try {
        exportRequests = (List<byte[]>) getLogExportRequests.invokeExact();
    } catch (Throwable t) {
        throw new AssertionError("Could not invoke getMetricExportRequests", t);
    }
    List<ResourceLogs> allResourceLogs = exportRequests.stream().map(serialized -> {
        try {
            return ExportLogsServiceRequest.parseFrom(serialized);
        } catch (InvalidProtocolBufferException e) {
            throw new AssertionError(e);
        }
    }).flatMap(request -> request.getResourceLogsList().stream()).collect(toList());
    List<LogData> logs = new ArrayList<>();
    for (ResourceLogs resourceLogs : allResourceLogs) {
        Resource resource = resourceLogs.getResource();
        for (InstrumentationLibraryLogs ilLogs : resourceLogs.getInstrumentationLibraryLogsList()) {
            InstrumentationLibrary instrumentationLibrary = ilLogs.getInstrumentationLibrary();
            for (LogRecord logRecord : ilLogs.getLogsList()) {
                logs.add(createLogData(logRecord, io.opentelemetry.sdk.resources.Resource.create(fromProto(resource.getAttributesList())), InstrumentationLibraryInfo.create(instrumentationLibrary.getName(), instrumentationLibrary.getVersion())));
            }
        }
    }
    return logs;
}
Also used : TraceFlags(io.opentelemetry.api.trace.TraceFlags) LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) KeyValue(io.opentelemetry.proto.common.v1.KeyValue) EventData(io.opentelemetry.sdk.trace.data.EventData) Sum(io.opentelemetry.proto.metrics.v1.Sum) StatusCode(io.opentelemetry.api.trace.StatusCode) ImmutableSummaryPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryPointData) Attributes(io.opentelemetry.api.common.Attributes) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) Resource(io.opentelemetry.proto.resource.v1.Resource) LogDataBuilder(io.opentelemetry.sdk.logs.data.LogDataBuilder) AttributeKey.stringArrayKey(io.opentelemetry.api.common.AttributeKey.stringArrayKey) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ValueAtPercentile(io.opentelemetry.sdk.metrics.data.ValueAtPercentile) SpanContext(io.opentelemetry.api.trace.SpanContext) SummaryPointData(io.opentelemetry.sdk.metrics.data.SummaryPointData) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) AttributeKey.doubleArrayKey(io.opentelemetry.api.common.AttributeKey.doubleArrayKey) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors(java.util.stream.Collectors) ArrayValue(io.opentelemetry.proto.common.v1.ArrayValue) List(java.util.List) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) SpanData(io.opentelemetry.sdk.trace.data.SpanData) AttributeKey.longArrayKey(io.opentelemetry.api.common.AttributeKey.longArrayKey) AnyValue(io.opentelemetry.proto.common.v1.AnyValue) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) Pattern(java.util.regex.Pattern) ImmutableValueAtPercentile(io.opentelemetry.sdk.metrics.internal.data.ImmutableValueAtPercentile) HistogramDataPoint(io.opentelemetry.proto.metrics.v1.HistogramDataPoint) MethodHandle(java.lang.invoke.MethodHandle) StatusData(io.opentelemetry.sdk.trace.data.StatusData) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) ImmutableSummaryData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) ArrayList(java.util.ArrayList) TraceState(io.opentelemetry.api.trace.TraceState) Metric(io.opentelemetry.proto.metrics.v1.Metric) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) TraceStateBuilder(io.opentelemetry.api.trace.TraceStateBuilder) LogData(io.opentelemetry.sdk.logs.data.LogData) ImmutableGaugeData(io.opentelemetry.sdk.metrics.internal.data.ImmutableGaugeData) Severity(io.opentelemetry.sdk.logs.data.Severity) ImmutableSumData(io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData) AggregationTemporality(io.opentelemetry.sdk.metrics.data.AggregationTemporality) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) SeverityNumber(io.opentelemetry.proto.logs.v1.SeverityNumber) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Span(io.opentelemetry.proto.trace.v1.Span) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) TimeUnit(java.util.concurrent.TimeUnit) ImmutableHistogramPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramPointData) Collectors.toList(java.util.stream.Collectors.toList) MethodType(java.lang.invoke.MethodType) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) InstrumentationLibraryLogs(io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs) AttributeKey.booleanArrayKey(io.opentelemetry.api.common.AttributeKey.booleanArrayKey) ImmutableHistogramData(io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramData) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SummaryDataPoint(io.opentelemetry.proto.metrics.v1.SummaryDataPoint) Status(io.opentelemetry.proto.trace.v1.Status) TestSpanData(io.opentelemetry.sdk.testing.trace.TestSpanData) LogData(io.opentelemetry.sdk.logs.data.LogData) ResourceLogs(io.opentelemetry.proto.logs.v1.ResourceLogs) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ArrayList(java.util.ArrayList) Resource(io.opentelemetry.proto.resource.v1.Resource) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) InstrumentationLibraryLogs(io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs) InstrumentationLibrary(io.opentelemetry.proto.common.v1.InstrumentationLibrary)

Aggregations

ResourceLogs (io.opentelemetry.proto.logs.v1.ResourceLogs)5 InstrumentationLibraryLogs (io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 ExportLogsServiceRequest (io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest)3 LogData (io.opentelemetry.sdk.logs.data.LogData)3 LogDataBuilder (io.opentelemetry.sdk.logs.data.LogDataBuilder)3 Message (com.google.protobuf.Message)2 Attributes (io.opentelemetry.api.common.Attributes)2 LogRecord (io.opentelemetry.proto.logs.v1.LogRecord)2 Test (org.junit.jupiter.api.Test)2 ByteString (com.google.protobuf.ByteString)1 Status (com.google.rpc.Status)1 AggregatedHttpRequest (com.linecorp.armeria.common.AggregatedHttpRequest)1 HttpMethod (com.linecorp.armeria.common.HttpMethod)1 HttpResponse (com.linecorp.armeria.common.HttpResponse)1 HttpStatus (com.linecorp.armeria.common.HttpStatus)1 MediaType (com.linecorp.armeria.common.MediaType)1 ServerBuilder (com.linecorp.armeria.server.ServerBuilder)1 MockWebServerExtension (com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension)1 RecordedRequest (com.linecorp.armeria.testing.junit5.server.mock.RecordedRequest)1