open-telemetry / opentelemetry-go-contrib

Collection of extensions for OpenTelemetry-Go.
https://opentelemetry.io/
Apache License 2.0
1.2k stars 563 forks source link

Otelhttp metrics are not exporter to prometheus with meter option #5964

Open kirill-lokhmatov opened 3 months ago

kirill-lokhmatov commented 3 months ago

Description

Environment

Steps To Reproduce

`

   prometheusExporter, err := prometheus.New(
    prometheus.WithoutTargetInfo(),
    prometheus.WithoutScopeInfo())
if err != nil {
    return nil, err
}
provider := metric.NewMeterProvider(
    metric.WithReader(prometheusExporter),
)
loggedHttpTransport := transport.NewLoggedHttpTransport(logger)
client := &http.Client{
    Transport: otelhttp.NewTransport(otelhttp.WithMeterProvider(provider))}```

No metrics in prom (i am doing the same when working with otelsql and see the metrics)

Expected behavior

Http client metrics are exported to prometheus (like in otelsql)

dmathieu commented 3 months ago

Could you provide us with actually runnable steps to reproduce? I have tried reproducing with an adaptation from the prometheus example, and I am properly getting metrics.

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main

import (
    "io"
    "log"
    "net/http"

    "github.com/prometheus/client_golang/prometheus/promhttp"

    "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
    "go.opentelemetry.io/otel/exporters/prometheus"
    "go.opentelemetry.io/otel/sdk/metric"
)

func main() {
    exporter, err := prometheus.New()
    if err != nil {
        log.Fatal(err)
    }
    provider := metric.NewMeterProvider(metric.WithReader(exporter))

    helloHandler := func(w http.ResponseWriter, req *http.Request) {
        _, _ = io.WriteString(w, "Hello, world!\n")
    }

    otelHandler := otelhttp.NewHandler(
        http.HandlerFunc(helloHandler),
        "Hello",
        otelhttp.WithMeterProvider(provider),
    )

    http.Handle("/metrics", promhttp.Handler())
    http.Handle("/hello", otelHandler)
    err = http.ListenAndServe(":7777", nil)
    if err != nil {
        log.Fatal(err)
    }
}