Search in sources :

Example 1 with LogEmitter

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());
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) Meter(io.opentelemetry.api.metrics.Meter) ArrayList(java.util.ArrayList) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) ExportLogsServiceRequest(io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) LogEmitter(io.opentelemetry.sdk.logs.LogEmitter) LogRecord(io.opentelemetry.proto.logs.v1.LogRecord) Metric(io.opentelemetry.proto.metrics.v1.Metric) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Test(org.junit.jupiter.api.Test)

Example 2 with LogEmitter

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);
            }
        }
    }
}
Also used : RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) RestClient(org.elasticsearch.client.RestClient) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Attributes(io.opentelemetry.api.common.Attributes) HashMap(java.util.HashMap) Random(java.util.Random) Tracer(io.opentelemetry.api.trace.Tracer) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk) Map(java.util.Map) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) Context(io.opentelemetry.context.Context) Properties(java.util.Properties) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) Span(io.opentelemetry.api.trace.Span) Scope(io.opentelemetry.context.Scope) Test(org.junit.Test) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Hit(co.elastic.clients.elasticsearch.core.search.Hit) List(java.util.List) AuthScope(org.apache.http.auth.AuthScope) AttributeKey(io.opentelemetry.api.common.AttributeKey) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) LogEmitter(io.opentelemetry.sdk.logs.LogEmitter) Assert(org.junit.Assert) HttpHost(org.apache.http.HttpHost) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) InputStream(java.io.InputStream) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HashMap(java.util.HashMap) Properties(java.util.Properties) Span(io.opentelemetry.api.trace.Span) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) List(java.util.List) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) Tracer(io.opentelemetry.api.trace.Tracer) RestClient(org.elasticsearch.client.RestClient) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Scope(io.opentelemetry.context.Scope) AuthScope(org.apache.http.auth.AuthScope) LogEmitter(io.opentelemetry.sdk.logs.LogEmitter) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk) Test(org.junit.Test)

Aggregations

LogEmitter (io.opentelemetry.sdk.logs.LogEmitter)2 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)1 FieldValue (co.elastic.clients.elasticsearch._types.FieldValue)1 SortOrder (co.elastic.clients.elasticsearch._types.SortOrder)1 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)1 SearchResponse (co.elastic.clients.elasticsearch.core.SearchResponse)1 Hit (co.elastic.clients.elasticsearch.core.search.Hit)1 JacksonJsonpMapper (co.elastic.clients.json.jackson.JacksonJsonpMapper)1 RestClientTransport (co.elastic.clients.transport.rest_client.RestClientTransport)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 Attributes (io.opentelemetry.api.common.Attributes)1 Meter (io.opentelemetry.api.metrics.Meter)1 Span (io.opentelemetry.api.trace.Span)1 Tracer (io.opentelemetry.api.trace.Tracer)1 Context (io.opentelemetry.context.Context)1 Scope (io.opentelemetry.context.Scope)1 ExportLogsServiceRequest (io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest)1 ExportMetricsServiceRequest (io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)1 ExportTraceServiceRequest (io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest)1