@@ -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+
64186func 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+
208358func sendLogsToCollector (logs map [string ]interface {}) error {
209359 jsonData , err := json .Marshal (logs )
210360 if err != nil {
0 commit comments