Skip to content

Commit

Permalink
updated metric packages
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Mar 20, 2023
1 parent ca5e074 commit 0ae7996
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 126 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.10.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 you 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"
// "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()
// }
//}
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
module github.com/NdoleStudio/go-otelroundtripper

go 1.17
go 1.19

require (
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/otel v1.11.2
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0
go.opentelemetry.io/otel/metric v0.34.0
go.opentelemetry.io/otel/sdk/metric v0.34.0
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/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.11.2 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
25 changes: 8 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,21 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
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.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0=
go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0 h1:O1E9/qhspQSz3O6/dSGLNBND2TO9mUaSvlhcKJMv278=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.34.0/go.mod h1:Id0oYi2ARij/um3gFV+t5rH1MTFdJpfTimsFsqKS7pE=
go.opentelemetry.io/otel/metric v0.34.0 h1:MCPoQxcg/26EuuJwpYN1mZTeCYAUGx8ABxfW07YkjP8=
go.opentelemetry.io/otel/metric v0.34.0/go.mod h1:ZFuI4yQGNCupurTXCwkeD/zHBt+C2bR7bw5JqUm/AP8=
go.opentelemetry.io/otel/sdk v1.11.2 h1:GF4JoaEx7iihdMFu30sOyRx52HDHOkl9xQ8SMqNXUiU=
go.opentelemetry.io/otel/sdk v1.11.2/go.mod h1:wZ1WxImwpq+lVRo4vsmSOxdd+xwoUJ6rqyLc3SyX9aU=
go.opentelemetry.io/otel/sdk/metric v0.34.0 h1:7ElxfQpXCFZlRTvVRTkcUvK8Gt5DC8QzmzsLsO2gdzo=
go.opentelemetry.io/otel/sdk/metric v0.34.0/go.mod h1:l4r16BIqiqPy5rd14kkxllPy/fOI4tWo1jkpD9Z3ffQ=
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
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/metric v0.37.0 h1:pHDQuLQOZwYD+Km0eb657A25NaRzy0a+eLyKfDXedEs=
go.opentelemetry.io/otel/metric v0.37.0/go.mod h1:DmdaHfGt54iV6UKxsV9slj2bBRJcKC1B1uvDLIioc1s=
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=
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
5 changes: 3 additions & 2 deletions option_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package otelroundtripper

import (
"go.opentelemetry.io/otel/metric"
"net/http"
"testing"

"go.opentelemetry.io/otel/metric"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"

"github.com/stretchr/testify/assert"
)
Expand Down
54 changes: 27 additions & 27 deletions round_tripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/metric/instrument"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)

type otelHTTPMetrics struct {
attemptsCounter syncint64.Counter
noRequestCounter syncint64.Counter
errorsCounter syncint64.Counter
successesCounter syncint64.Counter
failureCounter syncint64.Counter
redirectCounter syncint64.Counter
timeoutsCounter syncint64.Counter
canceledCounter syncint64.Counter
deadlineExceededCounter syncint64.Counter
totalDurationCounter syncint64.Histogram
inFlightCounter syncint64.UpDownCounter
attemptsCounter instrument.Int64Counter
noRequestCounter instrument.Int64Counter
errorsCounter instrument.Int64Counter
successesCounter instrument.Int64Counter
failureCounter instrument.Int64Counter
redirectCounter instrument.Int64Counter
timeoutsCounter instrument.Int64Counter
canceledCounter instrument.Int64Counter
deadlineExceededCounter instrument.Int64Counter
totalDurationCounter instrument.Int64Histogram
inFlightCounter instrument.Int64UpDownCounter
}

// otelRoundTripper is the http.RoundTripper which emits open telemetry metrics
Expand All @@ -46,36 +46,36 @@ func New(options ...Option) http.RoundTripper {
parent: cfg.parent,
attributes: cfg.attributes,
metrics: otelHTTPMetrics{
noRequestCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".no_request")),
errorsCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".errors")),
successesCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".success")),
timeoutsCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".timeouts")),
canceledCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".cancelled")),
deadlineExceededCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".deadline_exceeded")),
totalDurationCounter: mustHistogram(cfg.meter.SyncInt64().Histogram(cfg.name + ".total_duration")),
inFlightCounter: mustUpDownCounter(cfg.meter.SyncInt64().UpDownCounter(cfg.name + ".in_flight")),
attemptsCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".attempts")),
failureCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".failures")),
redirectCounter: mustCounter(cfg.meter.SyncInt64().Counter(cfg.name + ".redirects")),
noRequestCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".no_request")),
errorsCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".errors")),
successesCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".success")),
timeoutsCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".timeouts")),
canceledCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".cancelled")),
deadlineExceededCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".deadline_exceeded")),
totalDurationCounter: mustHistogram(cfg.meter.Int64Histogram(cfg.name + ".total_duration")),
inFlightCounter: mustUpDownCounter(cfg.meter.Int64UpDownCounter(cfg.name + ".in_flight")),
attemptsCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".attempts")),
failureCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".failures")),
redirectCounter: mustCounter(cfg.meter.Int64Counter(cfg.name + ".redirects")),
},
}
}

func mustCounter(counter syncint64.Counter, err error) syncint64.Counter {
func mustCounter(counter instrument.Int64Counter, err error) instrument.Int64Counter {
if err != nil {
panic(err)
}
return counter
}

func mustUpDownCounter(counter syncint64.UpDownCounter, err error) syncint64.UpDownCounter {
func mustUpDownCounter(counter instrument.Int64UpDownCounter, err error) instrument.Int64UpDownCounter {
if err != nil {
panic(err)
}
return counter
}

func mustHistogram(histogram syncint64.Histogram, err error) syncint64.Histogram {
func mustHistogram(histogram instrument.Int64Histogram, err error) instrument.Int64Histogram {
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions round_tripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package otelroundtripper
import (
"context"
"errors"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"io"
"net"
"net/http"
Expand All @@ -13,6 +11,9 @@ import (
"testing"
"time"

"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
)
Expand Down

0 comments on commit 0ae7996

Please sign in to comment.