Skip to content

Commit

Permalink
feat: [resty] Added resty instrument (#3)
Browse files Browse the repository at this point in the history
* feat: [resty] Added resty instrument

* feat: [resty] Update coverage logic exclude some test codes

* feat: [resty] Fixed codecov

* feat: [resty] Resort e2e test structure

* feat: [resty] Added codecov config

* feat: [resty] Added codecov config
  • Loading branch information
jiekun authored Jan 4, 2024
1 parent 41afeba commit b512e87
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 38 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ jobs:
go-version: '1.17'

- name: Test with coverage
run: go test -v -cover -timeout 5m -coverpkg=./... -parallel 1 -covermode=atomic -gcflags=all=-l $(go list ./test/e2e/...) -coverprofile=coverage.txt
run: go test -v -cover -timeout 5m -coverpkg=./... -parallel 1 -covermode=atomic -gcflags=all=-l $(go list ./test/e2e/...) -coverprofile=coverage_full.txt

- name: Filter unnecessary codes coverage
run: |
cat coverage_full.txt | grep -v "github.com/quwan-sre/observability-go-contrib/test" > coverage.txt
rm coverage_full.txt
- name: Upload coverage reports to Codecov
run: |
Expand Down
9 changes: 9 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coverage:
status:
project:
default:
target: 80% # the required coverage value
threshold: 5% # the leniency in hitting the target
patch:
default:
target: 80%
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -32,6 +32,8 @@ require (
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -116,6 +118,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -507,6 +511,9 @@ google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7Fc
google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 h1:9NWlQfY2ePejTmfwUH1OWwmznFa+0kKcHGPDvcPza9M=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand All @@ -519,6 +526,8 @@ google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKa
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand All @@ -534,6 +543,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
8 changes: 2 additions & 6 deletions metrics/common/rpc_enum.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package common

const (
RPCSDKGin = "github.com/gin-gonic/gin"
)

const (
RPCStatusSuccess = "0"
RPCStatusError = "1"
RPCSDKGin = "github.com/gin-gonic/gin"
RPCSDKResty = "github.com/go-resty/resty/v2"
)

const (
Expand Down
7 changes: 4 additions & 3 deletions metrics/common/rpc_metrics.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package common

import (
"github.com/prometheus/client_golang/prometheus"
"time"

"github.com/prometheus/client_golang/prometheus"
)

const (
Expand All @@ -19,7 +20,7 @@ var (
NativeHistogramMinResetDuration: 5 * time.Minute,
NativeHistogramMaxZeroThreshold: 0.05,
NativeHistogramMaxBucketNumber: 20,
}, []string{"sdk", "request_protocol", "endpoint", "status", "response_code"})
}, []string{"sdk", "request_protocol", "endpoint", "rpc_status", "response_code"})

DefaultRPCSendRequestMetric = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: DefaultRPCSendRequestMetricName,
Expand All @@ -29,7 +30,7 @@ var (
NativeHistogramMinResetDuration: 5 * time.Minute,
NativeHistogramMaxZeroThreshold: 0.05,
NativeHistogramMaxBucketNumber: 20,
}, []string{"sdk", "request_protocol", "endpoint", "status", "response_code"})
}, []string{"sdk", "request_protocol", "endpoint", "rpc_status", "response_code"})
)

