Skip to content

Commit

Permalink
move examples and tests to runtime directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Aug 7, 2024
1 parent 1cf302f commit 3f57ac5
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 340 deletions.
5 changes: 0 additions & 5 deletions instrumentation/runtime/example/doc.go

This file was deleted.

22 changes: 0 additions & 22 deletions instrumentation/runtime/example/go.mod

This file was deleted.

31 changes: 0 additions & 31 deletions instrumentation/runtime/example/go.sum

This file was deleted.

60 changes: 0 additions & 60 deletions instrumentation/runtime/example/main.go

This file was deleted.

38 changes: 38 additions & 0 deletions instrumentation/runtime/example_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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{
{
Expand Down
150 changes: 146 additions & 4 deletions instrumentation/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()}
}
Expand All @@ -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))
}
25 changes: 0 additions & 25 deletions instrumentation/runtime/test/go.mod

This file was deleted.

Loading

0 comments on commit 3f57ac5

Please sign in to comment.