Skip to content

Commit

Permalink
Merge pull request denverdino#481 from saberrey/feature/tracing_support
Browse files Browse the repository at this point in the history
sdk support tracing
  • Loading branch information
menglingwei authored May 16, 2022
2 parents 8160310 + 5631171 commit 1446ea6
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
50 changes: 50 additions & 0 deletions common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/opentracing/opentracing-go/ext"
"io/ioutil"
"log"
"net"
Expand All @@ -16,6 +17,7 @@ import (
"time"

"github.com/denverdino/aliyungo/util"
"github.com/opentracing/opentracing-go"
)

// RemovalPolicy.N add index to array item
Expand All @@ -39,6 +41,8 @@ type Client struct {
regionID Region
businessInfo string
userAgent string
disableTrace bool
span opentracing.Span
}

// Initialize properties of a client instance
Expand Down Expand Up @@ -292,6 +296,12 @@ func (client *Client) WithUserAgent(userAgent string) *Client {
return client
}

// WithUserAgent sets user agent to the request/response message
func (client *Client) WithDisableTrace(disableTrace bool) *Client {
client.SetDisableTrace(disableTrace)
return client
}

// ----------------------------------------------------
// SetXXX methods
// ----------------------------------------------------
Expand Down Expand Up @@ -362,6 +372,16 @@ func (client *Client) SetTransport(transport http.RoundTripper) {
client.httpClient.Transport = transport
}

// SetDisableTrace close trace mode
func (client *Client) SetDisableTrace(disableTrace bool) {
client.disableTrace = disableTrace
}

// SetSpan set the parent span
func (client *Client) SetSpan(span opentracing.Span) {
client.span = span
}

func (client *Client) initEndpoint() error {
// if set any value to "CUSTOMIZED_ENDPOINT" could skip location service.
// example: export CUSTOMIZED_ENDPOINT=true
Expand Down Expand Up @@ -417,10 +437,36 @@ func (client *Client) Invoke(action string, args interface{}, response interface

httpReq.Header.Set("User-Agent", httpReq.UserAgent()+" "+client.userAgent)

// Set tracer
var span opentracing.Span
if ok := opentracing.IsGlobalTracerRegistered(); ok && !client.disableTrace {
tracer := opentracing.GlobalTracer()
var rootCtx opentracing.SpanContext

if client.span != nil {
rootCtx = client.span.Context()
}

span = tracer.StartSpan(
"AliyunGO-"+request.Action,
opentracing.ChildOf(rootCtx),
opentracing.Tag{string(ext.Component), "AliyunGO"},
opentracing.Tag{"ActionName", request.Action})

defer span.Finish()
tracer.Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(httpReq.Header))
}

t0 := time.Now()
httpResp, err := client.httpClient.Do(httpReq)
t1 := time.Now()
if err != nil {
if span != nil {
ext.LogError(span, err)
}
return GetClientError(err)
}
statusCode := httpResp.StatusCode
Expand All @@ -429,6 +475,10 @@ func (client *Client) Invoke(action string, args interface{}, response interface
log.Printf("Invoke %s %s %d (%v)", ECSRequestMethod, requestURL, statusCode, t1.Sub(t0))
}

if span != nil {
ext.HTTPStatusCode.Set(span, uint16(httpResp.StatusCode))
}

defer httpResp.Body.Close()
body, err := ioutil.ReadAll(httpResp.Body)

Expand Down
57 changes: 57 additions & 0 deletions common/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package common

import (
"fmt"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
jaegerlog "github.com/uber/jaeger-client-go/log"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -93,3 +98,55 @@ func Test_InitClient4RegionalDomain(t *testing.T) {

}
}

func Test_InvokeTracer(t *testing.T) {
client := NewTestClientForDebug()
assert.NotNil(t, client)
args := &DescribeEndpointsArgs{
Id: Hangzhou,
ServiceCode: "ecs",
Type: "openAPI",
}
// not set global tracer
resp, err := client.DescribeEndpoints(args)
t.Log(resp)
assert.Nil(t, err)

//set global tracer, no root span
var cfg = jaegercfg.Configuration{
ServiceName: "client test",
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
},
}
jLogger := jaegerlog.StdLogger
tracer, closer, _ := cfg.NewTracer(
jaegercfg.Logger(jLogger),
)
opentracing.InitGlobalTracer(tracer)
resp, err = client.DescribeEndpoints(args)
t.Log(resp)
assert.Nil(t, err)

// set global tracer, with root span
parentSpan := tracer.StartSpan("root")
fmt.Println(parentSpan)
client.SetSpan(parentSpan)
resp, err = client.DescribeEndpoints(args)
t.Log(resp)
assert.Nil(t, err)

// set disable trace
client.SetDisableTrace(true)
client.SetSpan(parentSpan)
resp, err = client.DescribeEndpoints(args)
t.Log(resp)
assert.Nil(t, err)

parentSpan.Finish()
closer.Close()
}
2 changes: 1 addition & 1 deletion cs/node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type ScalingGroup struct {
SupportIPv6 bool `json:"support_ipv6"`
// deploymentset
DeploymentSetId string `json:"deploymentset_id"`
DesiredSize *int64 `json:"desired_size,omitempty"`
DesiredSize *int64 `json:"desired_size,omitempty"`
}

type AutoScaling struct {
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module github.com/denverdino/aliyungo

go 1.15

require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.uber.org/atomic v1.9.0 // indirect
)
25 changes: 25 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b/go.mod h1:AC62GU6hc0BrNm+9RK9VSiwa/EUe1bkIeFORAMcHvJU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 1446ea6

Please sign in to comment.