go.temporal.io/server/common/nexus/trace.go
180 LOC · 5 covered · 175 uncovered · 1 ranges · 64 concepts · 1 introducers · 12 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package nexus
import (
"crypto/tls"
"net/http/httptrace"
"time"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
)
type HTTPClientTraceProvider interface {
// NewTrace returns a *httptrace.ClientTrace which provides hooks to invoke at each point in the HTTP request
// lifecycle. This trace must be added to the HTTP request context using httptrace.WithClientTrace for the hooks to
// be invoked. The provided logger should already be tagged with relevant request information
// e.g. using log.With(logger, tag.RequestID(id), tag.Operation(op), ...).
NewTrace(attempt int32, logger log.Logger) *httptrace.ClientTrace
// NewForwardingTrace functions the same as NewTrace but forwarded requests do not have an associated attempt count,
// so all forwarded requests will be traced if enabled.
NewForwardingTrace(logger log.Logger) *httptrace.ClientTrace
}
// HTTPTraceConfig is the dynamic config for controlling Nexus HTTP request tracing behavior.
var HTTPTraceConfig = dynamicconfig.NewGlobalTypedSettingWithConverter(
"system.nexusHTTPTraceConfig",
convertHTTPClientTraceConfig,
defaultHTTPClientTraceConfig,
`Configuration options for controlling additional tracing for Nexus HTTP requests. Fields: Enabled, ForwardingEnabled, MinAttempt, MaxAttempt, Hooks. See HTTPClientTraceConfig comments for more detail.`,
)
type HTTPClientTraceConfig struct {
// Enabled controls whether any additional tracing will be invoked. Default false.
Enabled bool
// ForwardingEnabled controls whether any additional tracing will be invoked for forwarded requests. Default false. Forwarded requests do not have an attempt count, so MinAttempt and MaxAttempt are ignored for these requests.
ForwardingEnabled bool
// MinAttempt is the first operation attempt to include additional tracing. Default 2. Setting to 0 or 1 will add tracing to all requests and may be expensive.
MinAttempt int32
// MaxAttempt is the maximum operation attempt to include additional tracing. Default 2. Setting to 0 means no maximum.
MaxAttempt int32
// Hooks is the list of method names to invoke with extra tracing. See httptrace.ClientTrace for more detail.
// Defaults to all implemented hooks: GetConn, GotConn, ConnectStart, ConnectDone, DNSStart, DNSDone, TLSHandshakeStart, TLSHandshakeDone, WroteRequest, GotFirstResponseByte.
Hooks []string
}
var defaultHTTPClientTraceConfig = HTTPClientTraceConfig{
Enabled: false,
ForwardingEnabled: false,
MinAttempt: 2,
MaxAttempt: 2,
// use separate default for Hooks so that users can override with a smaller set of hooks
Hooks: nil,
}
var convertDefaultHTTPClientTraceConfig = dynamicconfig.ConvertStructure(defaultHTTPClientTraceConfig)
var defaultHTTPClientTraceHooks = []string{"GetConn", "GotConn", "ConnectStart", "ConnectDone", "DNSStart", "DNSDone", "TLSHandshakeStart", "TLSHandshakeDone", "WroteRequest", "GotFirstResponseByte"}
func convertHTTPClientTraceConfig(in any) (HTTPClientTraceConfig, error) {
cfg, err := convertDefaultHTTPClientTraceConfig(in)
if err != nil {
cfg = defaultHTTPClientTraceConfig
}
if len(cfg.Hooks) == 0 {
cfg.Hooks = defaultHTTPClientTraceHooks
}
return cfg, nil
}
type LoggedHTTPClientTraceProvider struct {
Config dynamicconfig.TypedPropertyFn[HTTPClientTraceConfig]
}
func NewLoggedHTTPClientTraceProvider(dc *dynamicconfig.Collection) HTTPClientTraceProvider {
fx.go ×44
return &LoggedHTTPClientTraceProvider{
Config: HTTPTraceConfig.Get(dc),
}
}
func (p *LoggedHTTPClientTraceProvider) NewTrace(attempt int32, logger log.Logger) *httptrace.ClientTrace {
config := p.Config()
if !config.Enabled {
return nil
}
if attempt < config.MinAttempt {
return nil
}
if config.MaxAttempt > 0 && attempt > config.MaxAttempt {
return nil
}
return p.newClientTrace(logger, config.Hooks)
}
func (p *LoggedHTTPClientTraceProvider) NewForwardingTrace(logger log.Logger) *httptrace.ClientTrace {
config := p.Config()
if !config.Enabled || !config.ForwardingEnabled {
return nil
}
return p.newClientTrace(logger, config.Hooks)
}
//nolint:revive // cognitive complexity (> 25 max) but is just adding a logging function for each method in the list.
func (p *LoggedHTTPClientTraceProvider) newClientTrace(logger log.Logger, hooks []string) *httptrace.ClientTrace {
clientTrace := &httptrace.ClientTrace{}
for _, h := range hooks {
switch h {
case "GetConn":
clientTrace.GetConn = func(hostPort string) {
logger.Info("attempting to get HTTP connection for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Address(hostPort))
}
case "GotConn":
clientTrace.GotConn = func(info httptrace.GotConnInfo) {
logger.Info("got HTTP connection for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Bool("reused", info.Reused),
tag.Bool("was-idle", info.WasIdle),
tag.Duration("idle-time", info.IdleTime))
}
case "ConnectStart":
clientTrace.ConnectStart = func(network, addr string) {
logger.Info("starting dial for new connection for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Address(addr),
tag.String("network", network))
}
case "ConnectDone":
clientTrace.ConnectDone = func(network, addr string, err error) {
logger.Info("finished dial for new connection for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Address(addr),
tag.String("network", network),
tag.Error(err))
}
case "DNSStart":
clientTrace.DNSStart = func(info httptrace.DNSStartInfo) {
logger.Info("starting DNS lookup for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Host(info.Host))
}
case "DNSDone":
clientTrace.DNSDone = func(info httptrace.DNSDoneInfo) {
addresses := make([]string, len(info.Addrs))
for i, a := range info.Addrs {
addresses[i] = a.String()
}
logger.Info("finished DNS lookup for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Addresses(addresses),
tag.Error(info.Err),
tag.Bool("coalesced", info.Coalesced))
}
case "TLSHandshakeStart":
clientTrace.TLSHandshakeStart = func() {
logger.Info("starting TLS handshake for Nexus request", tag.Timestamp(time.Now().UTC()))
}
case "TLSHandshakeDone":
clientTrace.TLSHandshakeDone = func(state tls.ConnectionState, err error) {
logger.Info("finished TLS handshake for Nexus request",
tag.Timestamp(time.Now().UTC()),
tag.Bool("handshake-complete", state.HandshakeComplete),
tag.Error(err))
}
case "WroteRequest":
clientTrace.WroteRequest = func(info httptrace.WroteRequestInfo) {
logger.Info("finished writing Nexus HTTP request",
tag.Timestamp(time.Now().UTC()),
tag.Error(info.Err))
}
case "GotFirstResponseByte":
clientTrace.GotFirstResponseByte = func() {
logger.Info("got response to Nexus HTTP request", tag.AttemptEnd(time.Now().UTC()))
}
}
}
return clientTrace
}