Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add observer package #230

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func yourAction(c *cli.Context) error {
// Run your application here...
}
```

## Flags

### Logger
Expand Down Expand Up @@ -154,3 +155,50 @@ Example: `--tracing.endpoint="agent-host:port"` or `--tracing.endpoint="http://h
This flag sets the sample ratio of spans that will be reported to the exporter. This should be between 0 and 1.

Example: `--tracing.ratio=0.2`

### Observer

The observe package exposes an `Observer` type which is essentially a helper that combines a logger, tracer and statter.
It is useful if you use all three for your services and want to avoid carrying around many arguments.

Here is an example of how one might use it:

```go
func runServer(c *cli.Context) error {
ctx, cancel := context.WithCancel(c.Context)
defer cancel()

obsvr, err := cmd.New(c, svcName, version)
if err != nil {
return err
}
defer obsvr.Close()

// Initialize components by passing the obsvr variable.
return nil
}

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
}
```

It also exposes `NewFake` which allows you to pass fake loggers, tracers and statters in your tests easily.
28 changes: 28 additions & 0 deletions observe/doc.go
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
56 changes: 56 additions & 0 deletions observe/observer.go
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)
}
Loading