Home > Backend Development > Golang > How to push metrics to Opentelemetry using go-sdk

How to push metrics to Opentelemetry using go-sdk

王林
Release: 2024-02-08 23:42:32
forward
1132 people have browsed it

如何使用 go-sdk 将指标推送到 Opentelemetry

php Editor Xigua will introduce you how to use go-sdk to push indicators to Opentelemetry. Opentelemetry is an open source observation framework that helps developers collect and analyze application metric data. Using go-sdk, you can easily push custom metrics to Opentelemetry and visualize and monitor them through dashboards. This article will introduce you to the installation and configuration process of go-sdk in detail, as well as how to write code to push indicator data to Opentelemetry, and give some practical examples and best practices. Whether you are a novice or an experienced developer, you can get useful information and guidance from this article to help you better use go-sdk to push indicator data to Opentelemetry.

Question content

I tried to use go-sdk to push metrics to opentelemetry, but I didn't find any documentation about it. So far I've tried using this code but don't know what the next step is, to be honest I'm a bit confused with the provider and reader stuff, I'm new to monitoring

func main() {
    ctx := context.Background()

    res, err := resource.New(ctx, resource.WithAttributes(semconv.ServiceName("my-service")))
    if err != nil {
        panic(err)
    }

    ctx, cancel := context.WithTimeout(ctx, time.Second)
    defer cancel()
    conn, err := grpc.DialContext(ctx, "localhost:4317", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
    if err != nil {
        panic(err)
    }

    metricExporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(conn))
    if err != nil {
        panic(err)
    }

    exporter, err := prometheus.New()
    if err != nil {
        panic(err)
    }

    provider := metric.NewMeterProvider(metric.WithReader(exporter))
    meter := provider.Meter("github.com/open-telemetry/opentelemetry-go/example/prometheus")

    counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter"))
    if err != nil {
        panic(err)
    }
    counter.Add(ctx, 5)

    reader := metric.NewPeriodicReader(metricExporter, metric.WithInterval(1*time.Second))

}
Copy after login

The source information I have so far is from the example here and the example here

Workaround

Firstly, it's worth noting that the OTel Go Metrics API and SDK Currently considered unstable. I'll try to focus my answer on higher-level items included in the stable OpenTelemetry metrics specification so that it doesn't become obsolete. Any go-specific content below is subject to change.

It looks like you are trying to set up two different types of exporters. The Prometheus exporter is scraped by Prometheus, and the otelmetricgrpc exporter creates a connection to push metrics through OTLP to a sink such as the OpenTelemetry Collector. You may only need one or the other, not both. The first example you linked is really just for tracking, not metrics, but the principle is roughly the same.

The entry point for any metric in the OpenTelemetry SDK is the meter provider . Instrument providers handle metric-related configuration, aggregation, and data export. This is also how users obtain meters. Each code module usually has its own instrumentation. Create based on this instrumentation metrics and record data points based on these metrics. All indicator points are aggregated and flowed back through the metering provider.

In order to read metrics from a meter provider, you need some kind of metric reader . If you are trying to push metrics using a protocol such as OTLP, you can use a tool like Periodic Export Metrics Reader, which periodically collects metrics from metering providers and exports them using an exporter.

If you are trying to use a pull-based metrics system (such as Prometheus), the exporter acts like a metrics reader and reads the metrics every time the metrics system requests the crawl endpoint. This is what is done in the second example you linked. serveMetrics The function opens a web server that listens on port 2223 and handles requests to localhost:2223/metrics. Each time a GET request comes into this endpoint, aggregated metrics are configured based on the instrumentation provider and returned in the response body. This allows systems like Prometheus to periodically request metrics from your application.

The above is the detailed content of How to push metrics to Opentelemetry using go-sdk. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template