use of io.opentelemetry.proto.logs.v1.InstrumentationLibraryLogs 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.InstrumentationLibraryLogs 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.InstrumentationLibraryLogs 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