open-telemetry / opentelemetry-go

OpenTelemetry Go API and SDK
https://opentelemetry.io/docs/languages/go
Apache License 2.0
5.34k stars 1.08k forks source link

How to clean self-data metric? #5917

Open rockingl opened 1 month ago

rockingl commented 1 month ago

Hi, First of all, thank you for your excellent work. Recently, I encountered some problems while doing push notifications for monitoring. Here is my code.

         // create http oltp exporter,exporter private data(similar to promethus collector.textfile.directory),not Datapoint
        exporter, _ := otlpmetrichttp.New(ctx, otlpmetrichttp.WithInsecure(), otlpmetrichttp.WithEndpoint(endpoint),
        otlpmetrichttp.WithURLPath(urlpath))

        // create provider
    meterProvider := metric.NewMeterProvider(metric.WithReader(metric.NewPeriodicReader(exporter, metric.WithInterval(15*time.Second))))
    otel.SetMeterProvider(meterProvider)

        // create meter
    meter := otel.GetMeterProvider().Meter("test_metric")

        // promethus: tag -> value
        // Notice:private data(has existed), not Datapoint
        opt1 := api.WithAttributes(
          attribute.Key("a").String("b"),
          attribute.Key("c").String("d"),
    )
        opt2 := api.WithAttributes(
          attribute.Key("a").String("b"),
          attribute.Key("c").String("e"), // value is changed
    )
        // create gauge metric name
    gauge,_ := meter.Float64Gauge("test_float64gauage", api.WithDescription("a fun little gauge"))

        // exporter data 
    cnt := 1.0
    for i := 0; i < 2; i++{
        time.Sleep(15 * time.Second)
        cnt++
        gauge.Record(context.Background(), cnt, opt1)
    }

        // end,i hope this attribute disappear, because it's not on the record

    cnt = 10.0
        for i := 0; i < 2; i++ {
        time.Sleep(15 * time.Second)
        cnt++
        gauge.Record(context.Background(), cnt, opt2)
    } 
        //  use the new attribute instead

My goal is to keep only the latest data, but this is not the case. However, two data appear, and the value of the first data has not changed. Is there a cleanup/update mechanism that can clean up the attribute data? Or is my approach wrong and there is another approach that is more suitable for this scenario? Any help would be appreciated,thanks!

dmathieu commented 1 month ago

Removing metrics is not something currently supported. The specification for that addition is however being discussed here: https://github.com/open-telemetry/opentelemetry-specification/issues/2232

rockingl commented 1 month ago

Removing metrics is not something currently supported. The specification for that addition is however being discussed here: open-telemetry/opentelemetry-specification#2232

thanks! I want to achieve what I want, is there any alternative solution? (Previously, the node_exporter export solution was used. However, due to the large number of machines and the inability to fully connect the network, this solution was abandoned.)