use of io.opentelemetry.sdk.logs.LogEmitter 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.sdk.logs.LogEmitter in project opentelemetry-plugin by jenkinsci.
the class OpenTelemetryLogToElasticsearchIT method test.
@Test
public void test() throws Exception {
InputStream envAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(".env");
Assert.assertNotNull(".env file not found in classpath", envAsStream);
Properties env = new Properties();
env.load(envAsStream);
Map<String, String> configuration = new HashMap<>();
env.forEach((k, v) -> configuration.put(k.toString(), v.toString()));
configuration.put("otel.traces.exporter", "otlp");
configuration.put("otel.metrics.exporter", "otlp");
configuration.put("otel.logs.exporter", "otlp");
final int LOG_MESSAGE_COUNT = 100;
String traceId;
// PRODUCE OPEN TELEMETRY LOG MESSAGES
{
AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = AutoConfiguredOpenTelemetrySdk.builder().addPropertiesSupplier(() -> configuration).build();
try {
OpenTelemetrySdk sdk = autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk();
Tracer tracer = sdk.getTracer("test");
LogEmitter logEmitter = sdk.getSdkLogEmitterProvider().get("test");
Span span = tracer.spanBuilder("my-test-pipeline").startSpan();
try (Scope scope = span.makeCurrent()) {
for (int i = 0; i < LOG_MESSAGE_COUNT; i++) {
logEmitter.logBuilder().setContext(Context.current()).setBody("Log Message " + i).setAttributes(Attributes.of(AttributeKey.stringKey("myStringAttribute"), "Value " + i, AttributeKey.longKey("myNumericAttribute"), (long) i)).emit();
Thread.sleep(RANDOM.nextInt(100));
}
} finally {
span.end();
}
traceId = span.getSpanContext().getTraceId();
} finally {
autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk().getSdkLogEmitterProvider().close();
autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk().getSdkTracerProvider().close();
}
}
Thread.sleep(3_000);
// VERIFY LOG MESSAGES IN ELASTICSEARCH
{
String elasticsearchUrl = configuration.get("elasticsearch.url");
String elasticsearchUsername = configuration.get("elasticsearch.username");
String elasticsearchPassword = configuration.get("elasticsearch.password");
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticsearchUsername, elasticsearchPassword));
RestClient restClient = RestClient.builder(HttpHost.create(elasticsearchUrl)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();
RestClientTransport elasticsearchTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(elasticsearchTransport);
SearchRequest searchRequest = new SearchRequest.Builder().index("logs-apm.app-*").size(500).sort(s -> s.field(f -> f.field("@timestamp").order(SortOrder.Asc))).query(q -> q.match(m -> m.field("trace.id").query(FieldValue.of(traceId)))).build();
SearchResponse<ObjectNode> searchResponse = elasticsearchClient.search(searchRequest, ObjectNode.class);
List<Hit<ObjectNode>> hits = searchResponse.hits().hits();
if (hits.size() != LOG_MESSAGE_COUNT) {
System.err.println("Invalid number of log messages: actual: " + hits.size() + ", expected: " + LOG_MESSAGE_COUNT);
}
for (Hit<ObjectNode> hit : hits) {
ObjectNode source = hit.source();
ObjectNode labels = (ObjectNode) source.findValue("labels");
ObjectNode numericLabels = (ObjectNode) source.findValue("numeric_labels");
try {
String message = source.findValue("message").asText();
String myStringAttribute = labels.findValue("myStringAttribute").asText();
long myNumericAttribute = numericLabels.findValue("myNumericAttribute").longValue();
System.out.println(hit.id() + "\tmessage:'" + message + "', \tmyStringAttribute: '" + myStringAttribute + "', myNumericAttribute: " + myNumericAttribute);
} catch (Exception e) {
System.err.println("Error parsing " + source);
}
}
}
}
Aggregations