通过环境变量配置 OTLP 导出器

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

Configuring OTLP exporter through environment variables

问题

目前我正在尝试使用环境变量配置我的 OTLP 导出器。根据官方文档,这应该是可行的。

特别是,我想关注 OTEL_EXPORTER_OTLP_ENDPOINT 这个环境变量,在 OTLPtrace 导出器中是允许的。根据他们代码中的注释,环境变量的值优先于代码中设置的任何其他值。

我用 Go 写了一个非常基本的 HTTP 应用程序,并使用 OpenTelemetry 进行了仪表化。当我在代码中明确指定导出器的端点时,仪表化工作得很好,例如:

exporter, err := otlptrace.New(
		context.Background(),
		otlptracegrpc.NewClient(
			otlptracegrpc.WithInsecure(),
			otlptracegrpc.WithEndpoint("My Endpoint"),
		),
	)

然而,如果我从代码中删除 otlptracegrpc.NewClient 的配置,它就无法获取环境中设置的值,环境变量的设置如下:

OTEL_EXPORTER_OTLP_ENDPOINT="my endpoint"

因此,当我在调试器中运行此应用程序时,我可以看到导出器客户端的端点值为空,但我可以在程序中获取到它们,如下所示:

exporterEndpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")

我理解这样的情况是因为变量实际上是在代码执行时存在的,这是我最担心的地方。

为什么会这样?我是否漏掉了什么?我应该以不同的方式设置环境变量吗(我在官方文档中看到有关环境变量的“选项”,但没有示例)?

英文:

Currently I am trying to configure my OTLP exporter using environment variables. This is supposed to be possible as per the official docs.

In particular, I want to focus on the OTEL_EXPORTER_OTLP_ENDPOINT one, which is allowed for the OTLPtrace exporter. According to the comments in their code, the environment variable takes precedence over any other value set in the code.

I wrote a very basic HTTP application in Go, which is instrumented with OpenTelemetry. When I specify the exporter endpoint explicitly in the code like:

exporter, err := otlptrace.New(
		context.Background(),
		otlptracegrpc.NewClient(
			otlptracegrpc.WithInsecure(),
			otlptracegrpc.WithEndpoint("My Endpoint"),
		),
	)

The instrumentation works just fine like that. However if I remove the otlptracegrpc.NewClient configuration from the code, it does not pick up the values set in the environment, which are set like:

OTEL_EXPORTER_OTLP_ENDPOINT="my endpoint"

So when I run this application in my debugger I can see that the exporter client has an empty value as the endpoint, yet I can pick them up within my program as:

exporterEndpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")

This I interpret as the variables being actually present at the time the code is being executed, which was my main fear.

Why is this? Am I missing something here? Should I populate the environment variable differently (I see there are "options" for the environment variable in the official docs, but no examples)?

答案1

得分: 1

从你的代码中我看到,你正在尝试通过 gRPC 调用来联系 OTLP 导出器。如果你看一下他们的文档,他们在第71行写道:

如果使用 WithGRPCConn,则此选项无效。

这意味着你完全可以不传递这个变量给 otlptracegrpc.NewClient 函数。我用下面的代码实例化了一个 gRPC 客户端,它可以正常工作:

func newOtlpExporter(ctx context.Context) (trace.SpanExporter, error) {
	client := otlptracegrpc.NewClient(otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock()))
	exporter, err := otlptrace.New(ctx, client)
	if err != nil {
		panic(err)
	}
	return exporter, err
}

回到你的问题,如果你通过 HTTPS 调用发送指标、跟踪等,你的猜测是正确的。
如果有帮助解决问题或需要其他任何帮助,请告诉我!

编辑 1

我忽略了这一点。你在问题中引用的注释是从错误的文件中提取的。正确的行是这个:https://github.com/open-telemetry/opentelemetry-go/blob/48a05478e238698e02b4025ac95a11ecd6bcc5ad/exporters/otlp/otlptrace/otlptracegrpc/options.go#L71
正如你所看到的,这个注释更清晰,你只有两个选项:

  1. 提供自己的端点地址
  2. 使用默认地址 localhost:0.0.0.0:4317

如果有帮助,请告诉我!

英文:

From what I see from your code, you're trying to contact the OTLP exporter through a gRPC call. If you see, in their documentation they wrote this in line 71:
> This option has no effect if WithGRPCConn is used.

This means that you can completely avoid passing this variable at all to the otlptracegrpc.NewClient function. I instantiate a gRPC client with this code and it works:

func newOtlpExporter(ctx context.Context) (trace.SpanExporter, error) {
	client := otlptracegrpc.NewClient(otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock()))
	exporter, err := otlptrace.New(ctx, client)
	if err != nil {
		panic(err)
	}
	return exporter, err
}

Back to your question, you're right with your guess but only if you're sending metrics, traces, and so on through HTTPS calls.
Let me know if this helps to solve the issue or if anything else is needed!

Edit 1

I overlooked this. The comment you linked in the question is taken from the wrong file. The correct line is this: https://github.com/open-telemetry/opentelemetry-go/blob/48a05478e238698e02b4025ac95a11ecd6bcc5ad/exporters/otlp/otlptrace/otlptracegrpc/options.go#L71
As you can see, the comment is clearer and you have only two options:

  1. Provide your own endpoint address
  2. Use the default one which is localhost:0.0.0.0:4317

Let me know if helps!

huangapple
  • 本文由 发表于 2022年11月15日 20:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74445800.html
匿名

发表评论

匿名网友

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

确定