diff --git a/instrumentation/runtime/example/doc.go b/instrumentation/runtime/example/doc.go deleted file mode 100644 index 93292afd9ce..00000000000 --- a/instrumentation/runtime/example/doc.go +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -// Package main provides an example use of the runtime instrumentation. -package main diff --git a/instrumentation/runtime/example/go.mod b/instrumentation/runtime/example/go.mod deleted file mode 100644 index a4db4ede5c2..00000000000 --- a/instrumentation/runtime/example/go.mod +++ /dev/null @@ -1,22 +0,0 @@ -module go.opentelemetry.io/contrib/instrumentation/runtime/example - -go 1.21 - -replace go.opentelemetry.io/contrib/instrumentation/runtime => ../ - -require ( - go.opentelemetry.io/contrib/instrumentation/runtime v0.53.0 - go.opentelemetry.io/otel v1.28.0 - go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0 - go.opentelemetry.io/otel/sdk v1.28.0 - go.opentelemetry.io/otel/sdk/metric v1.28.0 -) - -require ( - github.com/go-logr/logr v1.4.2 // indirect - github.com/go-logr/stdr v1.2.2 // indirect - github.com/google/uuid v1.6.0 // indirect - go.opentelemetry.io/otel/metric v1.28.0 // indirect - go.opentelemetry.io/otel/trace v1.28.0 // indirect - golang.org/x/sys v0.23.0 // indirect -) diff --git a/instrumentation/runtime/example/go.sum b/instrumentation/runtime/example/go.sum deleted file mode 100644 index fa3a58b33c8..00000000000 --- a/instrumentation/runtime/example/go.sum +++ /dev/null @@ -1,31 +0,0 @@ -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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -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.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= -go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0 h1:BJee2iLkfRfl9lc7aFmBwkWxY/RI1RDdXepSF6y8TPE= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0/go.mod h1:DIzlHs3DRscCIBU3Y9YSzPfScwnYnzfnCd4g8zA7bZc= -go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= -go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= -go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE= -go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg= -go.opentelemetry.io/otel/sdk/metric v1.28.0 h1:OkuaKgKrgAbYrrY0t92c+cC+2F6hsFNnCQArXCKlg08= -go.opentelemetry.io/otel/sdk/metric v1.28.0/go.mod h1:cWPjykihLAPvXKi4iZc1dpER3Jdq2Z0YLse3moQUCpg= -go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= -go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -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/example/main.go b/instrumentation/runtime/example/main.go deleted file mode 100644 index 4068dd39287..00000000000 --- a/instrumentation/runtime/example/main.go +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -//go:build go1.18 -// +build go1.18 - -package main - -import ( - "context" - "log" - "os" - "os/signal" - "time" - - "go.opentelemetry.io/contrib/instrumentation/runtime" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" - "go.opentelemetry.io/otel/sdk/metric" - "go.opentelemetry.io/otel/sdk/resource" - semconv "go.opentelemetry.io/otel/semconv/v1.17.0" -) - -var res = resource.NewWithAttributes( - semconv.SchemaURL, - semconv.ServiceName("runtime-instrumentation-example"), -) - -func main() { - exp, err := stdoutmetric.New(stdoutmetric.WithPrettyPrint()) - if err != nil { - log.Fatal(err) - } - - // Register the exporter with an SDK via a periodic reader. - read := metric.NewPeriodicReader( - exp, - metric.WithInterval(1*time.Second), - metric.WithProducer(runtime.NewProducer()), - ) - provider := metric.NewMeterProvider(metric.WithResource(res), metric.WithReader(read)) - defer func() { - err := provider.Shutdown(context.Background()) - if err != nil { - log.Fatal(err) - } - }() - otel.SetMeterProvider(provider) - - ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) - defer cancel() - - log.Print("Starting runtime instrumentation:") - err = runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second)) - if err != nil { - log.Fatal(err) - } - - <-ctx.Done() -} diff --git a/instrumentation/runtime/example_test.go b/instrumentation/runtime/example_test.go new file mode 100644 index 00000000000..08a438bcf6a --- /dev/null +++ b/instrumentation/runtime/example_test.go @@ -0,0 +1,38 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package runtime_test + +import ( + "context" + "log" + "time" + + "go.opentelemetry.io/contrib/instrumentation/runtime" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/sdk/metric" +) + +func Example() { + // This reader is used as a stand-in for a reader that will actually export + // data. See https://pkg.go.dev/go.opentelemetry.io/otel/exporters for + // exporters that can be used as or with readers. + reader := metric.NewManualReader( + // Add the runtime producer to get histograms from the Go runtime. + metric.WithProducer(runtime.NewProducer()), + ) + provider := metric.NewMeterProvider(metric.WithReader(reader)) + defer func() { + err := provider.Shutdown(context.Background()) + if err != nil { + log.Fatal(err) + } + }() + otel.SetMeterProvider(provider) + + // Start go runtime metric collection. + err := runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second)) + if err != nil { + log.Fatal(err) + } +} diff --git a/instrumentation/runtime/test/producer_test.go b/instrumentation/runtime/producer_test.go similarity index 86% rename from instrumentation/runtime/test/producer_test.go rename to instrumentation/runtime/producer_test.go index bb35d97f3ee..1e467928c5e 100644 --- a/instrumentation/runtime/test/producer_test.go +++ b/instrumentation/runtime/producer_test.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime/test" +package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" import ( "context" @@ -10,15 +10,14 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.opentelemetry.io/contrib/instrumentation/runtime" "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" ) -func TestProducer(t *testing.T) { - reader := metric.NewManualReader(metric.WithProducer(runtime.NewProducer())) +func TestNewProducer(t *testing.T) { + reader := metric.NewManualReader(metric.WithProducer(NewProducer())) _ = metric.NewMeterProvider(metric.WithReader(reader)) rm := metricdata.ResourceMetrics{} err := reader.Collect(context.Background(), &rm) @@ -29,7 +28,7 @@ func TestProducer(t *testing.T) { expectedScopeMetric := metricdata.ScopeMetrics{ Scope: instrumentation.Scope{ Name: "go.opentelemetry.io/contrib/instrumentation/runtime", - Version: runtime.Version(), + Version: Version(), }, Metrics: []metricdata.Metrics{ { diff --git a/instrumentation/runtime/runtime_test.go b/instrumentation/runtime/runtime_test.go index 4b5863bb600..78b3b52b784 100644 --- a/instrumentation/runtime/runtime_test.go +++ b/instrumentation/runtime/runtime_test.go @@ -4,10 +4,21 @@ package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime" import ( + "context" + "fmt" + "math" + "runtime/debug" "testing" "time" "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" ) func TestRefreshGoCollector(t *testing.T) { @@ -36,10 +47,6 @@ func TestRefreshGoCollector(t *testing.T) { assert.NotEqual(t, initialAllocations, collector.getInt(goMemoryAllocations)) } -func allocateMemory(buffer [][]byte) [][]byte { - return append(buffer, make([]byte, 1000000)) -} - func newClock() *clock { return &clock{current: time.Now()} } @@ -51,3 +58,138 @@ type clock struct { func (c *clock) now() time.Time { return c.current } func (c *clock) increment(d time.Duration) { c.current = c.current.Add(d) } + +func TestRuntimeWithLimit(t *testing.T) { + // buffer for allocating memory + var buffer [][]byte + _ = allocateMemory(buffer) + 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)) + err := Start(WithMeterProvider(mp)) + assert.NoError(t, err) + 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: 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()) + assertNonZeroValues(t, rm.ScopeMetrics[0]) +} + +func assertNonZeroValues(t *testing.T, sm metricdata.ScopeMetrics) { + for _, m := range sm.Metrics { + switch a := m.Data.(type) { + case metricdata.Sum[int64]: + for _, dp := range a.DataPoints { + assert.True(t, dp.Value > 0, fmt.Sprintf("Metric %q should have a non-zero value for point with attributes %+v", m.Name, dp.Attributes)) + } + default: + t.Fatalf("unexpected data type %v", a) + } + } +} + +func allocateMemory(buffer [][]byte) [][]byte { + return append(buffer, make([]byte, 1000000)) +} diff --git a/instrumentation/runtime/test/go.mod b/instrumentation/runtime/test/go.mod deleted file mode 100644 index 0abcd62dddb..00000000000 --- a/instrumentation/runtime/test/go.mod +++ /dev/null @@ -1,25 +0,0 @@ -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.53.0 - go.opentelemetry.io/otel v1.28.0 - go.opentelemetry.io/otel/sdk v1.28.0 - go.opentelemetry.io/otel/sdk/metric v1.28.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/google/uuid v1.6.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - go.opentelemetry.io/otel/metric v1.28.0 // indirect - go.opentelemetry.io/otel/trace v1.28.0 // indirect - golang.org/x/sys v0.23.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 deleted file mode 100644 index 11ffc9042a3..00000000000 --- a/instrumentation/runtime/test/go.sum +++ /dev/null @@ -1,31 +0,0 @@ -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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -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.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= -go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= -go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= -go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= -go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE= -go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg= -go.opentelemetry.io/otel/sdk/metric v1.28.0 h1:OkuaKgKrgAbYrrY0t92c+cC+2F6hsFNnCQArXCKlg08= -go.opentelemetry.io/otel/sdk/metric v1.28.0/go.mod h1:cWPjykihLAPvXKi4iZc1dpER3Jdq2Z0YLse3moQUCpg= -go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= -go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.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 deleted file mode 100644 index 936df2d54f5..00000000000 --- a/instrumentation/runtime/test/runtime_test.go +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime/test" - -import ( - "context" - "fmt" - "math" - "runtime/debug" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/contrib/instrumentation/runtime" - "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" -) - -func TestRuntimeWithLimit(t *testing.T) { - // buffer for allocating memory - var buffer [][]byte - _ = allocateMemory(buffer) - 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)) - err := runtime.Start(runtime.WithMeterProvider(mp)) - assert.NoError(t, err) - 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()) - assertNonZeroValues(t, rm.ScopeMetrics[0]) -} - -func assertNonZeroValues(t *testing.T, sm metricdata.ScopeMetrics) { - for _, m := range sm.Metrics { - switch a := m.Data.(type) { - case metricdata.Sum[int64]: - for _, dp := range a.DataPoints { - assert.True(t, dp.Value > 0, fmt.Sprintf("Metric %q should have a non-zero value for point with attributes %+v", m.Name, dp.Attributes)) - } - default: - t.Fatalf("unexpected data type %v", a) - } - } -} - -func allocateMemory(buffer [][]byte) [][]byte { - return append(buffer, make([]byte, 1000000)) -}