@WithSpan not generating logs

2.1k views Asked by At

I got the metrics and traces pushed to open telemetry collector successfully from my java application with the configuration

        System.setProperty("otel.resource.attributes", "service.name=OtlpExporterExample");
        System.setProperty("otel.metrics.exporter", "otlp");
        OpenTelemetry openTelemetry = initOpenTelemetry();
        MeterProvider meterProvider = initOpenTelemetryMetrics();
        tracer = openTelemetry.getTracer("io.opentelemetry.example");
        Meter meter = meterProvider.get("io.opentelemetry.example");
        // custom logs here
        LongHistogram recorder = meter.histogramBuilder("super_timer").ofLongs().setUnit("ms").build();
        // sleep for a bit to let everything settle
        Thread.sleep(2000);

and the helper methods are

static OpenTelemetry initOpenTelemetry() {
        OtlpGrpcSpanExporter spanExporter =
              OtlpGrpcSpanExporter.builder().setTimeout(2, TimeUnit.SECONDS)
                    .build();
        BatchSpanProcessor spanProcessor =
              BatchSpanProcessor.builder(spanExporter)
                    .setScheduleDelay(100, TimeUnit.MILLISECONDS)
                    .build();

        SdkTracerProvider tracerProvider =
              SdkTracerProvider.builder()
                    .addSpanProcessor(spanProcessor)
                    .setResource(OpenTelemetryResourceAutoConfiguration.configureResource())
                    .build();
        OpenTelemetrySdk openTelemetrySdk =
              OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).buildAndRegisterGlobal();

        Runtime.getRuntime().addShutdownHook(new Thread(tracerProvider::shutdown));

        return openTelemetrySdk;
    }

    /**
     * Initializes a Metrics SDK with a OtlpGrpcMetricExporter and an IntervalMetricReader.
     *
     * @return a ready-to-use {@link MeterProvider} instance
     */
    static MeterProvider initOpenTelemetryMetrics() {
        // set up the metric exporter and wire it into the SDK and a timed reader.
        OtlpGrpcMetricExporter metricExporter = OtlpGrpcMetricExporter.getDefault();

        MetricReaderFactory periodicReaderFactory =
              PeriodicMetricReader.create(metricExporter, Duration.ofMillis(1000));

        SdkMeterProvider sdkMeterProvider =
              SdkMeterProvider.builder()
                    .registerMetricReader(periodicReaderFactory)
                    .buildAndRegisterGlobal();

        Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::shutdown));
        return sdkMeterProvider;
    }

(I copied this piece of code from a github repo)

Now I am trying to move the existing logging wrapper to output spans with least changes

    @WithSpan("important")
    public void info(@SpanAttribute("class") Class classObj, @SpanAttribute("message") CustomClass message) {
        System.out.println("Trace id " + Span.current().getSpanContext().getTraceId());
            System.out.println("span id " + Span.current().getSpanContext().getSpanId());
        info(classObj, JsonUtils.mapToJson(message));
    }

but no spans get generated. Is there a restriction on what kind of methods withSpan can be used?

The output of above sysouts are

Trace id 00000000000000000000000000000000
span id 0000000000000000

Gradle config

implementation('io.opentelemetry:opentelemetry-api:1.6.0')
    implementation('io.opentelemetry:opentelemetry-sdk:1.6.0')

    //pull in the autoconfigure extension so we parse the `otel.resource.attributes` system property used in the example.
    implementation('io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.7.0-alpha')
    implementation('io.opentelemetry:opentelemetry-extension-annotations:1.7.0')
    implementation("io.opentelemetry:opentelemetry-api-metrics:1.7.0-alpha")
    implementation('io.opentelemetry:opentelemetry-sdk-metrics:1.7.0-alpha')
    implementation("io.opentelemetry:opentelemetry-exporter-otlp-metrics:1.7.0-alpha")

EDIT: If instead of @WithSpan I obtain a tracer and start/end the span then the spans do end up in collector

1

There are 1 answers

0
Gaurang On

According to java doc @WithSpan only works with automatic instrumentation of java application using agent.

https://javadoc.io/static/io.opentelemetry/opentelemetry-contrib-auto-annotations/0.5.0/io/opentelemetry/contrib/auto/annotations/WithSpan.html

What you are trying to do is manual instrumentation of the application. if you want to use @WithSpan use OpenTelemetry java agent