Skip to content

Commit

Permalink
Bringing back the example
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Mar 20, 2023
1 parent af88674 commit 0185917
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 69 deletions.
138 changes: 69 additions & 69 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
package otelroundtripper

//import (
// "context"
// "encoding/json"
// "go.opentelemetry.io/otel/sdk/metric"
// "log"
// "math/rand"
// "net/http"
// "os"
// "strconv"
// "time"
//
// "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
// "go.opentelemetry.io/otel/metric/global"
// semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
//)
//
//func InstallExportPipeline(ctx context.Context) func() {
// // Print with a JSON encoder that indents with two spaces.
// enc := json.NewEncoder(os.Stdout)
// enc.SetIndent("", " ")
// exporter, err := stdoutmetric.New(stdoutmetric.WithEncoder(enc))
// if err != nil {
// log.Fatalf("creating stdoutmetric exporter: %v", err)
// }
//
// // Register the exporter with an SDK via a periodic reader.
// sdk := metric.NewMeterProvider(
// metric.WithReader(metric.NewPeriodicReader(exporter)),
// )
//
// global.SetMeterProvider(sdk)
//
// return func() {
// if err := sdk.Shutdown(ctx); err != nil {
// log.Fatalf("stopping sdk: %v", err)
// }
// }
//}
//
//func Example() {
// ctx := context.Background()
//
// // Registers a meter Provider globally.
// cleanup := InstallExportPipeline(ctx)
// defer cleanup()
//
// client := http.Client{
// Transport: New(
// WithMeter(global.MeterProvider().Meter("otel-round-tripper")),
// WithAttributes(
// semconv.ServiceNameKey.String("otel-round-tripper"),
// ),
// ),
// }
//
// rand.Seed(time.Now().UnixNano())
// for i := 0; i < 10; i++ {
// // Add a random sleep duration so that we will see the metrics in the console
// url := "https://httpstat.us/200?sleep=" + strconv.Itoa(rand.Intn(1000)+1000) //nolint:gosec
//
// log.Printf("GET: %s", url)
// response, err := client.Get(url)
// if err != nil {
// log.Panicf("cannot perform http request: %v", err)
// }
//
// _ = response.Body.Close()
// }
//}
import (
"context"
"encoding/json"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"

"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)

func InstallExportPipeline(ctx context.Context) func() {
// Print with a JSON encoder that indents with two spaces.
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
exporter, err := stdoutmetric.New(stdoutmetric.WithEncoder(enc))
if err != nil {
log.Fatalf("creating stdoutmetric exporter: %v", err)
}

// Register the exporter with an SDK via a periodic reader.
sdk := metric.NewMeterProvider(
metric.WithReader(metric.NewPeriodicReader(exporter)),
)

global.SetMeterProvider(sdk)

return func() {
if err := sdk.Shutdown(ctx); err != nil {
log.Fatalf("stopping sdk: %v", err)
}
}
}

func Example() {
ctx := context.Background()

// Registers a meter Provider globally.
cleanup := InstallExportPipeline(ctx)
defer cleanup()

client := http.Client{
Transport: New(
WithMeter(global.MeterProvider().Meter("otel-round-tripper")),
WithAttributes(
semconv.ServiceNameKey.String("otel-round-tripper"),
),
),
}

rand.Seed(time.Now().UnixNano())
for i := 0; i < 10; i++ {
// Add a random sleep duration so that we will see the metrics in the console
url := "https://httpstat.us/200?sleep=" + strconv.Itoa(rand.Intn(1000)+1000) //nolint:gosec

log.Printf("GET: %s", url)
response, err := client.Get(url)
if err != nil {
log.Panicf("cannot perform http request: %v", err)
}

_ = response.Body.Close()
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ go 1.19
require (
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0
go.opentelemetry.io/otel/metric v0.37.0
go.opentelemetry.io/otel/sdk/metric v0.37.0
)

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/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0 h1:S1Y8Wkl44weO903rqc1mCV4Gqbb7Vd+R+qU1yceN7XQ=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0/go.mod h1:6xZwq1h4G4NxtU8PhjJnWSSVMaJ+yaNbjeSXfCYow+M=
go.opentelemetry.io/otel/metric v0.37.0 h1:pHDQuLQOZwYD+Km0eb657A25NaRzy0a+eLyKfDXedEs=
go.opentelemetry.io/otel/metric v0.37.0/go.mod h1:DmdaHfGt54iV6UKxsV9slj2bBRJcKC1B1uvDLIioc1s=
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
go.opentelemetry.io/otel/sdk/metric v0.37.0 h1:haYBBtZZxiI3ROwSmkZnI+d0+AVzBWeviuYQDeBWosU=
go.opentelemetry.io/otel/sdk/metric v0.37.0/go.mod h1:mO2WV1AZKKwhwHTV3AKOoIEb9LbUaENZDuGUQd+j4A0=
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
Expand Down

0 comments on commit 0185917

Please sign in to comment.