Skip to content

fix(contrib/metric/otelmetric): Fix MustProvider function parameter and improve runtime version retrieval#4729

Open
H4x3rs wants to merge 3 commits intogogf:masterfrom
H4x3rs:fix/4727
Open

fix(contrib/metric/otelmetric): Fix MustProvider function parameter and improve runtime version retrieval#4729
H4x3rs wants to merge 3 commits intogogf:masterfrom
H4x3rs:fix/4727

Conversation

@H4x3rs
Copy link
Copy Markdown

@H4x3rs H4x3rs commented Mar 2, 2026

docs(otelmetric/README.MD): 修复MustProvider函数参数错误

  • 将metric.WithReader替换为otelmetric.WithReader以匹配正确的函数签名

fix(otelmetric): 兼容不同版本runtime版本获取逻辑

  • 添加 reflect 包导入以支持动态调用
  • 实现 getRuntimeVersion 函数,通过反射安全获取运行时版本
  • 替换直接调用 runtime.Version() 为 getRuntimeVersion() 调用
  • 提高版本获取的稳定性和兼容性

haojie.ren added 2 commits March 2, 2026 16:58
- 将metric.WithReader替换为otelmetric.WithReader以匹配正确的函数签名
- 添加 reflect 包导入以支持动态调用
- 实现 getRuntimeVersion 函数,通过反射安全获取运行时版本
- 替换直接调用 runtime.Version() 为 getRuntimeVersion() 调用
- 提高版本获取的稳定性和兼容性
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the otelmetric contrib module to (1) correct the README example to use the module’s Option helpers, and (2) make built-in runtime metrics scope version retrieval resilient to upstream API differences.

Changes:

  • Update README example to pass otelmetric.WithReader(...) into otelmetric.MustProvider(...).
  • Introduce getRuntimeVersion() and replace direct runtime.Version() calls when constructing built-in metric views.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
contrib/metric/otelmetric/otelmetric_provider.go Adds a compatibility runtime version getter and uses it for instrumentation scope version in built-in metric views.
contrib/metric/otelmetric/README.MD Fixes the README example to use otelmetric.WithReader (matching MustProvider(...Option)).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/metric/otelmetric/otelmetric_provider.go Outdated
Comment on lines +83 to +88
func getRuntimeVersion() string {
val := reflect.ValueOf(runtime.Version)

if val.Kind() == reflect.Func {
results := val.Call(nil)
if len(results) > 0 && results[0].Kind() == reflect.String {
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRuntimeVersion uses reflect.Value.Call, which adds overhead and is harder to reason about than necessary. Since the goal is to support both runtime.Version being a string and a func() string, consider using a type-switch on any(runtime.Version) (or interface{}) instead of reflection; it’s simpler and avoids reflect entirely.

Copilot uses AI. Check for mistakes.
Comment on lines 111 to 115
Name: "process.runtime.go.gc.pause_ns",
Scope: instrumentation.Scope{
Name: runtime.ScopeName,
Version: runtime.Version(),
Version: getRuntimeVersion(),
},
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRuntimeVersion() is invoked twice during createViewsForBuiltInMetrics(). If you keep the reflective implementation, consider calling it once and reusing the result to avoid repeating the reflection work.

Copilot uses AI. Check for mistakes.
Comment on lines 124 to 126
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(otelmetric.WithReader(exporter))
provider.SetAsGlobal()
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the README example, after switching to otelmetric.WithReader(exporter), the go.opentelemetry.io/otel/sdk/metric import in the snippet is no longer used, so the sample code won’t compile as-is. Consider removing that import (or otherwise using it) to keep the example buildable.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants