Skip to content

Commit

Permalink
fix full example
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwltr committed Jun 11, 2024
1 parent d51492b commit b0bc26b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 306 deletions.
47 changes: 19 additions & 28 deletions examples/full/cmd/inst.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,35 @@ import (
"net/http"
"strings"

"github.com/rebuy-de/rebuy-go-sdk/v8/pkg/instutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rebuy-de/rebuy-go-sdk/v8/pkg/logutil"
)

// inst.go contains all functions for handling instrumentation (ie metrics and
// logs). All functions do not have an attached type and only rely on the
// context, so we do not have to pass another variable through the code. Having
// the instrumentation code in its own file gives a better separation between
// instrumentation code and actual buisniess logic. Each package can have its
// own ist.go file.

// We store the metric names in a constant, because this is our only reference
// in the code and using literal strings might be prone to errors.
const (
instRequestsAcceptEncodingMetric = "request_accept_encoding_total"
instRequestsMetric = "request_total"
// logs). Having the instrumentation code in its own file gives a better
// separation between instrumentation code and actual buisniess logic. Each
// package can have its own inst.go file.

var (
instRequestsAcceptEncodingMetric = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "rebuy_go_sdk",
Subsystem: "requests",
Name: "encoding_total",
}, []string{"encoding"})
instRequestsMetric = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "rebuy_go_sdk",
Subsystem: "requests",
Name: "total",
})
)

func InstInit(ctx context.Context) context.Context {
ctx = instutil.NewCounterVec(ctx, instRequestsAcceptEncodingMetric, "encoding")
ctx = instutil.NewCounter(ctx, instRequestsMetric)
return ctx
}

func InstIndexRequest(ctx context.Context, r *http.Request) {
logutil.Get(ctx).
WithFields(logutil.FromStruct(r.Header)).
Infof("got request")

c, ok := instutil.Counter(ctx, instRequestsMetric)
if ok {
// We need to check whether the metric was initialized to avoid a nil
// dereference.
c.Inc()
}
instRequestsMetric.Inc()

// We have a metric that lists all accepted encodings. Together with the
// total request count we get a ratio of each encoding. This is a good
Expand All @@ -52,10 +46,7 @@ func InstIndexRequest(ctx context.Context, r *http.Request) {
continue
}

cv, ok := instutil.CounterVec(ctx, instRequestsAcceptEncodingMetric)
if ok {
cv.WithLabelValues(accept).Inc()
}
instRequestsAcceptEncodingMetric.WithLabelValues(accept).Inc()
}
}
}
64 changes: 2 additions & 62 deletions examples/full/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ replace github.com/rebuy-de/rebuy-go-sdk/v8 => ../..
require (
github.com/go-chi/chi/v5 v5.0.12
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/rebuy-de/rebuy-go-sdk/v8 v8.0.0
github.com/redis/go-redis/v9 v9.5.1
github.com/sirupsen/logrus v1.9.3
Expand All @@ -16,96 +17,35 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/AlekSi/pointer v1.2.0 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/aws/aws-sdk-go-v2 v1.25.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.0 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.50.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.19.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.22.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.27.0 // indirect
github.com/aws/smithy-go v1.20.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb // indirect
github.com/cavaliergopher/cpio v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/coreos/go-oidc/v3 v3.9.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/gemnasium/logrus-graylog-hook/v3 v3.2.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.11.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/rpmpack v0.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/goreleaser/chglog v0.5.0 // indirect
github.com/goreleaser/fileglob v1.3.0 // indirect
github.com/goreleaser/nfpm/v2 v2.35.3 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.2.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.5 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
gitlab.com/digitalxero/go-conventional-commit v1.0.7 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/exp/typeparams v0.0.0-20221208152030-732eee02a75a // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/tools v0.18.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b0bc26b

Please sign in to comment.