-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [resty] Added resty instrument (#3)
* 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
Showing
15 changed files
with
172 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 1 addition & 20 deletions
21
test/e2e/metrics/gin/main_test.go → test/e2e/metrics/gin_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |