-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatbinder.go
More file actions
35 lines (30 loc) · 1.24 KB
/
statbinder.go
File metadata and controls
35 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package stats
import (
"context"
"github.com/asecurityteam/messageprocessor"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/rs/xstats"
)
// StatBinder is a `MessageProcessor` decorator that injects an
// `xstats.XStater` into the `context.Context` of the given `context`.
//
// The `StatBinder` can then, in turn, be decorated with `Transport` to
// make use of the injected `xstats.XStater` to emit key HTTP metrics on
// each ProcessMessage.
type StatBinder struct {
stats xstats.XStater
wrapped messageprocessor.MessageProcessor
}
// ProcessMessage injects an `xstats.XStater` into the context and invokes the
// wrapped `MessageProcessor`.
func (t *StatBinder) ProcessMessage(ctx context.Context, record *kinesis.Record) messageprocessor.MessageProcessorError {
ctx = xstats.NewContext(ctx, xstats.Copy(t.stats))
return t.wrapped.ProcessMessage(ctx, record)
}
// NewStatBinder returns a function that wraps a `transport.Decorator` in a
// `StatTransport` `transport.Decorator`.
func NewStatBinder(stats xstats.XStater) func(messageprocessor.MessageProcessor) messageprocessor.MessageProcessor {
return func(next messageprocessor.MessageProcessor) messageprocessor.MessageProcessor {
return &StatBinder{stats: stats, wrapped: next}
}
}