1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-21 15:17:01 +00:00

nettrace: Add a new tracing library

This commit introduces a new tracing library, that replaces
golang.org/x/net/trace, and supports (amongts other thing) nested
traces.

This is a minimal change, future patches will make use of the new
functionality.
This commit is contained in:
Alberto Bertogli
2022-03-07 01:13:34 +00:00
parent 3ebe5c5173
commit 9c6661eca2
18 changed files with 2276 additions and 54 deletions

View File

@@ -1,23 +1,15 @@
// Package trace extends golang.org/x/net/trace.
// Package trace extends nettrace with logging.
package trace
import (
"fmt"
"net/http"
"strconv"
"blitiri.com.ar/go/chasquid/internal/nettrace"
"blitiri.com.ar/go/log"
nettrace "golang.org/x/net/trace"
)
func init() {
// golang.org/x/net/trace has its own authorization which by default only
// allows localhost. This can be confusing and limiting in environments
// which access the monitoring server remotely.
nettrace.AuthRequest = func(req *http.Request) (any, sensitive bool) {
return true, true
}
}
// A Trace represents an active request.
@@ -38,9 +30,16 @@ func New(family, title string) *Trace {
return t
}
// NewChild creates a new child trace.
func (t *Trace) NewChild(family, title string) *Trace {
n := &Trace{family, title, t.t.NewChild(family, title)}
n.t.SetMaxEvents(30)
return n
}
// Printf adds this message to the trace's log.
func (t *Trace) Printf(format string, a ...interface{}) {
t.t.LazyPrintf(format, a...)
t.t.Printf(format, a...)
log.Log(log.Info, 1, "%s %s: %s", t.family, t.title,
quote(fmt.Sprintf(format, a...)))
@@ -48,7 +47,7 @@ func (t *Trace) Printf(format string, a ...interface{}) {
// Debugf adds this message to the trace's log, with a debugging level.
func (t *Trace) Debugf(format string, a ...interface{}) {
t.t.LazyPrintf(format, a...)
t.t.Printf(format, a...)
log.Log(log.Debug, 1, "%s %s: %s",
t.family, t.title, quote(fmt.Sprintf(format, a...)))
@@ -59,7 +58,7 @@ func (t *Trace) Errorf(format string, a ...interface{}) error {
// Note we can't just call t.Error here, as it breaks caller logging.
err := fmt.Errorf(format, a...)
t.t.SetError()
t.t.LazyPrintf("error: %v", err)
t.t.Printf("error: %v", err)
log.Log(log.Info, 1, "%s %s: error: %s", t.family, t.title,
quote(err.Error()))
@@ -70,7 +69,7 @@ func (t *Trace) Errorf(format string, a ...interface{}) error {
// trace's log.
func (t *Trace) Error(err error) error {
t.t.SetError()
t.t.LazyPrintf("error: %v", err)
t.t.Printf("error: %v", err)
log.Log(log.Info, 1, "%s %s: error: %s", t.family, t.title,
quote(err.Error()))
@@ -83,45 +82,6 @@ func (t *Trace) Finish() {
t.t.Finish()
}
// EventLog is used for tracing long-lived objects.
type EventLog struct {
family string
title string
e nettrace.EventLog
}
// NewEventLog returns a new EventLog.
func NewEventLog(family, title string) *EventLog {
return &EventLog{family, title, nettrace.NewEventLog(family, title)}
}
// Printf adds the message to the EventLog.
func (e *EventLog) Printf(format string, a ...interface{}) {
e.e.Printf(format, a...)
log.Log(log.Info, 1, "%s %s: %s", e.family, e.title,
quote(fmt.Sprintf(format, a...)))
}
// Debugf adds the message to the EventLog, with a debugging level.
func (e *EventLog) Debugf(format string, a ...interface{}) {
e.e.Printf(format, a...)
log.Log(log.Debug, 1, "%s %s: %s", e.family, e.title,
quote(fmt.Sprintf(format, a...)))
}
// Errorf adds the message to the EventLog, with an error level.
func (e *EventLog) Errorf(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
e.e.Errorf("error: %v", err)
log.Log(log.Info, 1, "%s %s: error: %s",
e.family, e.title, quote(err.Error()))
return err
}
func quote(s string) string {
qs := strconv.Quote(s)
return qs[1 : len(qs)-1]