-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_metrics_test.go
101 lines (81 loc) · 2.45 KB
/
example_metrics_test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package queue_test
import (
"context"
"fmt"
"time"
queue "github.com/DoNewsCode/core-queue"
"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/otredis"
"github.com/go-kit/kit/metrics/prometheus"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/oklog/run"
stdprometheus "github.com/prometheus/client_golang/prometheus"
)
type MockMetricsData struct {
Value string
}
type MockMetricsListener struct{}
func (m MockMetricsListener) Listen() queue.Job {
return queue.JobFrom(MockMetricsData{})
}
func (m MockMetricsListener) Process(_ context.Context, Job queue.Job) error {
fmt.Println(Job.Data().(MockMetricsData).Value)
return nil
}
// bootstrapMetrics is normally done when bootstrapping the framework. We mimic it here for demonstration.
func bootstrapMetrics() *core.C {
const sampleConfig = "{\"log\":{\"level\":\"error\"},\"queue\":{\"default\":{\"parallelism\":1}}}"
// Make sure redis is running at localhost:6379
c := core.New(
core.WithConfigStack(rawbytes.Provider([]byte(sampleConfig)), json.Parser()),
)
// Add ConfProvider
c.ProvideEssentials()
c.Provide(otredis.Providers())
c.Provide(queue.Providers())
c.Provide(di.Deps{func(appName contract.AppName, env contract.Env) queue.Gauge {
return prometheus.NewGaugeFrom(
stdprometheus.GaugeOpts{
Namespace: appName.String(),
Subsystem: env.String(),
Name: "queue_length",
Help: "The gauge JobFrom queue length",
}, []string{"name", "channel"},
)
}})
return c
}
// serveMetrics normally lives at serveMetrics command. We mimic it here for demonstration.
func serveMetrics(c *core.C, duration time.Duration) {
var g run.Group
c.ApplyRunGroup(&g)
// cancel the run group after some time, so that the program ends. In real project, this is not necessary.
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
g.Add(func() error {
<-ctx.Done()
return nil
}, func(err error) {
cancel()
})
err := g.Run()
if err != nil {
panic(err)
}
}
func Example_metrics() {
c := bootstrapMetrics()
c.Invoke(func(dispatcher *queue.Queue) {
// Subscribe
dispatcher.Subscribe(MockMetricsListener{})
// Trigger an Job
evt := queue.JobFrom(MockMetricsData{Value: "hello world"})
_ = dispatcher.Dispatch(context.Background(), queue.Adjust(evt))
})
serveMetrics(c, time.Second)
// Output:
// hello world
}