-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forwarder): add tracing to HTTP client (#243)
This commit introduces tracing support for the legacy HTTP client embedded in the forwarder. The HTTP client has be refactored out of the internal package in order to facilitate reuse across components if necessary.
- Loading branch information
Showing
43 changed files
with
9,299 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package tracing | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/hashicorp/go-retryablehttp" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
// leveledLogger provides an adapter between logr.Logger and retryablehttp.LeveledLogger. | ||
type leveledLogger struct { | ||
logr.Logger | ||
} | ||
|
||
func (l *leveledLogger) Error(msg string, keysAndValues ...interface{}) { | ||
l.V(1).Info(msg, keysAndValues...) | ||
} | ||
|
||
func (l *leveledLogger) Warn(msg string, keysAndValues ...interface{}) { | ||
l.V(2).Info(msg, keysAndValues...) | ||
} | ||
|
||
func (l *leveledLogger) Info(msg string, keysAndValues ...interface{}) { | ||
l.V(3).Info(msg, keysAndValues...) | ||
} | ||
|
||
func (l *leveledLogger) Debug(msg string, keysAndValues ...interface{}) { | ||
l.V(4).Info(msg, keysAndValues...) | ||
} | ||
|
||
type HTTPClientConfig struct { | ||
RetryWaitMin *time.Duration // Minimum time to wait on retry | ||
RetryWaitMax *time.Duration // Maximumum time to wait on retry | ||
RetryMax *int // Maximum number of retries | ||
HTTPClient *http.Client | ||
Logger *logr.Logger | ||
TracerProvider *trace.TracerProvider | ||
} | ||
|
||
func NewHTTPClient(cfg *HTTPClientConfig) *http.Client { | ||
if cfg == nil { | ||
cfg = &HTTPClientConfig{} | ||
} | ||
|
||
client := retryablehttp.NewClient() | ||
|
||
if cfg.HTTPClient != nil { | ||
client.HTTPClient = cfg.HTTPClient | ||
} | ||
|
||
if cfg.RetryWaitMin != nil { | ||
client.RetryWaitMin = *cfg.RetryWaitMin | ||
} | ||
|
||
if cfg.RetryWaitMax != nil { | ||
client.RetryWaitMax = *cfg.RetryWaitMax | ||
} | ||
|
||
if cfg.RetryMax != nil { | ||
client.RetryMax = *cfg.RetryMax | ||
} | ||
|
||
logger := logr.Discard() | ||
if cfg.Logger != nil { | ||
logger = *cfg.Logger | ||
} | ||
|
||
client.Logger = &leveledLogger{logger} | ||
|
||
transport := &retryablehttp.RoundTripper{Client: client} | ||
return &http.Client{ | ||
Transport: otelhttp.NewTransport(transport, otelhttp.WithTracerProvider(cfg.TracerProvider)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.