Skip to content

Commit 077d9e1

Browse files
committed
add Prometheus target and verify plugin metrics
1 parent e515a9e commit 077d9e1

File tree

7 files changed

+254
-6
lines changed

7 files changed

+254
-6
lines changed

framework/cmd/observability/compose/conf/prometheus.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ global:
22
scrape_interval: 10s
33

44
scrape_configs:
5+
- job_name: 'otel-collector'
6+
scrape_interval: 10s
7+
static_configs:
8+
- targets: [ 'otel-collector:8889' ]
59
- job_name: 'ctf'
610
metrics_path: /metrics
711
docker_sd_configs:

framework/cmd/observability/compose/docker-compose.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ services:
2020
command:
2121
- '--config=/etc/otel/config.yaml'
2222
ports:
23-
- "4317:4317" #grpc
24-
- "4318:4318" #http
23+
- "4317:4317" # grpc
24+
- "4318:4318" # http
25+
- "8889:8889" # Prometheus scrape target
2526
depends_on:
2627
- loki
2728
cadvisor:

framework/cmd/observability/compose/otel.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ exporters:
2222
endpoint: "http://tempo:4317"
2323
tls:
2424
insecure: true
25+
prometheus:
26+
endpoint: "0.0.0.0:8889"
2527

2628
service:
2729
pipelines:
2830
traces:
2931
receivers: [otlp]
32+
processors: [batch]
3033
exporters: [debug, otlp]
3134
logs:
3235
receivers: [otlp]
3336
processors: [batch]
34-
exporters: [debug, otlphttp/logs]
37+
exporters: [debug, otlphttp/logs]
38+
metrics:
39+
receivers: [otlp]
40+
processors: [batch]
41+
exporters: [debug, prometheus]

framework/cmd/observability/compose/tempo.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ distributor:
88
grpc:
99
http:
1010

11+
overrides:
12+
max_traces_per_user: 50000
13+
1114
storage:
1215
trace:
1316
backend: local
1417
local:
1518
path: /tmp/tempo/blocks
1619
wal:
17-
path: /tmp/tempo/wal
20+
path: /tmp/tempo/wal
21+
22+
compactor:
23+
compaction:
24+
max_block_bytes: 1073741824 # 1GB max block size
25+
max_compaction_objects: 1000000

framework/cmd/otel_test.go

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,31 @@ import (
1818
"go.opentelemetry.io/otel/trace"
1919
)
2020

