use of io.opentelemetry.proto.logs.v1.LogRecord in project opentelemetry-java by open-telemetry.
the class LogsRequestMarshalerTest method toProtoLogRecord_MinimalFields.
// name field has been deprecated
@SuppressWarnings("deprecation")
@Test
void toProtoLogRecord_MinimalFields() {
LogRecord logRecord = parse(LogRecord.getDefaultInstance(), LogMarshaler.create(LogDataBuilder.create(Resource.create(Attributes.builder().put("testKey", "testValue").build()), InstrumentationLibraryInfo.create("instrumentation", "1")).setEpoch(12345, TimeUnit.NANOSECONDS).build()));
assertThat(logRecord.getTraceId()).isEmpty();
assertThat(logRecord.getSpanId()).isEmpty();
assertThat(logRecord.getName()).isBlank();
assertThat(logRecord.getSeverityText()).isBlank();
assertThat(logRecord.getSeverityNumber().getNumber()).isEqualTo(Severity.UNDEFINED_SEVERITY_NUMBER.getSeverityNumber());
assertThat(logRecord.getBody()).isEqualTo(AnyValue.newBuilder().setStringValue("").build());
assertThat(logRecord.getAttributesList()).isEmpty();
assertThat(logRecord.getTimeUnixNano()).isEqualTo(12345);
}
use of io.opentelemetry.proto.logs.v1.LogRecord in project opentelemetry-java by open-telemetry.
the class FullConfigTest method configures.
@Test
void configures() throws Exception {
Collection<String> fields = GlobalOpenTelemetry.get().getPropagators().getTextMapPropagator().fields();
List<String> keys = new ArrayList<>();
keys.addAll(W3CTraceContextPropagator.getInstance().fields());
keys.addAll(W3CBaggagePropagator.getInstance().fields());
keys.addAll(B3Propagator.injectingSingleHeader().fields());
keys.addAll(B3Propagator.injectingMultiHeaders().fields());
keys.addAll(JaegerPropagator.getInstance().fields());
keys.addAll(OtTracePropagator.getInstance().fields());
keys.addAll(AwsXrayPropagator.getInstance().fields());
// Added by TestPropagatorProvider
keys.add("test");
assertThat(fields).containsExactlyInAnyOrderElementsOf(keys);
GlobalOpenTelemetry.get().getMeterProvider().get("test").counterBuilder("test").build().add(1);
GlobalOpenTelemetry.get().getTracer("test").spanBuilder("test").startSpan().setAttribute("cat", "meow").setAttribute("dog", "bark").end();
Meter meter = GlobalOpenTelemetry.get().getMeter("test");
meter.counterBuilder("my-metric").build().add(1, Attributes.builder().put("allowed", "bear").put("not allowed", "dog").build());
meter.counterBuilder("my-other-metric").build().add(1);
LogEmitter logEmitter = logEmitterProvider.get("test");
logEmitter.logBuilder().setBody("debug log message").setSeverity(Severity.DEBUG).emit();
logEmitter.logBuilder().setBody("info log message").setSeverity(Severity.INFO).emit();
await().untilAsserted(() -> assertThat(otlpTraceRequests).hasSize(1));
ExportTraceServiceRequest traceRequest = otlpTraceRequests.take();
assertThat(traceRequest.getResourceSpans(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
io.opentelemetry.proto.trace.v1.Span span = traceRequest.getResourceSpans(0).getInstrumentationLibrarySpans(0).getSpans(0);
// Dog dropped by attribute limit.
assertThat(span.getAttributesList()).containsExactlyInAnyOrder(KeyValue.newBuilder().setKey("configured").setValue(AnyValue.newBuilder().setBoolValue(true).build()).build(), KeyValue.newBuilder().setKey("wrapped").setValue(AnyValue.newBuilder().setIntValue(1).build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
// await on assertions since metrics may come in different order for BatchSpanProcessor,
// exporter, or the ones we
// created in the test.
await().untilAsserted(() -> {
ExportMetricsServiceRequest metricRequest = otlpMetricsRequests.take();
assertThat(metricRequest.getResourceMetrics(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
for (ResourceMetrics resourceMetrics : metricRequest.getResourceMetricsList()) {
assertThat(resourceMetrics.getInstrumentationLibraryMetricsList()).anySatisfy(ilm -> assertThat(ilm.getInstrumentationLibrary().getName()).isEqualTo("test"));
for (InstrumentationLibraryMetrics instrumentationLibraryMetrics : resourceMetrics.getInstrumentationLibraryMetricsList()) {
for (Metric metric : instrumentationLibraryMetrics.getMetricsList()) {
// SPI was loaded
// MetricExporterCustomizer filters metrics not named my-metric
assertThat(metric.getName()).isEqualTo("my-metric");
// TestMeterProviderConfigurer configures a view that only passes on attribute
// named allowed
// configured-test
assertThat(getFirstDataPointLabels(metric)).contains(KeyValue.newBuilder().setKey("allowed").setValue(AnyValue.newBuilder().setStringValue("bear").build()).build());
}
}
}
});
await().untilAsserted(() -> assertThat(otlpLogsRequests).hasSize(1));
ExportLogsServiceRequest logRequest = otlpLogsRequests.take();
assertThat(logRequest.getResourceLogs(0).getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("test").build()).build(), KeyValue.newBuilder().setKey("cat").setValue(AnyValue.newBuilder().setStringValue("meow").build()).build());
// MetricExporterCustomizer filters logs not whose level is less than Severity.INFO
LogRecord log = logRequest.getResourceLogs(0).getInstrumentationLibraryLogs(0).getLogRecords(0);
assertThat(log.getBody().getStringValue()).isEqualTo("info log message");
assertThat(log.getSeverityNumberValue()).isEqualTo(Severity.INFO.getSeverityNumber());
}
use of io.opentelemetry.proto.logs.v1.LogRecord in project opentelemetry-java by open-telemetry.
the class LogsRequestMarshalerTest method toProtoLogRecord.
@Test
// test deprecated setName method
@SuppressWarnings("deprecation")
void toProtoLogRecord() {
LogRecord logRecord = parse(LogRecord.getDefaultInstance(), LogMarshaler.create(LogDataBuilder.create(Resource.create(Attributes.builder().put("testKey", "testValue").build()), InstrumentationLibraryInfo.create("instrumentation", "1")).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(logRecord.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
assertThat(logRecord.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
assertThat(logRecord.getName()).isEqualTo(NAME);
assertThat(logRecord.getSeverityText()).isEqualTo("INFO");
assertThat(logRecord.getBody()).isEqualTo(AnyValue.newBuilder().setStringValue(BODY).build());
assertThat(logRecord.getAttributesList()).containsExactly(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setBoolValue(true).build()).build());
assertThat(logRecord.getTimeUnixNano()).isEqualTo(12345);
}
use of io.opentelemetry.proto.logs.v1.LogRecord 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.LogRecord 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());
}
Aggregations