Skip to content

Commit

Permalink
remove xopbase.Line.Error, add ErrorReactionFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Sep 25, 2022
1 parent 74e87c5 commit a9deaf7
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 287 deletions.
36 changes: 12 additions & 24 deletions basegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,12 @@ func (p prefillings) Duration(k string, v time.Duration) {
}
}

func (p prefillings) Error(k string, v error) {
for _, prefilling := range p {
prefilling.Error(k, v)
}
}

func (p prefillings) Link(k string, v trace.Trace) {
for _, prefilling := range p {
prefilling.Link(k, v)
}
}

func (p prefillings) String(k string, v string) {
for _, prefilling := range p {
prefilling.String(k, v)
}
}

func (p prefillings) Time(k string, v time.Time) {
for _, prefilling := range p {
prefilling.Time(k, v)
Expand All @@ -243,6 +231,12 @@ func (p prefillings) Int64(k string, v int64, dt xopbase.DataType) {
}
}

func (p prefillings) String(k string, v string, dt xopbase.DataType) {
for _, prefilling := range p {
prefilling.String(k, v, dt)
}
}

func (p prefillings) Uint64(k string, v uint64, dt xopbase.DataType) {
for _, prefilling := range p {
prefilling.Uint64(k, v, dt)
Expand All @@ -267,24 +261,12 @@ func (l lines) Duration(k string, v time.Duration) {
}
}

func (l lines) Error(k string, v error) {
for _, line := range l {
line.Error(k, v)
}
}

func (l lines) Link(k string, v trace.Trace) {
for _, line := range l {
line.Link(k, v)
}
}

func (l lines) String(k string, v string) {
for _, line := range l {
line.String(k, v)
}
}

func (l lines) Time(k string, v time.Time) {
for _, line := range l {
line.Time(k, v)
Expand All @@ -303,6 +285,12 @@ func (l lines) Int64(k string, v int64, dt xopbase.DataType) {
}
}

func (l lines) String(k string, v string, dt xopbase.DataType) {
for _, line := range l {
line.String(k, v, dt)
}
}

func (l lines) Uint64(k string, v uint64, dt xopbase.DataType) {
for _, line := range l {
line.Uint64(k, v, dt)
Expand Down
17 changes: 14 additions & 3 deletions line.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ func (line *Line) Enum(k *xopat.EnumAttribute, v xopat.Enum) *Line {
return line
}

func (line *Line) Error(k string, v error) *Line {
if line.skip {
return line
}
if line.log.settings.redactError != nil {
line.log.settings.redactError(line.line, k, v)
return line
}
line.line.String(k, v.Error(), xopbase.ErrorDataType)
return line
}

func (line *Line) Stringer(k string, v fmt.Stringer) *Line {
if line.skip {
return line
Expand All @@ -78,7 +90,7 @@ func (line *Line) Stringer(k string, v fmt.Stringer) *Line {
line.log.settings.redactString(line.line, k, v.String())
return line
}
line.line.String(k, v.String())
line.line.String(k, v.String(), xopbase.StringerDataType)
return line
}

Expand All @@ -90,13 +102,12 @@ func (line *Line) String(k string, v string) *Line {
line.log.settings.redactString(line.line, k, v)
return line
}
line.line.String(k, v)
line.line.String(k, v, xopbase.StringDataType)
return line
}

func (line *Line) Bool(k string, v bool) *Line { line.line.Bool(k, v); return line }
func (line *Line) Duration(k string, v time.Duration) *Line { line.line.Duration(k, v); return line }
func (line *Line) Error(k string, v error) *Line { line.line.Error(k, v); return line }
func (line *Line) Link(k string, v trace.Trace) *Line { line.line.Link(k, v); return line }
func (line *Line) Time(k string, v time.Time) *Line { line.line.Time(k, v); return line }

Expand Down
20 changes: 16 additions & 4 deletions line.zzzgo
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ func (line *Line) Enum(k *xopat.EnumAttribute, v xopat.Enum) *Line {
return line
}

func (line *Line) Error(k string, v error) *Line {
if line.skip {
return line
}
if line.log.settings.redactError != nil {
line.log.settings.redactError(line.line, k, v)
return line
}
line.line.String(k, v.Error(), xopbase.ErrorDataType)
return line
}

func (line *Line) Stringer(k string, v fmt.Stringer) *Line {
if line.skip {
return line
Expand All @@ -71,7 +83,7 @@ func (line *Line) Stringer(k string, v fmt.Stringer) *Line {
line.log.settings.redactString(line.line, k, v.String())
return line
}
line.line.String(k, v.String())
line.line.String(k, v.String(), xopbase.StringerDataType)
return line
}

Expand All @@ -83,14 +95,14 @@ func (line *Line) String(k string, v string) *Line {
line.log.settings.redactString(line.line, k, v)
return line
}
line.line.String(k, v)
line.line.String(k, v, xopbase.StringDataType)
return line
}

// MACRO BaseDataWithoutType SKIP:Any,String
// MACRO BaseDataWithoutType SKIP:Any
func (line *Line) ZZZ(k string, v zzz) *Line { line.line.ZZZ(k, v); return line }

// MACRO BaseDataWithType
// MACRO BaseDataWithType SKIP:String
func (line *Line) ZZZ(k string, v zzz) *Line {
line.line.ZZZ(k, v, xopbase.ZZZDataType)
return line
Expand Down
144 changes: 101 additions & 43 deletions sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,31 @@ type Detaching struct {
// the function call.
type RedactAnyFunc func(baseLine xopbase.Line, k string, v interface{}, alreadyImmutable bool)

// RedactStringFunc is used to redact strings as they're bing logged.
// RedactStringFunc is used to redact strings as they're being logged.
// It is RedactStringFunc's responsiblity to call
//
// baseLine.String(k, v)
// baseLine.String(k, v, xopbase.StringDataType)
//
// if it wants the value to be logged.
//
// The provided xopbase.Line may not be retained beyond the duration of
// the function call.
type RedactStringFunc func(baseLine xopbase.Line, k string, v string)

// RedactErrorFunc is used to redact or format errors as they're being
// logged. It is RedactErrorFunc's responsibility to call
//
// baseLine.String(k, v.Error(), xopbase.ErrorDataType)
//
// if it wants the value to be logged. Alternatively, it could log the
// error as a model:
//
// baseLine.Any(k, v)
//
// The provided xopbase.Line may not be retained beyond the duration of
// the function call.
type RedactErrorFunc func(baseLine xopbase.Line, k string, v error)

type LogSettings struct {
prefillMsg string
prefillData []func(xopbase.Prefilling)
Expand All @@ -58,6 +72,7 @@ type LogSettings struct {
synchronousFlushWhenDone bool
redactAny RedactAnyFunc
redactString RedactStringFunc
redactError RedactErrorFunc
}

// DefaultSettings are the settings that are used if no setting changes
Expand Down Expand Up @@ -238,7 +253,7 @@ func (log *Log) sendPrefill() {
f(prefilling)
}
if log.settings.tagLinesWithSpanSequence {
prefilling.String(xopconst.SpanSequenceCode.Key(), log.span.seed.spanSequenceCode)
prefilling.String(xopconst.SpanSequenceCode.Key(), log.span.seed.spanSequenceCode, xopbase.StringDataType)
}
log.prefilled = prefilling.PrefillComplete(log.settings.prefillMsg)
}
Expand Down Expand Up @@ -273,11 +288,29 @@ func (settings *LogSettings) PrefillEnum(k *xopat.EnumAttribute, v xopat.Enum) {
})
}

// PrefillError is used to set a data element that is included on every log
// line. Errors will always be formatted with v.Error(). Redaction is
// not supported.
func (sub *Sub) PrefillError(k string, v error) *Sub {
sub.settings.PrefillError(k, v)
return sub
}

// PrefillError is used to set a data element that is included on every log
// line. Errors will always be formatted with v.Error(). Redaction is
// not supported.
func (settings *LogSettings) PrefillError(k string, v error) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.String(k, v.Error(), xopbase.ErrorDataType)
})
}

// PrefillAny is used to set a data element that is included on every log
// line. Values provided with PrefillAny will be copied
// using https://github.com/mohae/deepcopy 's Copy().
// PrefillAny is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
// Redaction is not supported.
func (sub *Sub) PrefillAny(k string, v interface{}) *Sub {
sub.settings.PrefillAny(k, v)
return sub
Expand All @@ -288,12 +321,28 @@ func (sub *Sub) PrefillAny(k string, v interface{}) *Sub {
// using https://github.com/mohae/deepcopy 's Copy().
// PrefillAny is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
// Redaction is not supported.
func (settings *LogSettings) PrefillAny(k string, v interface{}) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Any(k, v)
})
}

// PrefillFloat32 is used to set a data element that is included on every log
// line.
// PrefillFloat32 is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
func (sub *Sub) PrefillFloat32(k string, v float32) *Sub {
sub.settings.PrefillFloat32(k, v)
return sub
}

func (settings *LogSettings) PrefillFloat32(k string, v float32) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Float64(k, float64(v), xopbase.Float32DataType)
})
}

