Skip to content

Commit 13f33b6

Browse files
Add custom metrics integration test (#343)
* increase example test coverage * add comment to check if setEnv configs are being read * skip failure case for now * remove publisher tests * sort imports * fix successfully spelling * add intergration test for custom metrics * fix import sort --------- Co-authored-by: aryan-mehrotra-zs <[email protected]>
1 parent 86f531d commit 13f33b6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

examples/using-custom-metrics/configs/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
HTTP_PORT=9011
2+
13
APP_NAME=using-metrics
24
APP_VERSION=v0.1.0
35

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"net/http"
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestIntegration(t *testing.T) {
13+
const host = "http://localhost:9011"
14+
go main()
15+
time.Sleep(time.Second * 1) // Giving some time to start the server
16+
17+
c := http.Client{}
18+
19+
req, _ := http.NewRequest("POST", host+"/transaction", nil)
20+
21+
_, err := c.Do(req)
22+
if err != nil {
23+
t.Fatalf("request to /transaction failed %v", err)
24+
}
25+
26+
req, _ = http.NewRequest("POST", host+"/return", nil)
27+
28+
_, err = c.Do(req)
29+
if err != nil {
30+
t.Fatalf("request to /transaction failed %v", err)
31+
}
32+
33+
req, _ = http.NewRequest("GET", "http://localhost:2121/metrics", nil)
34+
35+
resp, err := c.Do(req)
36+
if err != nil {
37+
t.Fatalf("request to localhost:2121/metrics failed %v", err)
38+
}
39+
40+
body, _ := io.ReadAll(resp.Body)
41+
42+
strBody := string(body)
43+
44+
assert.Equal(t, http.StatusOK, resp.StatusCode, "TEST[%d], Failed.\n%s")
45+
46+
assert.Contains(t, strBody, `product_stock{otel_scope_name="using-metrics",otel_scope_version="v0.1.0"} 50`)
47+
assert.Contains(t, strBody, `total_credit_day_sale{otel_scope_name="using-metrics",otel_scope_version="v0.1.0",sale_type="credit"} 1000`)
48+
assert.Contains(t, strBody, `total_credit_day_sale{otel_scope_name="using-metrics",otel_scope_version="v0.1.0",sale_type="credit_return"} -1000`)
49+
assert.Contains(t, strBody, `transaction_success_total{otel_scope_name="using-metrics",otel_scope_version="v0.1.0"} 1`)
50+
assert.Contains(t, strBody, "transaction_time")
51+
}

0 commit comments

Comments
 (0)