| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"text/template" |
| 8 |
) |
| 9 |
|
| 10 |
func main() { |
| 11 |
Run(metricsTemplate, metricsTestTemplate, os.Args...) |
| 12 |
} |
| 13 |
|
| 14 |
var metricsTemplate = template.Must(template.New("").Parse(`package {{.Package}} |
| 15 |
|
| 16 |
// go generate {{.Args}} |
| 17 |
// GENERATED CODE FOLLOWS; DO NOT EDIT. |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
{{if .Type "ET"}} |
| 22 |
"strings" |
| 23 |
{{end}} |
| 24 |
"github.com/mesos/mesos-go/api/v1/lib/extras/metrics" |
| 25 |
{{range .Imports}} |
| 26 |
{{ printf "%q" . -}} |
| 27 |
{{end}} |
| 28 |
) |
| 29 |
|
| 30 |
{{.RequireType "E" -}} |
| 31 |
|
| 32 |
// Labeler generates a set of strings that should be associated with metrics that are generated for the given event. |
| 33 |
type Labeler func(ctx context.Context, e {{.Type "E"}}) []string |
| 34 |
|
| 35 |
{{if .Type "ET" -}} |
| 36 |
var defaultLabels = func() map[{{.Type "ET"}}][]string { |
| 37 |
m := make(map[{{.Type "ET"}}][]string) |
| 38 |
for k, v := range {{.Type "ET"}}_name { |
| 39 |
m[{{.Type "ET"}}(k)] = []string{strings.ToLower(v)} |
| 40 |
} |
| 41 |
return m |
| 42 |
}() |
| 43 |
|
| 44 |
func defaultLabeler(ctx context.Context, e {{.Type "E"}}) []string { |
| 45 |
return defaultLabels[e.GetType()] |
| 46 |
} |
| 47 |
|
| 48 |
{{end -}} |
| 49 |
// Metrics generates a Rule that invokes the given harness for each event, using the labels generated by the Labeler. |
| 50 |
// Panics if harness {{if .Type "ET"}}or labeler {{end}}is nil. |
| 51 |
func Metrics(harness metrics.Harness, labeler Labeler) Rule { |
| 52 |
if harness == nil { |
| 53 |
panic("harness is a required parameter") |
| 54 |
} |
| 55 |
{{if .Type "ET" -}} |
| 56 |
if labeler == nil { |
| 57 |
labeler = defaultLabeler |
| 58 |
} |
| 59 |
{{else -}} |
| 60 |
if labeler == nil { |
| 61 |
panic("labeler is a required parameter") |
| 62 |
} |
| 63 |
{{end -}} |
| 64 |
return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) { |
| 65 |
labels := labeler(ctx, e) |
| 66 |
harness(func() error { |
| 67 |
ctx, e, {{.Ref "Z" "z," -}} err = ch(ctx, e, {{.Ref "Z" "z," -}} err) |
| 68 |
return err |
| 69 |
}, labels...) |
| 70 |
return ctx, e, {{.Ref "Z" "z," -}} err |
| 71 |
} |
| 72 |
} |
| 73 |
`)) |
| 74 |
|
| 75 |
var metricsTestTemplate = template.Must(template.New("").Parse(`package {{.Package}} |
| 76 |
|
| 77 |
// go generate {{.Args}} |
| 78 |
// GENERATED CODE FOLLOWS; DO NOT EDIT. |
| 79 |
|
| 80 |
import ( |
| 81 |
"context" |
| 82 |
"errors" |
| 83 |
"reflect" |
| 84 |
"testing" |
| 85 |
{{range .Imports}} |
| 86 |
{{ printf "%q" . -}} |
| 87 |
{{end}} |
| 88 |
) |
| 89 |
|
| 90 |
{{.RequireType "E" -}} |
| 91 |
{{.RequirePrototype "E" -}} |
| 92 |
{{.RequirePrototype "Z" -}} |
| 93 |
func TestMetrics(t *testing.T) { |
| 94 |
var ( |
| 95 |
i int |
| 96 |
ctx = context.Background() |
| 97 |
p = {{.Prototype "E"}} |
| 98 |
a = errors.New("a") |
| 99 |
h = func(f func() error, _ ...string) error { |
| 100 |
i++ |
| 101 |
return f() |
| 102 |
} |
| 103 |
) |
| 104 |
{{if .Type "Z" -}} |
| 105 |
var zp = {{.Prototype "Z"}} |
| 106 |
{{end -}} |
| 107 |
for ti, tc := range []struct { |
| 108 |
ctx context.Context |
| 109 |
e {{.Type "E"}} |
| 110 |
{{if .Type "Z"}} |
| 111 |
{{- .Arg "Z" "z "}} |
| 112 |
{{end -}} |
| 113 |
err error |
| 114 |
}{ |
| 115 |
{ctx, p, {{.Ref "Z" "zp," -}} a}, |
| 116 |
{ctx, p, {{.Ref "Z" "zp," -}} nil}, |
| 117 |
{{if .Type "Z"}} |
| 118 |
{ctx, p, {{.Ref "Z" "nil," -}} a}, |
| 119 |
{{end -}} |
| 120 |
{ctx, nil, {{.Ref "Z" "zp," -}} a}, |
| 121 |
} { |
| 122 |
for ri, r := range []Rule{ |
| 123 |
Metrics(h, nil), // default labeler |
| 124 |
Metrics(h, func(_ context.Context, _ {{.Type "E"}}) []string { return nil }), // custom labeler |
| 125 |
} { |
| 126 |
c, e, {{.Ref "Z" "z," -}} err := r.Eval(tc.ctx, tc.e, {{.Ref "Z" "tc.z," -}} tc.err, ChainIdentity) |
| 127 |
if !reflect.DeepEqual(c, tc.ctx) { |
| 128 |
t.Errorf("test case %d: expected context %q instead of %q", ti, tc.ctx, c) |
| 129 |
} |
| 130 |
if !reflect.DeepEqual(e, tc.e) { |
| 131 |
t.Errorf("test case %d: expected event %q instead of %q", ti, tc.e, e) |
| 132 |
} |
| 133 |
{{if .Type "Z" -}} |
| 134 |
if !reflect.DeepEqual(z, tc.z) { |
| 135 |
t.Errorf("expected return object %q instead of %q", z, tc.z) |
| 136 |
} |
| 137 |
{{end -}} |
| 138 |
if !reflect.DeepEqual(err, tc.err) { |
| 139 |
t.Errorf("test case %d: expected error %q instead of %q", ti, tc.err, err) |
| 140 |
} |
| 141 |
if y := (ti * 2) + ri + 1; y != i { |
| 142 |
t.Errorf("test case %d: expected count %q instead of %q", ti, y, i) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
func() { |
| 147 |
defer func() { |
| 148 |
if x := recover(); x != nil { |
| 149 |
t.Log("intercepted expected panic", x) |
| 150 |
} |
| 151 |
}() |
| 152 |
_ = Metrics(nil, nil) |
| 153 |
t.Fatalf("expected a panic because nil harness is not allowed") |
| 154 |
}() |
| 155 |
} |
| 156 |
`)) |