Skip to content

Commit

Permalink
Release Preparation: Update host instrumentation to use new metric SDK (
Browse files Browse the repository at this point in the history
#2754)

* Update host inst to use new metric SDK

* Add PR number to TODO

* Add build guard and doc file
  • Loading branch information
MrAlias authored Sep 19, 2022
1 parent 2a588c2 commit 7c2e03e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 300 deletions.
16 changes: 16 additions & 0 deletions instrumentation/host/example/doc.go
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions instrumentation/host/example/go.mod
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
)
13 changes: 6 additions & 7 deletions instrumentation/host/example/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
75 changes: 29 additions & 46 deletions instrumentation/host/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()
}
8 changes: 1 addition & 7 deletions instrumentation/host/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 2 additions & 8 deletions instrumentation/host/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
Loading

0 comments on commit 7c2e03e

Please sign in to comment.