21-
func TestSendLogsToOTELCollector(t *testing.T) {
21+
func TestOTELSendMetricsToOTELCollector(t *testing.T) {
22+
t.Skip("run manually to debug otel-collector")
23+
res, err := resource.New(context.Background(),
24+
resource.WithAttributes(
25+
semconv.ServiceName("test-metrics-service"),
26+
semconv.ServiceVersion("1.0.0"),
27+
attribute.String("environment", "test"),
28+
),
29+
)
30+
if err != nil {
31+
t.Fatalf("failed to create resource: %v", err)
32+
}
33+
34+
metrics := generateMetricsData(res)
35+
jsonData, _ := json.MarshalIndent(metrics, "", " ")
36+
t.Logf("Sending metrics payload:\n%s\n", string(jsonData))
37+
38+
err = sendMetricsToCollector(metrics)
39+
if err != nil {
40+
t.Fatalf("failed to send metrics: %v", err)
41+
}
42+
t.Log("Metrics sent successfully to OTEL Collector!")
43+
}
44+
45+
func TestOTELSendLogsToOTELCollector(t *testing.T) {
2246
t.Skip("run manually to debug otel-collector")
2347
res, err := resource.New(context.Background(),
2448
resource.WithAttributes(
@@ -39,7 +63,7 @@ func TestSendLogsToOTELCollector(t *testing.T) {
3963
t.Log("Logs sent successfully!")
4064
}
4165

42-
func TestSendTracesToOTELCollector(t *testing.T) {
66+
func TestOTELSendTracesToOTELCollector(t *testing.T) {
4367
t.Skip("run manually to debug otel-collector")
4468
res, err := resource.New(context.Background(),
4569
resource.WithAttributes(
@@ -61,6 +85,104 @@ func TestSendTracesToOTELCollector(t *testing.T) {
6185
t.Log("Traces sent successfully to OTEL Collector!")
6286
}
6387

88+
func generateMetricsData(res *resource.Resource) map[string]interface{} {
89+
now := time.Now()
90+
nanos := now.UnixNano()
91+
startTime := now.Add(-5 * time.Minute).UnixNano()
92+
93+
return map[string]interface{}{
94+
"resourceMetrics": []interface{}{
95+
map[string]interface{}{
96+
"resource": map[string]interface{}{
97+
"attributes": resourceToAttributes(res),
98+
},
99+
"scopeMetrics": []interface{}{
100+
map[string]interface{}{
101+
"scope": map[string]interface{}{
102+
"name": "test-metrics",
103+
"version": "1.0",
104+
},
105+
"metrics": []interface{}{
106+
// Counter example
107+
map[string]interface{}{
108+
"name": "http.requests",
109+
"description": "Count of HTTP requests",
110+
"unit": "1",
111+
"sum": map[string]interface{}{
112+
"dataPoints": []interface{}{
113+
map[string]interface{}{
114+
"startTimeUnixNano": startTime,
115+
"timeUnixNano": nanos,
116+
"asInt": "42",
117+
"attributes": []interface{}{
118+
map[string]interface{}{
119+
"key": "http.method",
120+
"value": map[string]interface{}{"stringValue": "GET"},
121+
},
122+
map[string]interface{}{
123+
"key": "http.status",
124+
"value": map[string]interface{}{"stringValue": "200"},
125+
},
126+
},
127+
},
128+
},
129+
"aggregationTemporality": 2, // Cumulative
130+
"isMonotonic": true,
131+
},
132+
},
133+
// Gauge example
134+
map[string]interface{}{
135+
"name": "memory.usage",
136+
"description": "Memory usage in bytes",
137+
"unit": "By",
138+
"gauge": map[string]interface{}{
139+
"dataPoints": []interface{}{
140+
map[string]interface{}{
141+
"timeUnixNano": nanos,
142+
"asDouble": 1024.42,
143+
"attributes": []interface{}{
144+
map[string]interface{}{
145+
"key": "memory.type",
146+
"value": map[string]interface{}{"stringValue": "heap"},
147+
},
148+
},
149+
},
150+
},
151+
},
152+
},
153+
// Histogram example
154+
map[string]interface{}{
155+
"name": "http.request.duration",
156+
"description": "HTTP request duration in seconds",
157+
"unit": "s",
158+
"histogram": map[string]interface{}{
159+
"dataPoints": []interface{}{
160+
map[string]interface{}{
161+
"startTimeUnixNano": startTime,
162+
"timeUnixNano": nanos,
163+
"count": "5",
164+
"sum": 4.2,
165+
"bucketCounts": []interface{}{"0", "1", "3", "1", "0"},
166+
"explicitBounds": []interface{}{"0.1", "0.5", "1", "2"},
167+
"attributes": []interface{}{
168+
map[string]interface{}{
169+
"key": "http.route",
170+
"value": map[string]interface{}{"stringValue": "/api/users"},
171+
},
172+
},
173+
},
174+
},
175+
"aggregationTemporality": 2, // Cumulative
176+
},
177+
},
178+
},
179+
},
180+
},
181+
},
182+
},
183+
}
184+
}
185+
64186
func generateLogRecords(res *resource.Resource) map[string]interface{} {
65187
now := time.Now()
66188
nanos := now.UnixNano()
@@ -205,6 +327,34 @@ func resourceToAttributes(res *resource.Resource) []interface{} {
205327
return attrs
206328
}
207329

330+
func sendMetricsToCollector(metrics map[string]interface{}) error {
331+
jsonData, err := json.Marshal(metrics)
332+
if err != nil {
333+
return fmt.Errorf("failed to marshal metrics: %w", err)
334+
}
335+
336+
req, err := http.NewRequest("POST", "http://localhost:4318/v1/metrics", bytes.NewBuffer(jsonData))
337+
if err != nil {
338+
return fmt.Errorf("failed to create request: %w", err)
339+
}
340+
341+
req.Header.Set("Content-Type", "application/json")
342+
343+
client := &http.Client{}
344+
resp, err := client.Do(req)
345+
if err != nil {
346+
return fmt.Errorf("failed to send request: %w", err)
347+
}
348+
defer resp.Body.Close()
349+
350+
if resp.StatusCode >= 300 {
351+
body, _ := io.ReadAll(resp.Body)
352+
return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body))
353+
}
354+
355+
return nil
356+
}
357+
208358
func sendLogsToCollector(logs map[string]interface{}) error {
209359
jsonData, err := json.Marshal(logs)
210360
if err != nil {

framework/examples/myproject/go.mod

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ require (
4040
github.com/Masterminds/semver/v3 v3.3.1 // indirect
4141
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
4242
github.com/Microsoft/go-winio v0.6.2 // indirect
43+
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
4344
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
4445
github.com/armon/go-metrics v0.4.1 // indirect
4546
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
47+
github.com/atotto/clipboard v0.1.4 // indirect
4648
github.com/avast/retry-go v3.0.0+incompatible // indirect
4749
github.com/awalterschulze/gographviz v2.0.3+incompatible // indirect
4850
github.com/aws/aws-sdk-go v1.55.6 // indirect
@@ -60,16 +62,27 @@ require (
6062
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 // indirect
6163
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2 // indirect
6264
github.com/aws/smithy-go v1.22.1 // indirect
65+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
6366
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
6467
github.com/benbjohnson/clock v1.3.5 // indirect
6568
github.com/beorn7/perks v1.0.1 // indirect
6669
github.com/bits-and-blooms/bitset v1.17.0 // indirect
6770
github.com/bytedance/sonic v1.12.3 // indirect
6871
github.com/bytedance/sonic/loader v0.2.0 // indirect
6972
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 // indirect
73+
github.com/catppuccin/go v0.2.0 // indirect
7074
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
7175
github.com/cespare/xxhash/v2 v2.3.0 // indirect
7276
github.com/chaos-mesh/chaos-mesh/api v0.0.0-20240821051457-da69c6d9617a // indirect
77+
github.com/charmbracelet/bubbles v0.20.0 // indirect
78+
github.com/charmbracelet/bubbletea v1.1.1 // indirect
79+
github.com/charmbracelet/huh v0.6.0 // indirect
80+
github.com/charmbracelet/huh/spinner v0.0.0-20241028115900-20a4d21717a8 // indirect
81+
github.com/charmbracelet/lipgloss v0.13.0 // indirect
82+
github.com/charmbracelet/x/ansi v0.2.3 // indirect
83+
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
84+
github.com/charmbracelet/x/term v0.2.0 // indirect
85+
github.com/cloudflare/circl v1.1.0 // indirect
7386
github.com/cloudwego/base64x v0.1.4 // indirect
7487
github.com/cloudwego/iasm v0.2.0 // indirect
7588
github.com/coder/websocket v1.8.12 // indirect
@@ -80,6 +93,7 @@ require (
8093
github.com/coreos/go-semver v0.3.1 // indirect
8194
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
8295
github.com/cpuguy83/dockercfg v0.3.2 // indirect
96+
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
8397
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
8498
github.com/crate-crypto/go-kzg-4844 v1.1.0 // indirect
8599
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
@@ -95,6 +109,7 @@ require (
95109
github.com/edsrzf/mmap-go v1.2.0 // indirect
96110
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
97111
github.com/envoyproxy/go-control-plane/envoy v1.32.3 // indirect
112+
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
98113
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
99114
github.com/ethereum/go-verkle v0.2.2 // indirect
100115
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
@@ -134,6 +149,8 @@ require (
134149
github.com/google/btree v1.1.3 // indirect
135150
github.com/google/gnostic-models v0.6.8 // indirect
136151
github.com/google/go-cmp v0.6.0 // indirect
152+
github.com/google/go-github/v50 v50.2.0 // indirect
153+
github.com/google/go-querystring v1.1.0 // indirect
137154
github.com/google/gofuzz v1.2.0 // indirect
138155
github.com/google/s2a-go v0.1.9 // indirect
139156
github.com/google/uuid v1.6.0 // indirect
@@ -173,16 +190,20 @@ require (
173190
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
174191
github.com/kylelemons/godebug v1.1.0 // indirect
175192
github.com/leodido/go-urn v1.4.0 // indirect
193+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
176194
github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a // indirect
177195
github.com/magiconair/properties v1.8.7 // indirect
178196
github.com/mailru/easyjson v0.7.7 // indirect
179197
github.com/mattn/go-colorable v0.1.13 // indirect
180198
github.com/mattn/go-isatty v0.0.20 // indirect
199+
github.com/mattn/go-localereader v0.0.1 // indirect
200+
github.com/mattn/go-runewidth v0.0.16 // indirect
181201
github.com/mdlayher/socket v0.5.1 // indirect
182202
github.com/mdlayher/vsock v1.2.1 // indirect
183203
github.com/miekg/dns v1.1.63 // indirect
184204
github.com/mitchellh/copystructure v1.2.0 // indirect
185205
github.com/mitchellh/go-homedir v1.1.0 // indirect
206+
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
186207
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
187208
github.com/mitchellh/reflectwalk v1.0.2 // indirect
188209
github.com/mmcloughlin/addchain v0.4.0 // indirect
@@ -197,6 +218,9 @@ require (
197218
github.com/montanaflynn/stats v0.7.1 // indirect
198219
github.com/morikuni/aec v1.0.0 // indirect
199220
github.com/mr-tron/base58 v1.2.0 // indirect
221+
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
222+
github.com/muesli/cancelreader v0.2.2 // indirect
223+
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect
200224
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
201225
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
202226
github.com/oklog/ulid v1.3.1 // indirect
@@ -208,6 +232,7 @@ require (
208232
github.com/opentracing-contrib/go-grpc v0.1.1 // indirect
209233
github.com/opentracing-contrib/go-stdlib v1.1.0 // indirect
210234
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
235+
github.com/pelletier/go-toml v1.9.5 // indirect
211236
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
212237
github.com/pires/go-proxyproto v0.7.0 // indirect
213238
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
@@ -223,8 +248,10 @@ require (
223248
github.com/prometheus/prometheus v0.302.0 // indirect
224249
github.com/prometheus/sigv4 v0.1.1 // indirect
225250
github.com/redis/go-redis/v9 v9.7.0 // indirect
251+
github.com/rivo/uniseg v0.4.7 // indirect
226252
github.com/robfig/cron/v3 v3.0.1 // indirect
227253
github.com/rs/zerolog v1.33.0 // indirect
254+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
228255
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
229256
github.com/sercand/kuberesolver/v5 v5.1.1 // indirect
230257
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
@@ -250,7 +277,9 @@ require (
250277
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
251278
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
252279
github.com/ugorji/go/codec v1.2.12 // indirect
280+
github.com/urfave/cli/v2 v2.27.5 // indirect
253281
github.com/x448/float16 v0.8.4 // indirect
282+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
254283
github.com/yusufpapurcu/wmi v1.2.4 // indirect
255284
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
256285
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect

0 commit comments

Comments
 (0)