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();
}
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());
}
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;
}
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());
}
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;
}
Aggregations