// PrefillBool is used to set a data element that is included on every log
// line.
// PrefillBool is not threadsafe with respect to other calls on the same *Sub.
Expand Down Expand Up @@ -324,21 +373,6 @@ func (settings *LogSettings) PrefillDuration(k string, v time.Duration) {
})
}

// PrefillError is used to set a data element that is included on every log
// line.
// PrefillError is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
func (sub *Sub) PrefillError(k string, v error) *Sub {
sub.settings.PrefillError(k, v)
return sub
}

func (settings *LogSettings) PrefillError(k string, v error) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Error(k, v)
})
}

// PrefillLink is used to set a data element that is included on every log
// line.
// PrefillLink is not threadsafe with respect to other calls on the same *Sub.
Expand All @@ -354,21 +388,6 @@ func (settings *LogSettings) PrefillLink(k string, v trace.Trace) {
})
}

// PrefillString is used to set a data element that is included on every log
// line.
// PrefillString is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
func (sub *Sub) PrefillString(k string, v string) *Sub {
sub.settings.PrefillString(k, v)
return sub
}

func (settings *LogSettings) PrefillString(k string, v string) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.String(k, v)
})
}

// PrefillTime is used to set a data element that is included on every log
// line.
// PrefillTime is not threadsafe with respect to other calls on the same *Sub.
Expand Down Expand Up @@ -414,6 +433,21 @@ func (settings *LogSettings) PrefillInt64(k string, v int64) {
})
}

