Skip to content

Commit

Permalink
feat: support opentracing
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Oct 17, 2023
1 parent 2b0e131 commit c9d4d2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/json-iterator/go v1.1.12
github.com/modern-go/reflect2 v1.0.2
golang.org/x/net v0.11.0
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b
)
32 changes: 32 additions & 0 deletions tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/alibabacloud-go/debug/debug"
"github.com/alibabacloud-go/tea/utils"

opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"golang.org/x/net/proxy"
)

Expand Down Expand Up @@ -97,6 +99,8 @@ type RuntimeObject struct {
Listener utils.ProgressListener `json:"listener" xml:"listener"`
Tracker *utils.ReaderTracker `json:"tracker" xml:"tracker"`
Logger *utils.Logger `json:"logger" xml:"logger"`
Span opentracing.Span `json:"span" xml:"span"`
IsCloseTrace *bool `json:"isCloseTrace" xml:"isCloseTrace"`
}

type teaClient struct {
Expand Down Expand Up @@ -133,6 +137,7 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
Key: TransInterfaceToString(runtime["key"]),
Cert: TransInterfaceToString(runtime["cert"]),
CA: TransInterfaceToString(runtime["ca"]),
IsCloseTrace: TransInterfaceToBool(runtime["isCloseTrace"]),
}
if runtime["listener"] != nil {
runtimeObject.Listener = runtime["listener"].(utils.ProgressListener)
Expand All @@ -143,6 +148,9 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
if runtime["logger"] != nil {
runtimeObject.Logger = runtime["logger"].(*utils.Logger)
}
if runtime["span"] != nil {
runtimeObject.Span = runtime["span"].(opentracing.Span)
}
return runtimeObject
}

Expand Down Expand Up @@ -378,6 +386,30 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
event := utils.NewProgressEvent(utils.TransferStartedEvent, 0, int64(contentlength), 0)
utils.PublishProgress(runtimeObject.Listener, event)

// Set tracer
var span opentracing.Span
if ok := opentracing.IsGlobalTracerRegistered(); ok && BoolValue(runtimeObject.IsCloseTrace) != true {
tracer := opentracing.GlobalTracer()
var rootCtx opentracing.SpanContext
var rootSpan opentracing.Span

if rootSpan = runtimeObject.Span; rootSpan != nil {
rootCtx = rootSpan.Context()
}

span = tracer.StartSpan(
httpRequest.URL.RequestURI(),
opentracing.ChildOf(rootCtx),
opentracing.Tag{Key: string(ext.Component), Value: "aliyunApi"},
opentracing.Tag{Key: "request", Value: requestURL})

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

putMsgToMap(fieldMap, httpRequest)
startTime := time.Now()
fieldMap["{start_time}"] = startTime.Format("2006-01-02 15:04:05")
Expand Down

0 comments on commit c9d4d2c

Please sign in to comment.