Skip to content

Commit

Permalink
refactor: make metric declare easy
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jun 20, 2023
1 parent a3b41bb commit 5511bee
Show file tree
Hide file tree
Showing 40 changed files with 1,761 additions and 130 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ gen:
go run ./internal/cmd/tool gen ./cmd/example

gen.debug:
go run ./internal/cmd/tool gen --log-level=debug ./cmd/example
go run ./internal/cmd/tool gen --log-level=debug ./cmd/example

hey:
hey -z 5m http://localhost:8081/api/example/v0/orgs
3 changes: 2 additions & 1 deletion cmd/example/apis/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"bytes"
"context"
"fmt"
"github.com/octohelm/courier/pkg/courierhttp"
"io"
"net/http"

"github.com/octohelm/courier/pkg/courierhttp"
)

type ArchiveZip struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/example/apis/archive/zz_generated.operator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package archive GENERATED BY gengo:operator
Package archive GENERATED BY gengo:operator
DON'T EDIT THIS FILE
*/
package archive
Expand Down
2 changes: 1 addition & 1 deletion cmd/example/apis/archive/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package archive GENERATED BY gengo:runtimedoc
Package archive GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package archive
Expand Down
2 changes: 1 addition & 1 deletion cmd/example/apis/org/zz_generated.enum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package org GENERATED BY gengo:enum
Package org GENERATED BY gengo:enum
DON'T EDIT THIS FILE
*/
package org
Expand Down
2 changes: 1 addition & 1 deletion cmd/example/apis/org/zz_generated.operator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package org GENERATED BY gengo:operator
Package org GENERATED BY gengo:operator
DON'T EDIT THIS FILE
*/
package org
Expand Down
2 changes: 1 addition & 1 deletion cmd/example/apis/org/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package org GENERATED BY gengo:runtimedoc
Package org GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package org
Expand Down
2 changes: 1 addition & 1 deletion cmd/example/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package main GENERATED BY gengo:runtimedoc
Package main GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package main
Expand Down
26 changes: 25 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/octohelm/x v0.0.0-20220811034253-019992077a5d
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/prometheus v0.44.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
go.opentelemetry.io/contrib/propagators/b3 v1.17.0
Expand All @@ -32,29 +33,52 @@ require (
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aws/aws-sdk-go v1.44.245 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/gomega v1.27.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
233 changes: 231 additions & 2 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions internal/otel/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package otel

import (
"context"

"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
Expand Down
13 changes: 13 additions & 0 deletions internal/otel/span_export__slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package otel
import (
"context"
"os"
"runtime"
_ "time/tzdata"

"github.com/go-courier/logr"
Expand All @@ -11,6 +12,18 @@ import (
"golang.org/x/exp/slog"
)

func Source(skip int) *slog.Source {
pc, _, _, _ := runtime.Caller(skip)
fs := runtime.CallersFrames([]uintptr{pc})
f, _ := fs.Next()

return &slog.Source{
Function: f.Function,
File: f.File,
Line: f.Line,
}
}

func NewLogger() *slog.Logger {
if os.Getenv("GOENV") == "DEV" {
return slog.New(slog.NewTextHandler(os.Stderr, nil))
Expand Down
1 change: 1 addition & 0 deletions internal/otel/span_exporter__err_ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package otel

import (
"context"

sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/otel/zz_generated.enum.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package otel GENERATED BY gengo:enum
Package otel GENERATED BY gengo:enum
DON'T EDIT THIS FILE
*/
package otel
Expand Down
2 changes: 1 addition & 1 deletion internal/otel/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package otel GENERATED BY gengo:runtimedoc
Package otel GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package otel
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package cli GENERATED BY gengo:runtimedoc
Package cli GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package cli
Expand Down
2 changes: 1 addition & 1 deletion pkg/configuration/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package configuration GENERATED BY gengo:runtimedoc
Package configuration GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package configuration
Expand Down
35 changes: 35 additions & 0 deletions pkg/http/middleware/metrichttp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package metrichttp

import (
"github.com/innoai-tech/infra/pkg/otel/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
)

var (
ClientDuration = metric.NewFloat64Histogram(
"http.client.duration",
metric.WithUnit("s"),
metric.WithDescription("Measures the duration of outbound HTTP requests"),
metric.WithAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: DurationHistogramBoundaries,
}),
)

ClientRequestSize = metric.NewInt64Histogram(
"http.client.request.size",
metric.WithUnit("By"),
metric.WithDescription("Measures the size of HTTP response messages"),
metric.WithAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: SizeHistogramBoundaries,
}),
)

ClientResponseSize = metric.NewInt64Histogram(
"http.client.response.size",
metric.WithUnit("By"),
metric.WithDescription("Measures the size of HTTP response messages"),
metric.WithAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: SizeHistogramBoundaries,
}),
)
)
41 changes: 41 additions & 0 deletions pkg/http/middleware/metrichttp/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package metrichttp