// PrefillString is used to set a data element that is included on every log
// line.
// PrefillString is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
func (sub *Sub) PrefillString(k string, v string) *Sub {
sub.settings.PrefillString(k, v)
return sub
}

func (settings *LogSettings) PrefillString(k string, v string) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.String(k, v, xopbase.StringDataType)
})
}

// PrefillUint64 is used to set a data element that is included on every log
// line.
// PrefillUint64 is not threadsafe with respect to other calls on the same *Sub.
Expand Down Expand Up @@ -549,17 +583,41 @@ func (settings *LogSettings) PrefillUint8(k string, v uint8) {
})
}

// PrefillFloat32 is used to set a data element that is included on every log
// line.
// PrefillFloat32 is not threadsafe with respect to other calls on the same *Sub.
// Should not be used after Step(), Fork(), or Log() is called.
func (sub *Sub) PrefillFloat32(k string, v float32) *Sub {
sub.settings.PrefillFloat32(k, v)
// SetRedactAnyFunc sets a redaction function to be used
// when Line.Any() is called.
func (sub *Sub) SetRedactAnyFunc(f RedactAnyFunc) *Sub {
sub.settings.SetRedactAnyFunc(f)
return sub
}

func (settings *LogSettings) PrefillFloat32(k string, v float32) {
settings.prefillData = append(settings.prefillData, func(line xopbase.Prefilling) {
line.Float64(k, float64(v), xopbase.Float32DataType)
})
// SetRedactErrorFunc sets a redaction function to be used
// when Line.Error() is called.
func (sub *Sub) SetRedactErrorFunc(f RedactErrorFunc) *Sub {
sub.settings.SetRedactErrorFunc(f)
return sub
}

// SetRedactStringFunc sets a redaction function to be used
// when Line.String() is called.
func (sub *Sub) SetRedactStringFunc(f RedactStringFunc) *Sub {
sub.settings.SetRedactStringFunc(f)
return sub
}

// SetRedactAnyFunc sets a redaction function to be used
// when Line.Any() is called.
func (settings *LogSettings) SetRedactAnyFunc(f RedactAnyFunc) {
settings.redactAny = f
}

// SetRedactErrorFunc sets a redaction function to be used
// when Line.Error() is called.
func (settings *LogSettings) SetRedactErrorFunc(f RedactErrorFunc) {
settings.redactError = f
}

// SetRedactStringFunc sets a redaction function to be used
// when Line.String() is called.
func (settings *LogSettings) SetRedactStringFunc(f RedactStringFunc) {
settings.redactString = f
}
Loading

0 comments on commit a9deaf7

Please sign in to comment.