diff --git a/instrumentation/host/example/doc.go b/instrumentation/host/example/doc.go new file mode 100644 index 00000000000..e7449d3938c --- /dev/null +++ b/instrumentation/host/example/doc.go @@ -0,0 +1,16 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package main provides and example use of the host instrumentation. +package main diff --git a/instrumentation/host/example/go.mod b/instrumentation/host/example/go.mod index efd1d093302..9497270fdc6 100644 --- a/instrumentation/host/example/go.mod +++ b/instrumentation/host/example/go.mod @@ -1,14 +1,15 @@ module go.opentelemetry.io/contrib/instrumentation/host/example -go 1.17 +go 1.18 replace go.opentelemetry.io/contrib/instrumentation/host => ../ require ( - go.opentelemetry.io/contrib/instrumentation/host v0.34.0 - go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.31.0 - go.opentelemetry.io/otel/metric v0.31.0 - go.opentelemetry.io/otel/sdk/metric v0.31.0 + go.opentelemetry.io/contrib/instrumentation/host v0.35.0 + go.opentelemetry.io/otel v1.10.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.31.1-0.20220916150821-e1a1f07e44e9 + go.opentelemetry.io/otel/sdk v1.10.0 + go.opentelemetry.io/otel/sdk/metric v0.31.1-0.20220916150821-e1a1f07e44e9 ) require ( @@ -21,8 +22,7 @@ require ( github.com/tklauser/go-sysconf v0.3.10 // indirect github.com/tklauser/numcpus v0.4.0 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect - go.opentelemetry.io/otel v1.10.0 // indirect - go.opentelemetry.io/otel/sdk v1.10.0 // indirect + go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9 // indirect go.opentelemetry.io/otel/trace v1.10.0 // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect ) diff --git a/instrumentation/host/example/go.sum b/instrumentation/host/example/go.sum index 6bd739a5f7d..67afbff6a26 100644 --- a/instrumentation/host/example/go.sum +++ b/instrumentation/host/example/go.sum @@ -1,4 +1,3 @@ -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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= @@ -33,14 +32,14 @@ github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPR github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4= go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.31.0 h1:fu/wxbXqjgIRZYzQNrF175qtwrJx+oQSFhZpTIbNQLc= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.31.0/go.mod h1:a80IJcYgCLVXJurhoyPjMBiNI5gPrWXLBTAwOp8N6Vw= -go.opentelemetry.io/otel/metric v0.31.0 h1:6SiklT+gfWAwWUR0meEMxQBtihpiEs4c+vL9spDTqUs= -go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.31.1-0.20220916150821-e1a1f07e44e9 h1:ZNeldvLAQWdeZzGwy7ZEoEqCN/37DKmeTeg6eSR4uJM= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.31.1-0.20220916150821-e1a1f07e44e9/go.mod h1:myA+qu/U9IRzdv3V7hrWZH3kfznhinsn7FnOauTp/jE= +go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9 h1:Ks2fKHtMzWUGr/faEwyAHfeU0J5uHzDpysl6x2hmRRw= +go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9/go.mod h1:PVDNTt297p8ehm949jsIzd+Z2bIZJYQQG/uuHTeWFHY= go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY= go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE= -go.opentelemetry.io/otel/sdk/metric v0.31.0 h1:2sZx4R43ZMhJdteKAlKoHvRgrMp53V1aRxvEf5lCq8Q= -go.opentelemetry.io/otel/sdk/metric v0.31.0/go.mod h1:fl0SmNnX9mN9xgU6OLYLMBMrNAsaZQi7qBwprwO3abk= +go.opentelemetry.io/otel/sdk/metric v0.31.1-0.20220916150821-e1a1f07e44e9 h1:lRizsZtGep2WADmJbLf5vlJnWRb5u4RVd1XlqVH6Zwk= +go.opentelemetry.io/otel/sdk/metric v0.31.1-0.20220916150821-e1a1f07e44e9/go.mod h1:2HFTEYxjMuc9WeAVZJ8VELbXn0YqcH/qZl4RkVXRfj4= go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E= go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/instrumentation/host/example/main.go b/instrumentation/host/example/main.go index b8de0bc3b78..42d4f879216 100644 --- a/instrumentation/host/example/main.go +++ b/instrumentation/host/example/main.go @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.18 +// +build go1.18 + package main import ( @@ -21,62 +24,42 @@ import ( "os/signal" "time" - stdout "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" - "go.opentelemetry.io/otel/metric/global" - controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" - processor "go.opentelemetry.io/otel/sdk/metric/processor/basic" - "go.opentelemetry.io/otel/sdk/metric/selector/simple" - "go.opentelemetry.io/contrib/instrumentation/host" + "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.11.0" +) + +var res = resource.NewWithAttributes( + semconv.SchemaURL, + semconv.ServiceNameKey.String("host-instrumentation-example"), ) func main() { - exporter, err := stdout.New(stdout.WithPrettyPrint()) + exp, err := stdoutmetric.New() if err != nil { - log.Fatalln("failed to initialize metric stdout exporter:", err) - } - cont := controller.New( - processor.NewFactory( - simple.NewWithInexpensiveDistribution(), - exporter, - ), - controller.WithExporter(exporter), - controller.WithCollectPeriod(3*time.Second), - ) - if err := cont.Start(context.Background()); err != nil { - log.Fatalln("failed to start the metric controller:", err) + log.Fatal(err) } - global.SetMeterProvider(cont) - if err := host.Start(); err != nil { - log.Fatalln("failed to start host instrumentation:", err) - } + // Register the exporter with an SDK via a periodic reader. + read := metric.NewPeriodicReader(exp, metric.WithInterval(1*time.Second)) + provider := metric.NewMeterProvider(metric.WithResource(res), metric.WithReader(read)) + defer func() { + err := provider.Shutdown(context.Background()) + if err != nil { + log.Fatal(err) + } + }() - ctx, cancel := newOSSignalContext() + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() - <-ctx.Done() - - if err := cont.Stop(context.Background()); err != nil { - log.Fatalln("failed to stop the metric controller:", err) + log.Print("Starting host instrumentation:") + err = host.Start(host.WithMeterProvider(provider)) + if err != nil { + log.Fatal(err) } -} - -func newOSSignalContext() (context.Context, func()) { - // trap Ctrl+C and call cancel on the context - ctx, cancel := context.WithCancel(context.Background()) - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - go func() { - select { - case <-c: - cancel() - case <-ctx.Done(): - } - }() - return ctx, func() { - signal.Stop(c) - cancel() - } + <-ctx.Done() } diff --git a/instrumentation/host/go.mod b/instrumentation/host/go.mod index 4b3f0d0f7d8..364d607af7b 100644 --- a/instrumentation/host/go.mod +++ b/instrumentation/host/go.mod @@ -4,25 +4,19 @@ go 1.17 require ( github.com/shirou/gopsutil/v3 v3.22.8 - github.com/stretchr/testify v1.8.0 go.opentelemetry.io/otel v1.10.0 - go.opentelemetry.io/otel/metric v0.31.0 - go.opentelemetry.io/otel/sdk/metric v0.31.0 + go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9 ) require ( - github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/tklauser/go-sysconf v0.3.10 // indirect github.com/tklauser/numcpus v0.4.0 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect - go.opentelemetry.io/otel/sdk v1.10.0 // indirect go.opentelemetry.io/otel/trace v1.10.0 // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/instrumentation/host/go.sum b/instrumentation/host/go.sum index 484e7528703..eb799952bf6 100644 --- a/instrumentation/host/go.sum +++ b/instrumentation/host/go.sum @@ -1,4 +1,3 @@ -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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= @@ -33,12 +32,8 @@ github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPR github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4= go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ= -go.opentelemetry.io/otel/metric v0.31.0 h1:6SiklT+gfWAwWUR0meEMxQBtihpiEs4c+vL9spDTqUs= -go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A= -go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY= -go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE= -go.opentelemetry.io/otel/sdk/metric v0.31.0 h1:2sZx4R43ZMhJdteKAlKoHvRgrMp53V1aRxvEf5lCq8Q= -go.opentelemetry.io/otel/sdk/metric v0.31.0/go.mod h1:fl0SmNnX9mN9xgU6OLYLMBMrNAsaZQi7qBwprwO3abk= +go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9 h1:Ks2fKHtMzWUGr/faEwyAHfeU0J5uHzDpysl6x2hmRRw= +go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9/go.mod h1:PVDNTt297p8ehm949jsIzd+Z2bIZJYQQG/uuHTeWFHY= go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E= go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -47,7 +42,6 @@ golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/instrumentation/host/host_test.go b/instrumentation/host/host_test.go index de64a59cab8..d803dcd3e13 100644 --- a/instrumentation/host/host_test.go +++ b/instrumentation/host/host_test.go @@ -14,228 +14,9 @@ package host_test -import ( - "context" - "fmt" - gonet "net" - "os" - "testing" - "time" - - "github.com/shirou/gopsutil/v3/cpu" - "github.com/shirou/gopsutil/v3/mem" - "github.com/shirou/gopsutil/v3/net" - "github.com/shirou/gopsutil/v3/process" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/contrib/instrumentation/host" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/sdk/metric/export/aggregation" - "go.opentelemetry.io/otel/sdk/metric/metrictest" -) - -func getMetric(exp *metrictest.Exporter, name string, lbl attribute.KeyValue) float64 { - for _, r := range exp.GetRecords() { - foundAttribute := false - for _, haveLabel := range r.Attributes { - if haveLabel != lbl { - continue - } - foundAttribute = true - break - } - if !foundAttribute { - continue - } - - if r.InstrumentName != name { - continue - } - switch r.AggregationKind { - case aggregation.SumKind, aggregation.HistogramKind: - return r.Sum.CoerceToFloat64(r.NumberKind) - case aggregation.LastValueKind: - return r.LastValue.CoerceToFloat64(r.NumberKind) - default: - panic(fmt.Sprintf("invalid aggregation type: %v", r.AggregationKind)) - } - } - panic("Could not locate a metric in test output") -} - -func TestHostCPU(t *testing.T) { - provider, exp := metrictest.NewTestMeterProvider() - err := host.Start( - host.WithMeterProvider(provider), - ) - assert.NoError(t, err) - - proc, err := process.NewProcess(int32(os.Getpid())) - require.NoError(t, err) - - ctx := context.Background() - processBefore, err := proc.TimesWithContext(ctx) - require.NoError(t, err) - - hostBefore, err := cpu.TimesWithContext(ctx, false) - require.NoError(t, err) - - start := time.Now() - for time.Since(start) < time.Second { - // This has a mix of user and system time, so serves - // the purpose of advancing both process and host, - // user and system CPU usage. - _, err = proc.TimesWithContext(ctx) - require.NoError(t, err) - } - - require.NoError(t, exp.Collect(ctx)) - - processUser := getMetric(exp, "process.cpu.time", host.AttributeCPUTimeUser[0]) - processSystem := getMetric(exp, "process.cpu.time", host.AttributeCPUTimeSystem[0]) - - hostUser := getMetric(exp, "system.cpu.time", host.AttributeCPUTimeUser[0]) - hostSystem := getMetric(exp, "system.cpu.time", host.AttributeCPUTimeSystem[0]) - - processAfter, err := proc.TimesWithContext(ctx) - require.NoError(t, err) - - hostAfter, err := cpu.TimesWithContext(ctx, false) - require.NoError(t, err) - - // Validate process times: - // User times are in range - require.LessOrEqual(t, processBefore.User, processUser) - require.GreaterOrEqual(t, processAfter.User, processUser) - // System times are in range - require.LessOrEqual(t, processBefore.System, processSystem) - require.GreaterOrEqual(t, processAfter.System, processSystem) - // Ranges are not empty - require.NotEqual(t, processAfter.System, processBefore.System) - require.NotEqual(t, processAfter.User, processBefore.User) - - // Validate host times: - // Correct assumptions: - require.Equal(t, 1, len(hostBefore)) - require.Equal(t, 1, len(hostAfter)) - // User times are in range - require.LessOrEqual(t, hostBefore[0].User, hostUser) - require.GreaterOrEqual(t, hostAfter[0].User, hostUser) - // System times are in range - require.LessOrEqual(t, hostBefore[0].System, hostSystem) - require.GreaterOrEqual(t, hostAfter[0].System, hostSystem) - // Ranges are not empty - require.NotEqual(t, hostAfter[0].System, hostBefore[0].System) - require.NotEqual(t, hostAfter[0].User, hostBefore[0].User) - // TODO: We are not testing host "Other" nor "Idle" and - // generally the specification hasn't been finalized, so - // there's more to do. Moreover, "Other" is not portable and - // "Idle" may not advance on a fully loaded machine => both - // are difficult to test. -} - -func TestHostMemory(t *testing.T) { - provider, exp := metrictest.NewTestMeterProvider() - err := host.Start( - host.WithMeterProvider(provider), - ) - assert.NoError(t, err) - - ctx := context.Background() - vMem, err := mem.VirtualMemoryWithContext(ctx) - require.NoError(t, err) - - require.NoError(t, exp.Collect(ctx)) - - hostUsed := getMetric(exp, "system.memory.usage", host.AttributeMemoryUsed[0]) - assert.Greater(t, hostUsed, 0.0) - assert.LessOrEqual(t, hostUsed, float64(vMem.Total)) - - hostAvailable := getMetric(exp, "system.memory.usage", host.AttributeMemoryAvailable[0]) - assert.GreaterOrEqual(t, hostAvailable, 0.0) - assert.Less(t, hostAvailable, float64(vMem.Total)) - - hostUsedUtil := getMetric(exp, "system.memory.utilization", host.AttributeMemoryUsed[0]) - assert.Greater(t, hostUsedUtil, 0.0) - assert.LessOrEqual(t, hostUsedUtil, 1.0) - - hostAvailableUtil := getMetric(exp, "system.memory.utilization", host.AttributeMemoryAvailable[0]) - assert.GreaterOrEqual(t, hostAvailableUtil, 0.0) - assert.Less(t, hostAvailableUtil, 1.0) - - if hostUsed > hostAvailable { - assert.Greater(t, hostUsedUtil, hostAvailableUtil) - } else { - assert.Less(t, hostUsedUtil, hostAvailableUtil) - } -} - -func sendBytes(t *testing.T, count int) error { - conn1, err := gonet.ListenPacket("udp", "127.0.0.1:0") - if err != nil { - return err - } - defer conn1.Close() - - conn2, err := gonet.ListenPacket("udp", "127.0.0.1:0") - if err != nil { - return err - } - defer conn2.Close() - - data1 := make([]byte, 1000) - data2 := make([]byte, 1000) - for i := range data1 { - data1[i] = byte(i) - } - - for ; count > 0; count -= len(data1) { - _, err = conn1.WriteTo(data1, conn2.LocalAddr()) - if err != nil { - return err - } - _, readAddr, err := conn2.ReadFrom(data2) - if err != nil { - return err - } - - require.Equal(t, "udp", readAddr.Network()) - require.Equal(t, conn1.LocalAddr().String(), readAddr.String()) - } - - return nil -} - -func TestHostNetwork(t *testing.T) { - provider, exp := metrictest.NewTestMeterProvider() - err := host.Start( - host.WithMeterProvider(provider), - ) - assert.NoError(t, err) - - ctx := context.Background() - hostBefore, err := net.IOCountersWithContext(ctx, false) - require.NoError(t, err) - - const howMuch = 10000 - err = sendBytes(t, howMuch) - require.NoError(t, err) - - // As we are going to read the /proc file system for this info, sleep a while: - require.Eventually(t, func() bool { - hostAfter, err := net.IOCountersWithContext(ctx, false) - require.NoError(t, err) - - return uint64(howMuch) <= hostAfter[0].BytesSent-hostBefore[0].BytesSent && - uint64(howMuch) <= hostAfter[0].BytesRecv-hostBefore[0].BytesRecv - }, 30*time.Second, time.Second/2) - - require.NoError(t, exp.Collect(ctx)) - hostTransmit := getMetric(exp, "system.network.io", host.AttributeNetworkTransmit[0]) - hostReceive := getMetric(exp, "system.network.io", host.AttributeNetworkReceive[0]) - - // Check that the recorded measurements reflect the same change: - require.LessOrEqual(t, uint64(howMuch), uint64(hostTransmit)-hostBefore[0].BytesSent) - require.LessOrEqual(t, uint64(howMuch), uint64(hostReceive)-hostBefore[0].BytesRecv) -} +// TODO(#2755): Add integration tests for the host 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.