How to push metric to Opentelemetry with go-sdk

huangapple go评论70阅读模式
英文:

How to push metric to Opentelemetry with go-sdk

问题

我尝试使用Go SDK将指标推送到OpenTelemetry,但是我找不到相关的文档。到目前为止,我尝试了以下代码,但是我不知道下一步该怎么做,老实说,我对提供程序和读取器的概念有点困惑,我在监控方面是新手。

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))

}

到目前为止,我所了解的源信息来自这里示例1示例2

英文:

I try to push metric to opentelemetry with go-sdk but I found nothing about doc for it. so far I try with this code but idk what the next step, to be honest, I'm bit confuse with the provider and reader stuff, I'm new in 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))

}

the source information i have so far its from here Sample and here Sample

答案1

得分: 2

首先,值得注意的是,OTel Go metrics API和SDK目前不被认为是稳定的。我会尽量将我的回答集中在稳定的OpenTelemetry metrics规范中包含的更高级别的项目上,以免过时。任何与Go特定相关的内容都可能会发生变化。

看起来你正在尝试设置两种不同类型的exporter。prometheus exporter由Prometheus进行抓取,而otelmetricgrpc exporter创建一个连接,通过OTLP将指标推送到像OpenTelemetry Collector这样的接收器。你可能只需要其中一个,而不是两者都需要。你提供的第一个示例实际上只是用于跟踪,而不是指标,但原则基本相同。

在任何OpenTelemetry SDK中,指标的入口点是meter provider。meter provider处理与指标相关的配置、聚合和数据的导出。它也是用户获取meters的方式。每个代码模块通常都有自己的meter。从该meter创建指标,从这些指标创建数据点。所有指标点都会被聚合在一起,并流回到meter provider。

为了从meter provider读取指标,你需要某种类型的metric reader。如果你想使用像OTLP这样的协议推送指标,你可以使用类似于周期性导出的metric reader,该reader定期从meter provider收集指标并使用exporter导出它们。

如果你想使用像prometheus这样的拉取式指标系统,exporter就像metric reader一样,每次指标系统请求抓取端点时读取指标。这就是你提供的第二个示例中所做的。serveMetrics函数打开一个在端口2223上监听的Web服务器,并处理对localhost:2223/metrics的请求。每当该端点上收到一个GET请求时,指标会根据meter provider的配置进行聚合,并在响应正文中返回。这允许像Prometheus这样的系统定期从你的应用程序请求指标。

英文:

First, it is worth noting that the OTel Go metrics API and SDK are not currently considered stable. I will try to keep my answer focused on higher level items which are included in the stable OpenTelemetry metrics specification so that it doesn't become outdated. Anything go-specific which follows is subject to change.

It looks like you're trying to set up two different types of exporters. The prometheus exporter is scraped by Prometheus, and the otelmetricgrpc exporter creates a connection which pushes metrics over OTLP to a receiver like the OpenTelemetry Collector. You likely only need one and not both of those things. The first sample you linked is actually only for traces, not metrics, but the principal is largely the same.

The entry point for metrics in any OpenTelemetry SDK is a meter provider. The meter provider handles metric related configuration and aggregation and export of data. It is also how users acquire meters. Each code module typically has its own meter. From that meter metrics are created, and from those metrics data points are recorded. All metric points are aggregated together and flow back through the meter provider.

In order to read metrics from a meter provider you need a metric reader of some kind. If you are trying to push metrics using a protocol like OTLP, you would use something like the periodic exporting metric reader which periodically collects metrics from the meter provider and exports them using an exporter.

If you were trying to use a pull-based metric system like prometheus, the exporter acts like the metric reader and reads metrics each time the scrape endpoint is requested by the metric system. This is what is done in the second sample you linked. The serveMetrics function opens a webserver that listens on port 2223 and handles requests to localhost:2223/metrics. Each time a GET request comes in on that endpoint, the metrics are aggregated according to the meter provider configuration and returned in the response body. This allows a system like Prometheus to periodically request metrics from your application.

huangapple
  • 本文由 发表于 2023年3月10日 15:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75693119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定