SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat(httpclient): add an extensible http client (#7446)
Vibhu Pandey027a1631efeb
diff --git a/go.mod b/go.modindex 5f08efba2e..8bf379d5aa 100644--- a/go.mod+++ b/go.mod@@ -22,6 +22,7 @@ require (github.com/go-redis/redis/v8 v8.11.5github.com/go-redis/redismock/v8 v8.11.5github.com/go-viper/mapstructure/v2 v2.1.0+ github.com/gojek/heimdall/v7 v7.0.3github.com/golang-jwt/jwt/v5 v5.2.1github.com/google/uuid v1.6.0github.com/gorilla/handlers v1.5.1@@ -62,6 +63,7 @@ require (go.opentelemetry.io/collector/pdata v1.17.0go.opentelemetry.io/collector/processor v0.111.0go.opentelemetry.io/contrib/config v0.10.0+ go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0go.opentelemetry.io/otel v1.34.0go.opentelemetry.io/otel/metric v1.34.0go.opentelemetry.io/otel/sdk v1.34.0@@ -129,6 +131,7 @@ require (github.com/goccy/go-json v0.10.3 // indirectgithub.com/gofrs/uuid v4.4.0+incompatible // indirectgithub.com/gogo/protobuf v1.3.2 // indirect+ github.com/gojek/valkyrie v0.0.0-20180215180059-6aee720afcdf // indirectgithub.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirectgithub.com/golang/protobuf v1.5.4 // indirectgithub.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect@@ -202,6 +205,7 @@ require (github.com/smarty/assertions v1.15.0 // indirectgithub.com/spf13/cobra v1.8.1 // indirectgithub.com/spf13/pflag v1.0.5 // indirect+ github.com/stretchr/objx v0.5.2 // indirectgithub.com/tidwall/match v1.1.1 // indirectgithub.com/tidwall/pretty v1.2.0 // indirectgithub.com/tklauser/go-sysconf v0.3.13 // indirect@@ -246,7 +250,6 @@ require (go.opentelemetry.io/collector/receiver/receiverprofiles v0.111.0 // indirectgo.opentelemetry.io/collector/semconv v0.111.0 // indirectgo.opentelemetry.io/collector/service v0.111.0 // indirect- go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirectgo.opentelemetry.io/contrib/propagators/b3 v1.30.0 // indirectgo.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.6.0 // indirectgo.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.30.0 // indirectdiff --git a/go.sum b/go.sumindex e98ba06a03..cde09d69d4 100644--- a/go.sum+++ b/go.sum@@ -90,6 +90,7 @@ github.com/Code-Hex/go-generics-cache v1.5.1/go.mod h1:qxcC9kRVrct9rHeiYpFWSoW1vgithub.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=+github.com/DataDog/datadog-go v3.7.1+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=@@ -99,6 +100,7 @@ github.com/SigNoz/prometheus v1.13.0 h1:hsUql1zd83ifXtswO9Qk1rpCgVjE/ItQvgdNocBSgithub.com/SigNoz/prometheus v1.13.0/go.mod h1:4PC0dxmx6y3kNI2d9oOTvEFTPkH6QnxDxERyqeL1hvI=github.com/SigNoz/signoz-otel-collector v0.111.16 h1:535uKH5Oux+35EsI+L3C6pnAP/Ye0PTCbVizXoL+VqE=github.com/SigNoz/signoz-otel-collector v0.111.16/go.mod h1:HJ4m0LY1MPsuZmuRF7Ixb+bY8rxgRzI0VXzOedESsjg=+github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=@@ -144,6 +146,7 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rgithub.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=+github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c/go.mod h1:l/bIBLeOl9eX+wxJAzxS4TveKRtAqlyDpHjhkfO0MEI=github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=@@ -328,6 +331,10 @@ github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxgithub.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=+github.com/gojek/heimdall/v7 v7.0.3 h1:+5sAhl8S0m+qRRL8IVeHCJudFh/XkG3wyO++nvOg+gc=+github.com/gojek/heimdall/v7 v7.0.3/go.mod h1:Z43HtMid7ysSjmsedPTXAki6jcdcNVnjn5pmsTyiMic=+github.com/gojek/valkyrie v0.0.0-20180215180059-6aee720afcdf h1:5xRGbUdOmZKoDXkGx5evVLehuCMpuO1hl701bEQqXOM=+github.com/gojek/valkyrie v0.0.0-20180215180059-6aee720afcdf/go.mod h1:QzhUKaYKJmcbTnCYCAVQrroCOY7vOOI8cSQ4NbuhYf0=github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=@@ -433,6 +440,7 @@ github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxKgithub.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=github.com/gophercloud/gophercloud v1.14.0 h1:Bt9zQDhPrbd4qX7EILGmy+i7GP35cc+AAL2+wIJpUE8=github.com/gophercloud/gophercloud v1.14.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=+github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=@@ -802,6 +810,7 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargggithub.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=github.com/puzpuzpuz/xsync/v3 v3.5.0 h1:i+cMcpEDY1BkNm7lPDkCtE4oElsYLn+EKF8kAu2vXT4=github.com/puzpuzpuz/xsync/v3 v3.5.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=+github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=github.com/rhnvrm/simples3 v0.6.1/go.mod h1:Y+3vYm2V7Y4VijFoJHHTrja6OgPrJ2cBti8dPGkC3sA=github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=@@ -854,6 +863,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQgithub.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=+github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=+github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=@@ -871,6 +882,7 @@ github.com/srikanthccv/ClickHouse-go-mock v0.11.0 h1:hKY9l7SbhI4IPPs7hjKAL1iDgKcgithub.com/srikanthccv/ClickHouse-go-mock v0.11.0/go.mod h1:CzFC21J4tLn7cEYdU5k6hg7yyf052xtZXUY2e3UF6+I=github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=+github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=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/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=@@ -1359,6 +1371,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=+golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=diff --git a/pkg/http/client/http.go b/pkg/http/client/http.gonew file mode 100644index 0000000000..fa30d9c47d--- /dev/null+++ b/pkg/http/client/http.go@@ -0,0 +1,58 @@+package client++import (+ "log/slog"+ "net/http"+ "time"++ "github.com/SigNoz/signoz/pkg/http/client/plugin"+ "github.com/gojek/heimdall/v7/httpclient"+ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"+ "go.opentelemetry.io/otel/metric"+ "go.opentelemetry.io/otel/trace"+)++type Client struct {+ c *httpclient.Client+ netc *http.Client+}++func New(logger *slog.Logger, tracerProvider trace.TracerProvider, meterProvider metric.MeterProvider, opts ...Option) (*Client, error) {+ clientOpts := options{+ retryCount: 3,+ requestResponseLog: false,+ timeout: 5 * time.Second,+ }++ for _, opt := range opts {+ opt(&clientOpts)+ }++ netc := &http.Client{+ Timeout: clientOpts.timeout,+ Transport: otelhttp.NewTransport(http.DefaultTransport, otelhttp.WithTracerProvider(tracerProvider), otelhttp.WithMeterProvider(meterProvider)),+ }++ c := httpclient.NewClient(+ httpclient.WithHTTPClient(netc),+ httpclient.WithRetrier(clientOpts.retriable),+ httpclient.WithRetryCount(clientOpts.retryCount),+ )++ if clientOpts.requestResponseLog {+ c.AddPlugin(plugin.NewLog(logger))+ }++ return &Client{+ netc: netc,+ c: c,+ }, nil+}++func (c *Client) Do(request *http.Request) (*http.Response, error) {+ return c.c.Do(request)+}++func (c *Client) Client() *http.Client {+ return c.netc+}diff --git a/pkg/http/client/option.go b/pkg/http/client/option.gonew file mode 100644index 0000000000..7ae8b56292--- /dev/null+++ b/pkg/http/client/option.go@@ -0,0 +1,42 @@+package client++import (+ "time"++ "github.com/gojek/heimdall/v7"+)++type Retriable = heimdall.Retriable++type options struct {+ retryCount int+ requestResponseLog bool+ timeout time.Duration+ retriable Retriable+}++type Option func(*options)++func WithRetryCount(i int) Option {+ return func(o *options) {+ o.retryCount = i+ }+}++func WithTimeout(i time.Duration) Option {+ return func(o *options) {+ o.timeout = i+ }+}++func WithRequestResponseLog(b bool) Option {+ return func(o *options) {+ o.requestResponseLog = b+ }+}++func WithRetriable(retriable Retriable) Option {+ return func(o *options) {+ o.retriable = retriable+ }+}diff --git a/pkg/http/client/plugin/log.go b/pkg/http/client/plugin/log.gonew file mode 100644index 0000000000..c58de171e8--- /dev/null+++ b/pkg/http/client/plugin/log.go@@ -0,0 +1,77 @@+package plugin++import (+ "bytes"+ "io"+ "log/slog"+ "net"+ "net/http"++ "github.com/gojek/heimdall/v7"+ semconv "go.opentelemetry.io/otel/semconv/v1.27.0"+)++type reqResLog struct {+ logger *slog.Logger+}++func NewLog(logger *slog.Logger) heimdall.Plugin {+ return &reqResLog{+ logger: logger,+ }+}++func (plugin *reqResLog) OnRequestStart(request *http.Request) {+ host, port, _ := net.SplitHostPort(request.Host)+ fields := []any{+ string(semconv.HTTPRequestMethodKey), request.Method,+ string(semconv.URLPathKey), request.URL.Path,+ string(semconv.URLSchemeKey), request.URL.Scheme,+ string(semconv.UserAgentOriginalKey), request.UserAgent(),+ string(semconv.ServerAddressKey), host,+ string(semconv.ServerPortKey), port,+ string(semconv.HTTPRequestSizeKey), request.ContentLength,+ "http.request.headers", request.Header,+ }++ plugin.logger.InfoContext(request.Context(), "::SENT-REQUEST::", fields...)+}++func (plugin *reqResLog) OnRequestEnd(request *http.Request, response *http.Response) {+ fields := []any{+ string(semconv.HTTPResponseStatusCodeKey), response.StatusCode,+ string(semconv.HTTPResponseBodySizeKey), response.ContentLength,+ }++ bodybytes, err := io.ReadAll(response.Body)+ if err != nil {+ plugin.logger.DebugContext(request.Context(), "::UNABLE-TO-LOG-RESPONSE-BODY::", "error", err)+ } else {+ _ = response.Body.Close()+ response.Body = io.NopCloser(bytes.NewBuffer(bodybytes))++ if len(bodybytes) > 0 {+ fields = append(fields, "http.response.body", string(bodybytes))+ } else {+ fields = append(fields, "http.response.body", "(empty)")+ }+ }++ plugin.logger.InfoContext(request.Context(), "::RECEIVED-RESPONSE::", fields...)+}++func (plugin *reqResLog) OnError(request *http.Request, err error) {+ host, port, _ := net.SplitHostPort(request.Host)+ fields := []any{+ err,+ string(semconv.HTTPRequestMethodKey), request.Method,+ string(semconv.URLPathKey), request.URL.Path,+ string(semconv.URLSchemeKey), request.URL.Scheme,+ string(semconv.UserAgentOriginalKey), request.UserAgent(),+ string(semconv.ServerAddressKey), host,+ string(semconv.ServerPortKey), port,+ string(semconv.HTTPRequestSizeKey), request.ContentLength,+ }++ plugin.logger.ErrorContext(request.Context(), "::UNABLE-TO-SEND-REQUEST::", fields...)+}