import (
"github.com/innoai-tech/infra/pkg/otel/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
)

var (
ServerDuration = metric.NewFloat64Histogram(
"http.server.duration",
metric.WithUnit("s"),
metric.WithDescription("Measures the duration of inbound HTTP requests"),
metric.WithAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: DurationHistogramBoundaries,
}),
)

ServerActiveRequest = metric.NewInt64UpDownCounter(
"http.server.active_requests",
metric.WithDescription("Measures the number of concurrent HTTP requests that are currently in-flight"),
)

ServerRequestSize = metric.NewInt64Histogram(
"http.server.request.size",
metric.WithUnit("By"),
metric.WithDescription("Measures the size of HTTP response messages"),
metric.WithAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: SizeHistogramBoundaries,
}),
)

ServerResponseSize = metric.NewInt64Histogram(
"http.server.response.size",
metric.WithUnit("By"),
metric.WithDescription("Measures the size of HTTP response messages"),
metric.WithAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: SizeHistogramBoundaries,
}),
//metric.WithAggregationFunc("increase", 1*time.Minute),
)
)
38 changes: 38 additions & 0 deletions pkg/http/middleware/metrichttp/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package metrichttp

var DurationHistogramBoundaries = []float64{
0,
0.005,
0.01,
0.025,
0.05,
0.075,
0.1,
0.25,
0.5,
0.75,
1,
2.5,
5,
7.5,
10,
}

const B = 1
const KiB = 1024 * B
const MiB = 1024 * KiB

var SizeHistogramBoundaries = []float64{
512 * B,
1 * KiB,
64 * KiB,
128 * KiB,
256 * KiB,
512 * KiB,
1 * MiB,
2 * MiB,
5 * MiB,
10 * MiB,
20 * MiB,
50 * MiB,
}
37 changes: 22 additions & 15 deletions pkg/http/middleware/otel_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ package middleware

import (
"fmt"
"github.com/innoai-tech/infra/internal/otel"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"net/http"
"strconv"
"time"

"github.com/innoai-tech/infra/pkg/http/middleware/metrichttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"

"github.com/go-courier/logr"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel/propagation"
)

func NewLogRoundTripper(clientName string) func(roundTripper http.RoundTripper) http.RoundTripper {
func NewLogRoundTripper() func(roundTripper http.RoundTripper) http.RoundTripper {
return func(roundTripper http.RoundTripper) http.RoundTripper {
return &LogRoundTripper{
clientName: clientName,
nextRoundTripper: roundTripper,
}
}
}

type LogRoundTripper struct {
clientName string
nextRoundTripper http.RoundTripper
}

Expand All @@ -33,13 +34,6 @@ func (rt *LogRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

ctx := req.Context()

mp := otel.MeterProviderFromContext(ctx)

h, _ := mp.Meter(rt.clientName).Float64Histogram(
"http.client.duration",
metric.WithUnit("s"),
)

// inject b3 form context
b3.New().Inject(ctx, propagation.HeaderCarrier(req.Header))

Expand All @@ -49,18 +43,31 @@ func (rt *LogRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
resp, err := rt.nextRoundTripper.RoundTrip(req.WithContext(ctx))

cost := time.Since(startedAt)

l := log.WithValues(
semconv.HTTPMethod(req.Method),
semconv.HTTPURL(omitAuthorization(req.URL)),
"http.client.duration", fmt.Sprintf("%0.3fms", float64(cost/time.Millisecond)),
attribute.Key("http.client.duration").String(fmt.Sprintf("%s", cost)),
)

h.Record(ctx, float64(cost)/float64(time.Second), metric.WithAttributes(
metrichttp.ClientDuration.Record(ctx, cost.Seconds(), metric.WithAttributes(
semconv.HTTPMethod(req.Method),
semconv.HTTPURL(omitAuthorization(req.URL)),
))

if resp != nil {
p, _ := strconv.ParseInt(req.URL.Port(), 10, 64)

attrs := []attribute.KeyValue{
attribute.Key("http.request.method").String(req.Method),
attribute.Key("http.response.status_code").Int(resp.StatusCode),
attribute.Key("server.address").String(req.URL.Hostname()),
attribute.Key("server.port").Int(int(p)),
}

metrichttp.ClientRequestSize.Record(ctx, req.ContentLength, metric.WithAttributes(attrs...))
metrichttp.ClientResponseSize.Record(ctx, resp.ContentLength, metric.WithAttributes(attrs...))

l = l.WithValues(
semconv.HTTPStatusCode(resp.StatusCode),
)
Expand Down
Loading

0 comments on commit 5511bee

Please sign in to comment.