use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest 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.collector.logs.v1.ExportLogsServiceRequest in project opentelemetry-java by open-telemetry.
the class OtlpHttpLogExporterTest method testExportGzipCompressed.
@Test
void testExportGzipCompressed() {
server.enqueue(successResponse());
OtlpHttpLogExporter exporter = builder.setCompression("gzip").build();
ExportLogsServiceRequest payload = exportAndAssertResult(exporter, /* expectedResult= */
true);
AggregatedHttpRequest request = server.takeRequest().request();
assertRequestCommon(request);
assertThat(request.headers().get("Content-Encoding")).isEqualTo("gzip");
assertThat(parseRequestBody(gzipDecompress(request.content().array()))).isEqualTo(payload);
}
use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest in project opentelemetry-java by open-telemetry.
the class OtlpHttpLogExporterTest method testExportTls.
@Test
void testExportTls() {
server.enqueue(successResponse());
OtlpHttpLogExporter exporter = builder.setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/logs").setTrustedCertificates(HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)).build();
ExportLogsServiceRequest payload = exportAndAssertResult(exporter, /* expectedResult= */
true);
RecordedRequest recorded = server.takeRequest();
AggregatedHttpRequest request = recorded.request();
assertRequestCommon(request);
assertThat(parseRequestBody(request.content().array())).isEqualTo(payload);
// OkHttp does support HTTP/2 upgrade on TLS.
assertThat(recorded.context().sessionProtocol().isMultiplex()).isTrue();
}
use of io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest 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.collector.logs.v1.ExportLogsServiceRequest 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