Skip to content

Commit

Permalink
log/martianlog: add LoggingDialContext()
Browse files Browse the repository at this point in the history
This is a temporary solution until we have context-aware logging everywhere.
It allows us to log the network and address of the connection being established together with the trace ID.
  • Loading branch information
mmatczuk committed Feb 28, 2024
1 parent 83923ef commit 6ef0817
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions log/martianlog/dial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

package martianlog

import (
"context"
"net"
"time"

martianlog "github.com/saucelabs/forwarder/internal/martian/log"
)

// LoggingDialContext wraps a dial function adding logging.
// This is a temporary solution until we have context-aware logging everywhere.
// It allows us to log the network and address of the connection being established together with the trace ID.
func LoggingDialContext(dial func(context.Context, string, string) (net.Conn, error)) func(context.Context, string, string) (net.Conn, error) {
return func(ctx context.Context, network, address string) (conn net.Conn, err error) {
martianlog.Debugf(ctx, "opening connection to %s %s", network, address)

start := time.Now()
conn, err = dial(ctx, network, address)
if err != nil {
martianlog.Debugf(ctx, "failed to establish connection to %s %s duration=%s", network, address, time.Since(start))
} else {
martianlog.Debugf(ctx, "connection to %s %s established duration=%s", network, address, time.Since(start))
}

return
}
}

0 comments on commit 6ef0817

Please sign in to comment.