This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand.go
67 lines (57 loc) · 1.68 KB
/
command.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Package command provides utilities for creating MDM Payloads.
package command
import (
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
"github.com/micromdm/mdm"
"golang.org/x/net/context"
)
type Service interface {
NewCommand(context.Context, *mdm.CommandRequest) (*mdm.Payload, error)
}
// Middleware describes a service (as opposed to endpoint) middleware.
type Middleware func(Service) Service
// ServiceLoggingMiddleware returns a service middleware that logs the
// parameters and result of each method invocation.
func ServiceLoggingMiddleware(logger log.Logger) Middleware {
return func(next Service) Service {
return serviceLoggingMiddleware{
logger: logger,
next: next,
}
}
}
func (mw serviceLoggingMiddleware) NewCommand(ctx context.Context, req *mdm.CommandRequest) (p *mdm.Payload, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "NewCommand",
"error", err,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.NewCommand(ctx, req)
}
type serviceLoggingMiddleware struct {
logger log.Logger
next Service
}
// ServiceInstrumentingMiddleware returns a service middleware that tracks the
// number of payloads created by the service.
func ServiceInstrumentingMiddleware(p metrics.Counter) Middleware {
return func(next Service) Service {
return serviceInstrumentingMiddleware{
payloads: p,
next: next,
}
}
}
type serviceInstrumentingMiddleware struct {
payloads metrics.Counter
next Service
}
func (mw serviceInstrumentingMiddleware) NewCommand(ctx context.Context, req *mdm.CommandRequest) (*mdm.Payload, error) {
p, err := mw.next.NewCommand(ctx, req)
mw.payloads.Add(1)
return p, err
}