-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_test.go
More file actions
147 lines (117 loc) · 3.91 KB
/
example_test.go
File metadata and controls
147 lines (117 loc) · 3.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package otlpwire_test
import (
"bytes"
"fmt"
"hash/fnv"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.olly.garden/otlp-wire"
)
// Example_observabilityStats demonstrates using Count() for observability metrics.
func Example_observabilityStats() {
// Simulate receiving OTLP metrics data
metrics := createSampleMetrics(100)
marshaler := &pmetric.ProtoMarshaler{}
otlpBytes, _ := marshaler.MarshalMetrics(metrics)
// Count signals for observability
data := otlpwire.ExportMetricsServiceRequest(otlpBytes)
count, _ := data.DataPointCount()
// Emit metrics about incoming data (cardinality monitoring, billing, etc.)
fmt.Printf("Received %d data points for processing\n", count)
// Output: Received 100 data points for processing
}
// Example_shardingByService demonstrates splitting batches for distributed processing.
func Example_shardingByService() {
// Create metrics from multiple services
metrics := createMultiServiceMetrics()
marshaler := &pmetric.ProtoMarshaler{}
otlpBytes, _ := marshaler.MarshalMetrics(metrics)
// Split batch by resource for sharding
data := otlpwire.ExportMetricsServiceRequest(otlpBytes)
numWorkers := 3
resources, getErr := data.ResourceMetrics()
i := 0
for resource := range resources {
// Hash resource for consistent routing
resourceBytes, _ := resource.Resource()
hash := hashBytes(resourceBytes)
workerID := int(hash % uint64(numWorkers))
var buf bytes.Buffer
_, _ = resource.WriteTo(&buf)
count, _ := otlpwire.ExportMetricsServiceRequest(buf.Bytes()).DataPointCount()
fmt.Printf("Resource %d → Worker %d (%d data points)\n", i, workerID, count)
i++
}
if err := getErr(); err != nil {
fmt.Printf("Error: %v\n", err)
}
// Output:
// Resource 0 → Worker 0 (10 data points)
// Resource 1 → Worker 1 (10 data points)
// Resource 2 → Worker 2 (10 data points)
}
// Example_typeComposition demonstrates how types compose naturally.
func Example_typeComposition() {
metrics := createSampleMetrics(25)
marshaler := &pmetric.ProtoMarshaler{}
otlpBytes, _ := marshaler.MarshalMetrics(metrics)
// Count at batch level
batch := otlpwire.ExportMetricsServiceRequest(otlpBytes)
count, _ := batch.DataPointCount()
fmt.Printf("Total data points: %d\n", count)
// Iterate and count at resource level (zero allocation)
resourceCount := 0
resources, getErr := batch.ResourceMetrics()
for resource := range resources {
if resourceCount == 0 {
// Count signals in this resource (zero allocation)
dpCount, _ := resource.DataPointCount()
fmt.Printf("Resource 0 data points: %d\n", dpCount)
}
resourceCount++
}
if err := getErr(); err != nil {
fmt.Printf("Error: %v\n", err)
}
fmt.Printf("Number of resources: %d\n", resourceCount)
// Output:
// Total data points: 25
// Resource 0 data points: 25
// Number of resources: 1
}
// Helper functions
func createSampleMetrics(dataPoints int) pmetric.Metrics {
metrics := pmetric.NewMetrics()
rm := metrics.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutStr("service.name", "test-service")
sm := rm.ScopeMetrics().AppendEmpty()
metric := sm.Metrics().AppendEmpty()
metric.SetName("test.metric")
gauge := metric.SetEmptyGauge()
for i := 0; i < dataPoints; i++ {
dp := gauge.DataPoints().AppendEmpty()
dp.SetIntValue(int64(i))
}
return metrics
}
func createMultiServiceMetrics() pmetric.Metrics {
metrics := pmetric.NewMetrics()
services := []string{"frontend", "backend", "database"}
for _, svc := range services {
rm := metrics.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutStr("service.name", svc)
sm := rm.ScopeMetrics().AppendEmpty()
metric := sm.Metrics().AppendEmpty()
metric.SetName("request.count")
gauge := metric.SetEmptyGauge()
for i := 0; i < 10; i++ {
dp := gauge.DataPoints().AppendEmpty()
dp.SetIntValue(int64(i))
}
}
return metrics
}
func hashBytes(data []byte) uint64 {
h := fnv.New64a()
_, _ = h.Write(data)
return h.Sum64()
}