From 228243567f61354047fcaeb285ab82a6a9838e59 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Tue, 18 Jun 2024 20:40:50 +0000 Subject: [PATCH] implement new runtime metrics --- CHANGELOG.md | 1 + instrumentation/runtime/doc.go | 12 + instrumentation/runtime/options.go | 76 ++++++ instrumentation/runtime/options_test.go | 44 ++++ instrumentation/runtime/runtime.go | 243 ++++++++++++++----- instrumentation/runtime/runtime_test.go | 12 - instrumentation/runtime/test/go.mod | 24 ++ instrumentation/runtime/test/go.sum | 29 +++ instrumentation/runtime/test/runtime_test.go | 134 ++++++++++ 9 files changed, 498 insertions(+), 77 deletions(-) create mode 100644 instrumentation/runtime/options.go create mode 100644 instrumentation/runtime/options_test.go delete mode 100644 instrumentation/runtime/runtime_test.go create mode 100644 instrumentation/runtime/test/go.mod create mode 100644 instrumentation/runtime/test/go.sum create mode 100644 instrumentation/runtime/test/runtime_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b5f2cb4c64..d8bd99dbda0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add support to configure views when creating MeterProvider using the config package. (#5654) - Add log support for the autoexport package. (#5733) - Add support for disabling the old runtime metrics using the `OTEL_GO_X_DEPRECATED_RUNTIME_METRICS=false` environment variable. (#5747) +- Add new runtime metrics to `go.opentelemetry.io/contrib/instrumentation/runtime`, which are still disabled by default. (#5870) ### Fixed diff --git a/instrumentation/runtime/doc.go b/instrumentation/runtime/doc.go index 5cbcf1e4589..2b5e78686d4 100644 --- a/instrumentation/runtime/doc.go +++ b/instrumentation/runtime/doc.go @@ -19,4 +19,16 @@ // runtime.go.mem.heap_sys (bytes) Bytes of heap memory obtained from the OS // runtime.go.mem.live_objects - Number of live objects is the number of cumulative Mallocs - Frees // runtime.uptime (ms) Milliseconds since application was initialized +// +// When the OTEL_GO_X_DEPRECATED_RUNTIME_METRICS environment variable is set to +// false, the metrics produced are: +// +// go.memory.used By Memory used by the Go runtime. +// go.memory.limit By Go runtime memory limit configured by the user, if a limit exists. +// go.memory.allocated By Memory allocated to the heap by the application. +// go.memory.allocations {allocation} Count of allocations to the heap by the application. +// go.memory.gc.goal By Heap size target for the end of the GC cycle. +// go.goroutine.count {goroutine} Count of live goroutines. +// go.processor.limit {thread} The number of OS threads that can execute user-level Go code simultaneously. +// go.config.gogc % Heap size target percentage configured by the user, otherwise 100. package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" diff --git a/instrumentation/runtime/options.go b/instrumentation/runtime/options.go new file mode 100644 index 00000000000..30046ab3509 --- /dev/null +++ b/instrumentation/runtime/options.go @@ -0,0 +1,76 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" + +import ( + "time" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/metric" +) + +// config contains optional settings for reporting runtime metrics. +type config struct { + // MinimumReadMemStatsInterval sets the minimum interval + // between calls to runtime.ReadMemStats(). Negative values + // are ignored. + MinimumReadMemStatsInterval time.Duration + + // MeterProvider sets the metric.MeterProvider. If nil, the global + // Provider will be used. + MeterProvider metric.MeterProvider +} + +// Option supports configuring optional settings for runtime metrics. +type Option interface { + apply(*config) +} + +// DefaultMinimumReadMemStatsInterval is the default minimum interval +// between calls to runtime.ReadMemStats(). Use the +// WithMinimumReadMemStatsInterval() option to modify this setting in +// Start(). +const DefaultMinimumReadMemStatsInterval time.Duration = 15 * time.Second + +// WithMinimumReadMemStatsInterval sets a minimum interval between calls to +// runtime.ReadMemStats(), which is a relatively expensive call to make +// frequently. This setting is ignored when `d` is negative. +func WithMinimumReadMemStatsInterval(d time.Duration) Option { + return minimumReadMemStatsIntervalOption(d) +} + +type minimumReadMemStatsIntervalOption time.Duration + +func (o minimumReadMemStatsIntervalOption) apply(c *config) { + if o >= 0 { + c.MinimumReadMemStatsInterval = time.Duration(o) + } +} + +// WithMeterProvider sets the Metric implementation to use for +// reporting. If this option is not used, the global metric.MeterProvider +// will be used. `provider` must be non-nil. +func WithMeterProvider(provider metric.MeterProvider) Option { + return metricProviderOption{provider} +} + +type metricProviderOption struct{ metric.MeterProvider } + +func (o metricProviderOption) apply(c *config) { + if o.MeterProvider != nil { + c.MeterProvider = o.MeterProvider + } +} + +// newConfig computes a config from the supplied Options. +func newConfig(opts ...Option) config { + c := config{ + MeterProvider: otel.GetMeterProvider(), + MinimumReadMemStatsInterval: DefaultMinimumReadMemStatsInterval, + } + for _, opt := range opts { + opt.apply(&c) + } + return c +} diff --git a/instrumentation/runtime/options_test.go b/instrumentation/runtime/options_test.go new file mode 100644 index 00000000000..3c32a740938 --- /dev/null +++ b/instrumentation/runtime/options_test.go @@ -0,0 +1,44 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestNewConfig(t *testing.T) { + for _, tt := range []struct { + name string + opts []Option + expect config + }{ + { + name: "default", + expect: config{MinimumReadMemStatsInterval: 15 * time.Second}, + }, + { + name: "negative MinimumReadMemStatsInterval ignored", + opts: []Option{WithMinimumReadMemStatsInterval(-1 * time.Second)}, + expect: config{MinimumReadMemStatsInterval: 15 * time.Second}, + }, + { + name: "set MinimumReadMemStatsInterval", + opts: []Option{WithMinimumReadMemStatsInterval(10 * time.Second)}, + expect: config{MinimumReadMemStatsInterval: 10 * time.Second}, + }, + } { + t.Run(tt.name, func(t *testing.T) { + got := newConfig(tt.opts...) + assert.True(t, configEqual(got, tt.expect)) + }) + } +} + +func configEqual(a, b config) bool { + // ignore MeterProvider + return a.MinimumReadMemStatsInterval == b.MinimumReadMemStatsInterval +} diff --git a/instrumentation/runtime/runtime.go b/instrumentation/runtime/runtime.go index 3c520a49933..3221ac753f8 100644 --- a/instrumentation/runtime/runtime.go +++ b/instrumentation/runtime/runtime.go @@ -4,9 +4,14 @@ package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" import ( + "context" + "math" + "runtime/metrics" + "sync" "time" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/contrib/instrumentation/runtime/internal/deprecatedruntime" @@ -16,70 +21,18 @@ import ( // ScopeName is the instrumentation scope name. const ScopeName = "go.opentelemetry.io/contrib/instrumentation/runtime" -// config contains optional settings for reporting runtime metrics. -type config struct { - // MinimumReadMemStatsInterval sets the minimum interval - // between calls to runtime.ReadMemStats(). Negative values - // are ignored. - MinimumReadMemStatsInterval time.Duration - - // MeterProvider sets the metric.MeterProvider. If nil, the global - // Provider will be used. - MeterProvider metric.MeterProvider -} - -// Option supports configuring optional settings for runtime metrics. -type Option interface { - apply(*config) -} - -// DefaultMinimumReadMemStatsInterval is the default minimum interval -// between calls to runtime.ReadMemStats(). Use the -// WithMinimumReadMemStatsInterval() option to modify this setting in -// Start(). -const DefaultMinimumReadMemStatsInterval time.Duration = 15 * time.Second - -// WithMinimumReadMemStatsInterval sets a minimum interval between calls to -// runtime.ReadMemStats(), which is a relatively expensive call to make -// frequently. This setting is ignored when `d` is negative. -func WithMinimumReadMemStatsInterval(d time.Duration) Option { - return minimumReadMemStatsIntervalOption(d) -} - -type minimumReadMemStatsIntervalOption time.Duration - -func (o minimumReadMemStatsIntervalOption) apply(c *config) { - if o >= 0 { - c.MinimumReadMemStatsInterval = time.Duration(o) - } -} - -// WithMeterProvider sets the Metric implementation to use for -// reporting. If this option is not used, the global metric.MeterProvider -// will be used. `provider` must be non-nil. -func WithMeterProvider(provider metric.MeterProvider) Option { - return metricProviderOption{provider} -} - -type metricProviderOption struct{ metric.MeterProvider } - -func (o metricProviderOption) apply(c *config) { - if o.MeterProvider != nil { - c.MeterProvider = o.MeterProvider - } -} - -// newConfig computes a config from the supplied Options. -func newConfig(opts ...Option) config { - c := config{ - MeterProvider: otel.GetMeterProvider(), - MinimumReadMemStatsInterval: DefaultMinimumReadMemStatsInterval, - } - for _, opt := range opts { - opt.apply(&c) - } - return c -} +const ( + goTotalMemory = "/memory/classes/total:bytes" + goMemoryReleased = "/memory/classes/heap/released:bytes" + goHeapMemory = "/memory/classes/heap/stacks:bytes" + goMemoryLimit = "/gc/gomemlimit:bytes" + goMemoryAllocated = "/gc/heap/allocs:bytes" + goMemoryAllocations = "/gc/heap/allocs:objects" + goMemoryGoal = "/gc/heap/goal:bytes" + goGoroutines = "/sched/goroutines:goroutines" + goMaxProcs = "/sched/gomaxprocs:threads" + goConfigGC = "/gc/gogc:percent" +) // Start initializes reporting of runtime metrics using the supplied config. func Start(opts ...Option) error { @@ -97,6 +50,166 @@ func Start(opts ...Option) error { if x.DeprecatedRuntimeMetrics.Enabled() { return deprecatedruntime.Start(meter, c.MinimumReadMemStatsInterval) } - // TODO (#5655) Implement new runtime conventions + memoryUsedInstrument, err := meter.Int64ObservableUpDownCounter( + "go.memory.used", + metric.WithUnit("By"), + metric.WithDescription("Memory used by the Go runtime."), + ) + if err != nil { + return err + } + memoryLimitInstrument, err := meter.Int64ObservableUpDownCounter( + "go.memory.limit", + metric.WithUnit("By"), + metric.WithDescription("Go runtime memory limit configured by the user, if a limit exists."), + ) + if err != nil { + return err + } + memoryAllocatedInstrument, err := meter.Int64ObservableCounter( + "go.memory.allocated", + metric.WithUnit("By"), + metric.WithDescription("Memory allocated to the heap by the application."), + ) + if err != nil { + return err + } + memoryAllocationsInstrument, err := meter.Int64ObservableCounter( + "go.memory.allocations", + metric.WithUnit("{allocation}"), + metric.WithDescription("Count of allocations to the heap by the application."), + ) + if err != nil { + return err + } + memoryGCGoalInstrument, err := meter.Int64ObservableUpDownCounter( + "go.memory.gc.goal", + metric.WithUnit("By"), + metric.WithDescription("Heap size target for the end of the GC cycle."), + ) + if err != nil { + return err + } + goroutineCountInstrument, err := meter.Int64ObservableUpDownCounter( + "go.goroutine.count", + metric.WithUnit("{goroutine}"), + metric.WithDescription("Count of live goroutines."), + ) + if err != nil { + return err + } + processorLimitInstrument, err := meter.Int64ObservableUpDownCounter( + "go.processor.limit", + metric.WithUnit("{thread}"), + metric.WithDescription("The number of OS threads that can execute user-level Go code simultaneously."), + ) + if err != nil { + return err + } + gogcConfigInstrument, err := meter.Int64ObservableUpDownCounter( + "go.config.gogc", + metric.WithUnit("%"), + metric.WithDescription("Heap size target percentage configured by the user, otherwise 100."), + ) + if err != nil { + return err + } + + otherMemoryOpt := metric.WithAttributeSet( + attribute.NewSet(attribute.String("go.memory.type", "other")), + ) + stackMemoryOpt := metric.WithAttributeSet( + attribute.NewSet(attribute.String("go.memory.type", "stack")), + ) + collector := newCollector(c.MinimumReadMemStatsInterval) + var lock sync.Mutex + _, err = meter.RegisterCallback( + func(ctx context.Context, o metric.Observer) error { + lock.Lock() + defer lock.Unlock() + collector.refresh() + stackMemory := collector.get(goHeapMemory) + o.ObserveInt64(memoryUsedInstrument, stackMemory, stackMemoryOpt) + totalMemory := collector.get(goTotalMemory) - collector.get(goMemoryReleased) + otherMemory := totalMemory - stackMemory + o.ObserveInt64(memoryUsedInstrument, otherMemory, otherMemoryOpt) + // Only observe the limit metric if a limit exists + if limit := collector.get(goMemoryLimit); limit != math.MaxInt64 { + o.ObserveInt64(memoryLimitInstrument, limit) + } + o.ObserveInt64(memoryAllocatedInstrument, collector.get(goMemoryAllocated)) + o.ObserveInt64(memoryAllocationsInstrument, collector.get(goMemoryAllocations)) + o.ObserveInt64(memoryGCGoalInstrument, collector.get(goMemoryGoal)) + o.ObserveInt64(goroutineCountInstrument, collector.get(goGoroutines)) + o.ObserveInt64(processorLimitInstrument, collector.get(goMaxProcs)) + o.ObserveInt64(gogcConfigInstrument, collector.get(goConfigGC)) + return nil + }, + memoryUsedInstrument, + memoryLimitInstrument, + memoryAllocatedInstrument, + memoryAllocationsInstrument, + memoryGCGoalInstrument, + goroutineCountInstrument, + processorLimitInstrument, + gogcConfigInstrument, + ) + if err != nil { + return err + } + // TODO (#5655) support go.schedule.duration return nil } + +// These are the metrics we actually fetch from the go runtime. +var runtimeMetrics = []string{ + goTotalMemory, + goMemoryReleased, + goHeapMemory, + goMemoryLimit, + goMemoryAllocated, + goMemoryAllocations, + goMemoryGoal, + goGoroutines, + goMaxProcs, + goConfigGC, +} + +type goCollector struct { + lastCollect time.Time + minimumInterval time.Duration + // sampleBuffer is populated by runtime/metrics + sampleBuffer []metrics.Sample + // sampleMap allows us to easily get the value of a single metric + sampleMap map[string]*metrics.Sample +} + +func newCollector(minimumInterval time.Duration) *goCollector { + g := &goCollector{ + sampleBuffer: make([]metrics.Sample, 0, len(runtimeMetrics)), + sampleMap: make(map[string]*metrics.Sample, len(runtimeMetrics)), + } + for _, runtimeMetric := range runtimeMetrics { + g.sampleBuffer = append(g.sampleBuffer, metrics.Sample{Name: runtimeMetric}) + g.sampleMap[runtimeMetric] = &g.sampleBuffer[len(g.sampleBuffer)-1] + } + return g +} + +func (g *goCollector) refresh() { + now := time.Now() + if now.Sub(g.lastCollect) < g.minimumInterval { + // refresh was invoked more frequently than allowed by the minimum + // interval. Do nothing. + return + } + metrics.Read(g.sampleBuffer) + g.lastCollect = now +} + +func (g *goCollector) get(name string) int64 { + if s, ok := g.sampleMap[name]; ok { + return int64(s.Value.Uint64()) + } + return 0 +} diff --git a/instrumentation/runtime/runtime_test.go b/instrumentation/runtime/runtime_test.go deleted file mode 100644 index 78d89a3c391..00000000000 --- a/instrumentation/runtime/runtime_test.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package runtime_test - -// TODO(#2757): Add integration tests for the runtime instrumentation. These -// tests depend on -// https://github.com/open-telemetry/opentelemetry-go/issues/3031 being -// resolved. -// -// The added tests will depend on the metric SDK. Therefore, they should be -// added to a sub-directory called "test" instead of this file. diff --git a/instrumentation/runtime/test/go.mod b/instrumentation/runtime/test/go.mod new file mode 100644 index 00000000000..5e51e6cfe19 --- /dev/null +++ b/instrumentation/runtime/test/go.mod @@ -0,0 +1,24 @@ +module go.opentelemetry.io/contrib/instrumentation/runtime/test + +go 1.21 + +require ( + github.com/stretchr/testify v1.9.0 + go.opentelemetry.io/contrib/instrumentation/runtime v0.52.0 + go.opentelemetry.io/otel v1.27.0 + go.opentelemetry.io/otel/sdk v1.27.0 + go.opentelemetry.io/otel/sdk/metric v1.27.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + go.opentelemetry.io/otel/metric v1.27.0 // indirect + go.opentelemetry.io/otel/trace v1.27.0 // indirect + golang.org/x/sys v0.20.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace go.opentelemetry.io/contrib/instrumentation/runtime => ../ diff --git a/instrumentation/runtime/test/go.sum b/instrumentation/runtime/test/go.sum new file mode 100644 index 00000000000..fc620f7e822 --- /dev/null +++ b/instrumentation/runtime/test/go.sum @@ -0,0 +1,29 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg= +go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ= +go.opentelemetry.io/otel/metric v1.27.0 h1:hvj3vdEKyeCi4YaYfNjv2NUje8FqKqUY8IlF0FxV/ik= +go.opentelemetry.io/otel/metric v1.27.0/go.mod h1:mVFgmRlhljgBiuk/MP/oKylr4hs85GZAylncepAX/ak= +go.opentelemetry.io/otel/sdk v1.27.0 h1:mlk+/Y1gLPLn84U4tI8d3GNJmGT/eXe3ZuOXN9kTWmI= +go.opentelemetry.io/otel/sdk v1.27.0/go.mod h1:Ha9vbLwJE6W86YstIywK2xFfPjbWlCuwPtMkKdz/Y4A= +go.opentelemetry.io/otel/sdk/metric v1.27.0 h1:5uGNOlpXi+Hbo/DRoI31BSb1v+OGcpv2NemcCrOL8gI= +go.opentelemetry.io/otel/sdk/metric v1.27.0/go.mod h1:we7jJVrYN2kh3mVBlswtPU22K0SA+769l93J6bsyvqw= +go.opentelemetry.io/otel/trace v1.27.0 h1:IqYb813p7cmbHk0a5y6pD5JPakbVfftRXABGt5/Rscw= +go.opentelemetry.io/otel/trace v1.27.0/go.mod h1:6RiD1hkAprV4/q+yd2ln1HG9GoPx39SuvvstaLBl+l4= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/instrumentation/runtime/test/runtime_test.go b/instrumentation/runtime/test/runtime_test.go new file mode 100644 index 00000000000..03d5475ff26 --- /dev/null +++ b/instrumentation/runtime/test/runtime_test.go @@ -0,0 +1,134 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime/test" + +import ( + "context" + "math" + "runtime/debug" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/sdk/instrumentation" + "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/metric/metricdata" + "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" + + "go.opentelemetry.io/contrib/instrumentation/runtime" +) + +func TestRuntimeWithLimit(t *testing.T) { + t.Setenv("OTEL_GO_X_DEPRECATED_RUNTIME_METRICS", "false") + debug.SetMemoryLimit(1234567890) + // reset to default + defer debug.SetMemoryLimit(math.MaxInt64) + + reader := metric.NewManualReader() + mp := metric.NewMeterProvider(metric.WithReader(reader)) + runtime.Start(runtime.WithMeterProvider(mp)) + rm := metricdata.ResourceMetrics{} + err := reader.Collect(context.Background(), &rm) + assert.NoError(t, err) + require.Len(t, rm.ScopeMetrics, 1) + require.Len(t, rm.ScopeMetrics[0].Metrics, 8) + + expectedScopeMetric := metricdata.ScopeMetrics{ + Scope: instrumentation.Scope{ + Name: "go.opentelemetry.io/contrib/instrumentation/runtime", + Version: runtime.Version(), + }, + Metrics: []metricdata.Metrics{ + { + Name: "go.memory.used", + Description: "Memory used by the Go runtime.", + Unit: "By", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: false, + DataPoints: []metricdata.DataPoint[int64]{ + { + Attributes: attribute.NewSet(attribute.String("go.memory.type", "stack")), + }, + { + Attributes: attribute.NewSet(attribute.String("go.memory.type", "other")), + }, + }, + }, + }, + { + Name: "go.memory.limit", + Description: "Go runtime memory limit configured by the user, if a limit exists.", + Unit: "By", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: false, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + { + Name: "go.memory.allocated", + Description: "Memory allocated to the heap by the application.", + Unit: "By", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + { + Name: "go.memory.allocations", + Description: "Count of allocations to the heap by the application.", + Unit: "{allocation}", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + { + Name: "go.memory.gc.goal", + Description: "Heap size target for the end of the GC cycle.", + Unit: "By", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: false, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + { + Name: "go.goroutine.count", + Description: "Count of live goroutines.", + Unit: "{goroutine}", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: false, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + { + Name: "go.processor.limit", + Description: "The number of OS threads that can execute user-level Go code simultaneously.", + Unit: "{thread}", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: false, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + { + Name: "go.config.gogc", + Description: "Heap size target percentage configured by the user, otherwise 100.", + Unit: "%", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: false, + DataPoints: []metricdata.DataPoint[int64]{{}}, + }, + }, + }, + } + metricdatatest.AssertEqual(t, expectedScopeMetric, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) +}