1 | // Copyright 2020 The Go Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style |
3 | // license that can be found in the LICENSE file. |
4 | |
5 | package ocagent_test |
6 | |
7 | import ( |
8 | "context" |
9 | "errors" |
10 | "testing" |
11 | |
12 | "golang.org/x/tools/internal/event" |
13 | "golang.org/x/tools/internal/event/keys" |
14 | ) |
15 | |
16 | func TestEncodeMetric(t *testing.T) { |
17 | exporter := registerExporter() |
18 | const prefix = testNodeStr + ` |
19 | "metrics":[` |
20 | const suffix = `]}` |
21 | tests := []struct { |
22 | name string |
23 | run func(ctx context.Context) |
24 | want string |
25 | }{ |
26 | { |
27 | name: "HistogramFloat64, HistogramInt64", |
28 | run: func(ctx context.Context) { |
29 | ctx = event.Label(ctx, keyMethod.Of("godoc.ServeHTTP")) |
30 | event.Metric(ctx, latencyMs.Of(96.58)) |
31 | ctx = event.Label(ctx, keys.Err.Of(errors.New("panic: fatal signal"))) |
32 | event.Metric(ctx, bytesIn.Of(97e2)) |
33 | }, |
34 | want: prefix + ` |
35 | { |
36 | "metric_descriptor": { |
37 | "name": "latency_ms", |
38 | "description": "The latency of calls in milliseconds", |
39 | "type": 6, |
40 | "label_keys": [ |
41 | { |
42 | "key": "method" |
43 | }, |
44 | { |
45 | "key": "route" |
46 | } |
47 | ] |
48 | }, |
49 | "timeseries": [ |
50 | { |
51 | "start_timestamp": "1970-01-01T00:00:00Z", |
52 | "points": [ |
53 | { |
54 | "timestamp": "1970-01-01T00:00:40Z", |
55 | "distributionValue": { |
56 | "count": 1, |
57 | "sum": 96.58, |
58 | "bucket_options": { |
59 | "explicit": { |
60 | "bounds": [ |
61 | 0, |
62 | 5, |
63 | 10, |
64 | 25, |
65 | 50 |
66 | ] |
67 | } |
68 | }, |
69 | "buckets": [ |
70 | {}, |
71 | {}, |
72 | {}, |
73 | {}, |
74 | {} |
75 | ] |
76 | } |
77 | } |
78 | ] |
79 | } |
80 | ] |
81 | }, |
82 | { |
83 | "metric_descriptor": { |
84 | "name": "latency_ms", |
85 | "description": "The latency of calls in milliseconds", |
86 | "type": 6, |
87 | "label_keys": [ |
88 | { |
89 | "key": "method" |
90 | }, |
91 | { |
92 | "key": "route" |
93 | } |
94 | ] |
95 | }, |
96 | "timeseries": [ |
97 | { |
98 | "start_timestamp": "1970-01-01T00:00:00Z", |
99 | "points": [ |
100 | { |
101 | "timestamp": "1970-01-01T00:00:40Z", |
102 | "distributionValue": { |
103 | "count": 1, |
104 | "sum": 9700, |
105 | "bucket_options": { |
106 | "explicit": { |
107 | "bounds": [ |
108 | 0, |
109 | 10, |
110 | 50, |
111 | 100, |
112 | 500, |
113 | 1000, |
114 | 2000 |
115 | ] |
116 | } |
117 | }, |
118 | "buckets": [ |
119 | {}, |
120 | {}, |
121 | {}, |
122 | {}, |
123 | {}, |
124 | {}, |
125 | {} |
126 | ] |
127 | } |
128 | } |
129 | ] |
130 | } |
131 | ] |
132 | }` + suffix, |
133 | }, |
134 | } |
135 | |
136 | ctx := context.TODO() |
137 | for _, tt := range tests { |
138 | t.Run(tt.name, func(t *testing.T) { |
139 | tt.run(ctx) |
140 | got := exporter.Output("/v1/metrics") |
141 | checkJSON(t, got, []byte(tt.want)) |
142 | }) |
143 | } |
144 | } |
145 |
Members