Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support net/http http metrics #131

Merged
merged 14 commits into from
Oct 15, 2024
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Also there are several [**documents**](./docs) that you may find useful
| gorm | https://github.com/go-gorm/gorm | v1.22.0 | v1.25.9 |
| grpc | https://google.golang.org/grpc | v1.44.0 | v1.67.0 |
| hertz | https://github.com/cloudwego/hertz | v0.8.0 | v0.9.2 |
| kratos | https://github.com/go-kratos/kratos | v2.6.3 | v2.8.0 |
| kratos | https://github.com/go-kratos/kratos | v2.6.3 | v2.8.1 |
| log | https://pkg.go.dev/log | - | - |
| logrus | https://github.com/sirupsen/logrus | v1.5.0 | v1.9.3 |
| mongodb | https://github.com/mongodb/mongo-go-driver | v1.11.1 | v1.15.2 |
Expand Down
2 changes: 1 addition & 1 deletion docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| gorm | https://github.com/go-gorm/gorm | v1.22.0 | v1.25.9 |
| grpc | https://google.golang.org/grpc | v1.44.0 | v1.67.0 |
| hertz | https://github.com/cloudwego/hertz | v0.8.0 | v0.9.2 |
| kratos | https://github.com/go-kratos/kratos | v2.6.3 | v2.8.0 |
| kratos | https://github.com/go-kratos/kratos | v2.6.3 | v2.8.1 |
| log | https://pkg.go.dev/log | - | - |
| logrus | https://github.com/sirupsen/logrus | v1.5.0 | v1.9.3 |
| mongodb | https://github.com/mongodb/mongo-go-driver | v1.11.1 | v1.15.2 |
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ go 1.22
require (
github.com/dave/dst v0.27.3
github.com/docker/go-connections v0.5.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/testcontainers/testcontainers-go v0.32.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.32.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/metric v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.opentelemetry.io/otel/sdk/metric v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
golang.org/x/mod v0.17.0
)
Expand Down Expand Up @@ -56,7 +58,7 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ github.com/moby/sys/user v0.1.0 h1:WmZ93f5Ux6het5iituh9x2zAG7NFY9Aqi49jjE1PaQg=
github.com/moby/sys/user v0.1.0/go.mod h1:fKJhFOnsCN6xZ5gSfbM6zaHGgDJMrqt9/reuj4T7MmU=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down Expand Up @@ -131,6 +133,8 @@ go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4Q
go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ=
go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE=
go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg=
go.opentelemetry.io/otel/sdk/metric v1.30.0 h1:QJLT8Pe11jyHBHfSAgYH7kEmT24eX792jZO1bo4BXkM=
go.opentelemetry.io/otel/sdk/metric v1.30.0/go.mod h1:waS6P3YqFNzeP01kuo/MBBYqaoBJl7efRQHOaydhy1Y=
go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc=
go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o=
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
Expand Down Expand Up @@ -165,8 +169,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
68 changes: 14 additions & 54 deletions pkg/core/meter/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,22 @@

package meter

import "go.opentelemetry.io/otel/metric"
import (
"go.opentelemetry.io/otel/metric"
"sync"
)

var globalMeterProvider MeterProvider
var globalMeter metric.Meter
var mu sync.Mutex

type MeterMeta map[string]string

type MeterWithMeta struct {
meter metric.Meter
meta MeterMeta
}

func NewMeterWithMeta(meter metric.Meter) *MeterWithMeta {
return &MeterWithMeta{meter: meter, meta: make(map[string]string)}
}

func NewMeterWithMetaWithKVs(meter metric.Meter, tag MeterMeta) *MeterWithMeta {
return &MeterWithMeta{meter: meter, meta: tag}
}

func (m *MeterWithMeta) Meter() metric.Meter {
return m.meter
}

func (m *MeterWithMeta) Metas() MeterMeta {
return m.meta
}

func (m *MeterWithMeta) Meta(key string) (string, bool) {
val, ok := m.meta[key]
return val, ok
}

func (m *MeterWithMeta) SetMeta(key string, val string) {
m.meta[key] = val
}

type MeterProvider interface {
GetMeters() []MeterWithMeta
}

type OtelMeterProvider struct {
metricsMeter MeterWithMeta
}

func NewOtelMeterProvider(metricsMeter MeterWithMeta) *OtelMeterProvider {
return &OtelMeterProvider{metricsMeter: metricsMeter}
}

func (o *OtelMeterProvider) GetMeters() []MeterWithMeta {
return []MeterWithMeta{o.metricsMeter}
}

func GetMeterProvider() MeterProvider {
return globalMeterProvider
func SetMeter(meter metric.Meter) {
mu.Lock()
defer mu.Unlock()
globalMeter = meter
}

func SetMeterProvider(meterProvider MeterProvider) {
globalMeterProvider = meterProvider
func GetMeter() metric.Meter {
mu.Lock()
defer mu.Unlock()
return globalMeter
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ type HttpClientAttrsExtractor[REQUEST any, RESPONSE any, GETTER1 HttpClientAttrs

func (h *HttpClientAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2]) OnStart(attributes []attribute.KeyValue, parentContext context.Context, request REQUEST) []attribute.KeyValue {
attributes = h.Base.OnStart(attributes, parentContext, request)
attributes = h.NetworkExtractor.OnStart(attributes, parentContext, request)
fullUrl := h.Base.HttpGetter.GetUrlFull(request)
// TODO: add resend count
attributes = append(attributes, attribute.KeyValue{
Key: semconv.URLFullKey,
Value: attribute.StringValue(fullUrl),
}, attribute.KeyValue{
Key: semconv.ServerAddressKey,
Value: attribute.StringValue(h.Base.HttpGetter.GetServerAddress(request)),
}, attribute.KeyValue{
Key: semconv.ServerPortKey,
Value: attribute.IntValue(h.Base.HttpGetter.GetServerPort(request)),
})
return attributes
}
Expand Down Expand Up @@ -102,6 +109,7 @@ func (h *HttpServerAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2, GETTER3])

func (h *HttpServerAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2, GETTER3]) OnEnd(attributes []attribute.KeyValue, context context.Context, request REQUEST, response RESPONSE, err error) []attribute.KeyValue {
attributes = h.Base.OnEnd(attributes, context, request, response, err)
attributes = h.UrlExtractor.OnEnd(attributes, context, request, response, err)
attributes = h.NetworkExtractor.OnEnd(attributes, context, request, response, err)
return attributes
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func (h httpClientAttrsGetter) GetServerAddress(request testRequest) string {
return "server-address"
}

func (h httpClientAttrsGetter) GetServerPort(request testRequest) int {
return 8080
}

func (h httpServerAttrsGetter) GetRequestMethod(request testRequest) string {
return "GET"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ type HttpClientAttrsGetter[REQUEST any, RESPONSE any] interface {
net.NetworkAttrsGetter[REQUEST, RESPONSE]
GetUrlFull(request REQUEST) string
GetServerAddress(request REQUEST) string
GetServerPort(request REQUEST) int
}
Loading
Loading