func init() {
Expand Down
9 changes: 6 additions & 3 deletions metrics/gin/gin.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package gin

import (
"strconv"
"time"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
grpc "google.golang.org/grpc/codes"

"github.com/quwan-sre/observability-go-contrib/metrics/common"
"strconv"
"time"
)

func NewMetricsMiddleware() gin.HandlerFunc {
Expand Down Expand Up @@ -33,7 +36,7 @@ func NewMetricsMiddleware() gin.HandlerFunc {
"sdk": common.RPCSDKGin,
"request_protocol": common.RPCProtocolHTTP,
"endpoint": endpoint,
"status": common.RPCStatusSuccess,
"rpc_status": strconv.Itoa(int(grpc.OK)),
"response_code": strconv.Itoa(responseCode),
}).Observe(latency.Seconds())
return
Expand Down
Empty file added metrics/resty/README.md
Empty file.
61 changes: 61 additions & 0 deletions metrics/resty/resty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package resty

import (
"context"
"strconv"
"time"

"github.com/go-resty/resty/v2"
"github.com/prometheus/client_golang/prometheus"
grpc "google.golang.org/grpc/codes"

"github.com/quwan-sre/observability-go-contrib/metrics/common"
)

type metricsCtxKey struct{}

func NewMetricsMiddleware(c *resty.Client) {
if c == nil {
return
}

c.OnBeforeRequest(NewBeforeRequest())
c.OnAfterResponse(NewAfterResponse())

return
}

func NewBeforeRequest() func(c *resty.Client, r *resty.Request) error {
return func(c *resty.Client, r *resty.Request) error {
ctx := context.WithValue(r.Context(), metricsCtxKey{}, time.Now())
r.SetContext(ctx)
return nil
}
}

func NewAfterResponse() func(c *resty.Client, r *resty.Response) error {
return func(c *resty.Client, r *resty.Response) error {
req := r.Request
timeInterface := req.Context().Value(metricsCtxKey{})
t, ok := timeInterface.(time.Time)
if !ok {
// should never reach here
return nil
}

latency := time.Now().Sub(t)
endpoint := ""
if req.RawRequest != nil && req.RawRequest.URL != nil {
endpoint = req.RawRequest.URL.Path
}

common.DefaultRPCSendRequestMetric.With(prometheus.Labels{
"sdk": common.RPCSDKResty,
"request_protocol": common.RPCProtocolHTTP,
"endpoint": endpoint,
"rpc_status": strconv.Itoa(int(grpc.OK)),
"response_code": strconv.Itoa(r.StatusCode()),
}).Observe(latency.Seconds())
return nil
}
}
2 changes: 2 additions & 0 deletions test/benchmark/metrics/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package gin

import (
"fmt"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"

metrics "github.com/quwan-sre/observability-go-contrib/metrics/gin"
)

Expand Down
3 changes: 2 additions & 1 deletion test/benchmark/metrics/gin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package gin

import (
"fmt"
"github.com/go-resty/resty/v2"
"net/http"
"testing"
"time"

"github.com/go-resty/resty/v2"
)

var (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
package gin
package metrics

import (
"fmt"
"io"
"net/http"
"testing"
"time"
)

func TestMain(t *testing.M) {
// prepare a gin HTTP server
go main()

// health check
for {
resp, err := http.Get("http://127.0.0.1:8080/health")
if err != nil || resp.StatusCode != 200 {
time.Sleep(1 * time.Second)
} else {
break
}
}

// ready, run the test case
t.Run()
}

func TestExpose(t *testing.T) {
resp, err := http.Get("http://127.0.0.1:8080/metrics")
if err != nil || resp.StatusCode != 200 {
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/metrics/gin/main.go → test/e2e/metrics/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package gin
package metrics

import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"

metrics "github.com/quwan-sre/observability-go-contrib/metrics/gin"
)

Expand Down
25 changes: 25 additions & 0 deletions test/e2e/metrics/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package metrics

import (
"net/http"
"testing"
"time"
)

func TestMain(t *testing.M) {
// prepare a gin HTTP server
go main()

// health check
for {
resp, err := http.Get("http://127.0.0.1:8080/health")
if err != nil || resp.StatusCode != 200 {
time.Sleep(1 * time.Second)
} else {
break
}
}

// ready, run the test case
t.Run()
}
36 changes: 36 additions & 0 deletions test/e2e/metrics/resty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package metrics

import (
"fmt"
"github.com/go-resty/resty/v2"
metrics "github.com/quwan-sre/observability-go-contrib/metrics/resty"
"io"
"net/http"
"testing"
)

func TestRestyClient(t *testing.T) {
restyClient := resty.New()
metrics.NewMetricsMiddleware(restyClient)

testCases := []string{
"http://127.0.0.1:8080/health",
"http://127.0.0.1:8080/not_exist",
}

for _, tc := range testCases {
for i := 0; i < 10; i++ {
restyClient.R().Get(tc)
}
}

resp, err := http.Get("http://127.0.0.1:8080/metrics")
if err != nil || resp.StatusCode != 200 {
t.Fatalf("test failed, err: %v", err)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("test failed read body, err: %v", err)
}
fmt.Println(string(bodyBytes))
}

0 comments on commit b512e87

Please sign in to comment.