Skip to content

Commit

Permalink
Merge pull request #1 from vankleefjim/upgrade-otel-metrics-v0.28
Browse files Browse the repository at this point in the history
Upgrade to otel/metrics@v0.28.0
  • Loading branch information
AchoArnold committed Apr 6, 2022
2 parents eb148de + 5df151d commit 532cfb5
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 63 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.MD
@@ -0,0 +1,7 @@
# Changelog

## Unreleased

### Changed
- There were breaking changes in the [opentelemetry-go Release v1.6.0/v0.28.0](https://github.com/open-telemetry/opentelemetry-go/releases/tag/v1.6.0) for the metric pkg. These also effect this package.
- One of the changes is that `metric.Meter` is now an interface instead of a struct. It can be provided via `global.MeterProvider().Meter("name")` (see [example](example_test.go#L55))
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -11,7 +11,7 @@

This package provides an easy way to collect http related metrics
(e.g Response times, Status Codes, number of in flight requests etc) for your HTTP API Clients.
You can do this by using this round tripper when instantiating the `http.CLient{}`.
You can do this by using this round tripper when instantiating the `http.Client{}`.

## Why this package exists

Expand Down Expand Up @@ -42,21 +42,21 @@ You can see a runnable [example here](./example_test.go)
client := http.Client{
Transport: New(
WithName("example.com")
WithMeter(global.Meter("otel-round-tripper")),
WithMeter(global.MeterProvider()Meter("otel-round-tripper")),
WithAttributes(
semconv.ServiceNameKey.String("otel-round-tripper"),
),
),
}

resposne, err := client.Get("https://example.com")
response, err := client.Get("https://example.com")
```

## Metrics Emitted

The following metrics will be emitted by this package. Note that `*` will be replaced by the prefix passed in `WithName()`.

- `*.no_reqeusts` http calls with nil `http.Request`
- `*.no_request` http calls with nil `http.Request`
- `*.errors` http requests which had an error response i.e `err != nil`
- `*.success` http requests which were successfull. Meaning there were no transport errors
- `*.timeouts` http requests which timed out
Expand Down
2 changes: 2 additions & 0 deletions config.go
Expand Up @@ -5,6 +5,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
)

type config struct {
Expand All @@ -18,5 +19,6 @@ func defaultConfig() *config {
return &config{
name: "http.client",
parent: http.DefaultTransport,
meter: global.MeterProvider().Meter("http.client"),
}
}
2 changes: 1 addition & 1 deletion example_test.go
Expand Up @@ -52,7 +52,7 @@ func Example() {

client := http.Client{
Transport: New(
WithMeter(global.Meter("otel-round-tripper")),
WithMeter(global.MeterProvider().Meter("otel-round-tripper")),
WithAttributes(
semconv.ServiceNameKey.String("otel-round-tripper"),
),
Expand Down
18 changes: 9 additions & 9 deletions go.mod
Expand Up @@ -3,20 +3,20 @@ module github.com/NdoleStudio/go-otelroundtripper
go 1.17

require (
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/otel v1.2.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.25.0
go.opentelemetry.io/otel/metric v0.25.0
go.opentelemetry.io/otel/sdk/metric v0.25.0
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.6.1
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.28.0
go.opentelemetry.io/otel/metric v0.28.0
go.opentelemetry.io/otel/sdk/metric v0.28.0
)

require (
github.com/davecgh/go-spew v1.1.0 // 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/internal/metric v0.25.0 // indirect
go.opentelemetry.io/otel/sdk v1.2.0 // indirect
go.opentelemetry.io/otel/sdk/export/metric v0.25.0 // indirect
go.opentelemetry.io/otel/trace v1.2.0 // indirect
go.opentelemetry.io/otel/sdk v1.6.0 // indirect
go.opentelemetry.io/otel/trace v1.6.1 // indirect
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
47 changes: 25 additions & 22 deletions go.sum
@@ -1,30 +1,33 @@
github.com/benbjohnson/clock v1.2.0 h1:9Re3G2TWxkE06LdMWMpcY6KV81GLXMGiYpPYUPkFAws=
github.com/benbjohnson/clock v1.2.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
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.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
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/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.2.0 h1:YOQDvxO1FayUcT9MIhJhgMyNO1WqoduiyvQHzGN0kUQ=
go.opentelemetry.io/otel v1.2.0/go.mod h1:aT17Fk0Z1Nor9e0uisf98LrntPGMnk4frBO9+dkf69I=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.25.0 h1:XyBEWc22bxYllvyeG3bmW0G4esJ8Wi6P2m0e/tIuMsE=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.25.0/go.mod h1:Mn5lMLB4mIMKZ1IR4qCoYspC4lEbfK6pD7bI3SSAMKk=
go.opentelemetry.io/otel/internal/metric v0.25.0 h1:w/7RXe16WdPylaIXDgcYM6t/q0K5lXgSdZOEbIEyliE=
go.opentelemetry.io/otel/internal/metric v0.25.0/go.mod h1:Nhuw26QSX7d6n4duoqAFi5KOQR4AuzyMcl5eXOgwxtc=
go.opentelemetry.io/otel/metric v0.25.0 h1:7cXOnCADUsR3+EOqxPaSKwhEuNu0gz/56dRN1hpIdKw=
go.opentelemetry.io/otel/metric v0.25.0/go.mod h1:E884FSpQfnJOMMUaq+05IWlJ4rjZpk2s/F1Ju+TEEm8=
go.opentelemetry.io/otel/sdk v1.2.0 h1:wKN260u4DesJYhyjxDa7LRFkuhH7ncEVKU37LWcyNIo=
go.opentelemetry.io/otel/sdk v1.2.0/go.mod h1:jNN8QtpvbsKhgaC6V5lHiejMoKD+V8uadoSafgHPx1U=
go.opentelemetry.io/otel/sdk/export/metric v0.25.0 h1:6UjAFmVB5Fza3K5qUJpYWGrk8QMPIqlSnya5FI46VBY=
go.opentelemetry.io/otel/sdk/export/metric v0.25.0/go.mod h1:Ej7NOa+WpN49EIcr1HMUYRvxXXCCnQCg2+ovdt2z8Pk=
go.opentelemetry.io/otel/sdk/metric v0.25.0 h1:J+Ta+4IAA5W9AdWhGQLfciEpavBqqSkBzTDeYvJLFNU=
go.opentelemetry.io/otel/sdk/metric v0.25.0/go.mod h1:G4xzj4LvC6xDDSsVXpvRVclQCbofGGg4ZU2VKKtDRfg=
go.opentelemetry.io/otel/trace v1.2.0 h1:Ys3iqbqZhcf28hHzrm5WAquMkDHNZTUkw7KHbuNjej0=
go.opentelemetry.io/otel/trace v1.2.0/go.mod h1:N5FLswTubnxKxOJHM7XZC074qpeEdLy3CgAVsdMucK0=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.6.0/go.mod h1:bfJD2DZVw0LBxghOTlgnlI0CV3hLDu9XF/QKOUXMTQQ=
go.opentelemetry.io/otel v1.6.1 h1:6r1YrcTenBvYa1x491d0GGpTVBsNECmrc/K6b+zDeis=
go.opentelemetry.io/otel v1.6.1/go.mod h1:blzUabWHkX6LJewxvadmzafgh/wnvBSDBdOuwkAtrWQ=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.28.0 h1:ML6aEhlJAhSkPxVPS/3zkRcQOdYRiul8FDXcwL/KnpI=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.28.0/go.mod h1:8JL8qoBinVMmTsd57ekSmuRf4nDQPttgO6MUKzKaLII=
go.opentelemetry.io/otel/metric v0.28.0 h1:o5YNh+jxACMODoAo1bI7OES0RUW4jAMae0Vgs2etWAQ=
go.opentelemetry.io/otel/metric v0.28.0/go.mod h1:TrzsfQAmQaB1PDcdhBauLMk7nyyg9hm+GoQq/ekE9Iw=
go.opentelemetry.io/otel/sdk v1.6.0 h1:JoriAoiNENuxxIQApR1O0k2h1Md5QegZhbentcRJpWk=
go.opentelemetry.io/otel/sdk v1.6.0/go.mod h1:PjLRUfDsoPy0zl7yrDGSUqjj43tL7rEtFdCEiGlxXRM=
go.opentelemetry.io/otel/sdk/metric v0.28.0 h1:+1ndwHSiknwZtC8VmXM3xtMsd6kbFxtqti4qevn2J+o=
go.opentelemetry.io/otel/sdk/metric v0.28.0/go.mod h1:DqJmT0ovBgoW6TJ8CAQyTnwxZPIp3KWtCiDDZ1uHAzU=
go.opentelemetry.io/otel/trace v1.6.0/go.mod h1:qs7BrU5cZ8dXQHBGxHMOxwME/27YH2qEp4/+tZLLwJE=
go.opentelemetry.io/otel/trace v1.6.1 h1:f8c93l5tboBYZna1nWk0W9DYyMzJXDWdZcJZ0Kb400U=
go.opentelemetry.io/otel/trace v1.6.1/go.mod h1:RkFRM1m0puWIq10oxImnGEduNBzxiN7TXluRBtE+5j0=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
4 changes: 2 additions & 2 deletions option_test.go
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/nonrecording"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestWithMeter(t *testing.T) {

// Arrange
cfg := defaultConfig()
meter := metric.Meter{}
meter := nonrecording.NewNoopMeter()

// Act
WithMeter(meter).apply(cfg)
Expand Down
67 changes: 44 additions & 23 deletions round_tripper.go
Expand Up @@ -9,22 +9,22 @@ import (
"time"

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

type otelHTTPMetrics struct {
attemptsCounter metric.Int64Counter
noRequestCounter metric.Int64Counter
errorsCounter metric.Int64Counter
successesCounter metric.Int64Counter
failureCounter metric.Int64Counter
redirectCounter metric.Int64Counter
timeoutsCounter metric.Int64Counter
canceledCounter metric.Int64Counter
deadlineExceededCounter metric.Int64Counter
totalDurationCounter metric.Int64Histogram
inFlightCounter metric.Int64UpDownCounter
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
}

// otelRoundTripper is the http.RoundTripper which emits open telemetry metrics
Expand All @@ -46,21 +46,42 @@ func New(options ...Option) http.RoundTripper {
parent: cfg.parent,
attributes: cfg.attributes,
metrics: otelHTTPMetrics{
noRequestCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".no_request"),
errorsCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".errors"),
successesCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".success"),
timeoutsCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".timeouts"),
canceledCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".cancelled"),
deadlineExceededCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".deadline_exceeded"),
totalDurationCounter: metric.Must(cfg.meter).NewInt64Histogram(cfg.name + ".total_duration"),
inFlightCounter: metric.Must(cfg.meter).NewInt64UpDownCounter(cfg.name + ".in_flight"),
attemptsCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".attempts"),
failureCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".failures"),
redirectCounter: metric.Must(cfg.meter).NewInt64Counter(cfg.name + ".redirects"),
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")),
},
}
}

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

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

func mustHistogram(histogram syncint64.Histogram, err error) syncint64.Histogram {
if err != nil {
panic(err)
}
return histogram
}

// RoundTrip executes a single HTTP transaction, returning a Response for the provided Request.
func (roundTripper *otelRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
ctx := roundTripper.extractCtx(request)
Expand Down
4 changes: 2 additions & 2 deletions round_tripper_test.go
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/nonrecording"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)

Expand All @@ -29,7 +29,7 @@ func TestNew(t *testing.T) {
roundTripper := New(
WithName("name"),
WithParent(http.DefaultTransport),
WithMeter(metric.Meter{}),
WithMeter(nonrecording.NewNoopMeter()),
WithAttributes([]attribute.KeyValue{semconv.ServiceNameKey.String("service")}...),
)
assert.NotNil(t, roundTripper)
Expand Down

0 comments on commit 532cfb5

Please sign in to comment.