-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Package observe implements a type that combines statter, logger and tracer. | ||
Example usage: | ||
func New(c *cli.Context, svc, version string) (*observe.Observer, error) { | ||
log, logCancel, err := NewLogger(c, svc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stats, statsCancel, err := NewStatter(c, log, svc) | ||
if err != nil { | ||
logCancel() | ||
return nil, err | ||
} | ||
tracer, traceCancel, err := NewTracer(c, log, svc, version) | ||
if err != nil { | ||
logCancel() | ||
statsCancel() | ||
return nil, err | ||
} | ||
return observe.New(log, stats, tracer, traceCancel, statsCancel, logCancel), nil | ||
} | ||
*/ | ||
package observe |
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,56 @@ | ||
package observe | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"github.com/hamba/logger/v2" | ||
"github.com/hamba/statter/v2" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// Observer contains observability primitives. | ||
type Observer struct { | ||
Log *logger.Logger | ||
Stats *statter.Statter | ||
TraceProv trace.TracerProvider | ||
|
||
closeFns []func() | ||
} | ||
|
||
// New returns an observer with the given observability primitives. | ||
func New(log *logger.Logger, stats *statter.Statter, traceProv trace.TracerProvider, closeFns ...func()) *Observer { | ||
return &Observer{ | ||
Log: log, | ||
Stats: stats, | ||
TraceProv: traceProv, | ||
closeFns: closeFns, | ||
} | ||
} | ||
|
||
// Tracer returns a tracer with the given name and options. | ||
// If no trace provider has been set, this function will panic. | ||
func (o *Observer) Tracer(name string, opts ...trace.TracerOption) trace.Tracer { | ||
if o.TraceProv == nil { | ||
panic("calling tracer when no trace provider has been set") | ||
} | ||
return o.TraceProv.Tracer(name, opts...) | ||
} | ||
|
||
// Close closes the observability primitives. | ||
func (o *Observer) Close() { | ||
for _, cancel := range o.closeFns { | ||
cancel() | ||
} | ||
} | ||
|
||
// NewFake returns a fake observer that reports nothing. | ||
// This is useful for tests. | ||
func NewFake() *Observer { | ||
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Error) | ||
stats := statter.New(statter.DiscardReporter, time.Minute) | ||
tracer := otel.GetTracerProvider() | ||
|
||
return New(log, stats, tracer) | ||
} |