Skip to content

Commit

Permalink
[tracer] update log
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed Jan 25, 2021
1 parent c9e71b6 commit 05746a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion candihelper/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

const (
// Version of this library
Version = "v1.3.5"
Version = "v1.3.6"
// TimeZoneAsia constanta
TimeZoneAsia = "Asia/Jakarta"
// TokenClaimKey const
Expand Down
4 changes: 2 additions & 2 deletions candiutils/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (request *httpRequestImpl) Do(ctx context.Context, method, url string, requ
trace.SetTag("http.method", req.Method)
trace.SetTag("http.url", req.URL.String())
if requestBody != nil {
tracer.LogKV(ctx, "request.body", requestBody)
tracer.Log(ctx, "request.body", requestBody)
}

// client request
Expand All @@ -153,7 +153,7 @@ func (request *httpRequestImpl) Do(ctx context.Context, method, url string, requ
trace.SetTag("response.code", resp.StatusCode)
trace.SetTag("response.status", resp.Status)
trace.SetTag("response.header", resp.Header)
tracer.LogKV(ctx, "response.body", respBody)
tracer.Log(ctx, "response.body", respBody)

if request.minHTTPErrorCodeThreshold != 0 && resp.StatusCode >= request.minHTTPErrorCodeThreshold {
err = errors.New(resp.Status)
Expand Down
30 changes: 13 additions & 17 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,17 @@ func (t *tracerImpl) Finish(additionalTags ...map[string]interface{}) {
}

// Log trace
func Log(ctx context.Context, event string, payload ...interface{}) {
func Log(ctx context.Context, key string, value interface{}) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}

span.LogKV(key, toString(value))
}

// LogEvent trace
func LogEvent(ctx context.Context, event string, payload ...interface{}) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
Expand All @@ -165,22 +175,6 @@ func Log(ctx context.Context, event string, payload ...interface{}) {
}
}

// LogKV trace
func LogKV(ctx context.Context, kv ...interface{}) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return
}

for i, p := range kv {
if e, ok := p.(error); ok && e != nil {
ext.Error.Set(span, true)
}
kv[i] = toString(p)
}
span.LogKV(kv...)
}

// WithTraceFunc functional with context and tags in function params
func WithTraceFunc(ctx context.Context, operationName string, fn func(context.Context, map[string]interface{})) {
t := StartTrace(ctx, operationName)
Expand All @@ -207,6 +201,8 @@ func toString(v interface{}) (s string) {
s = val
case int:
s = strconv.Itoa(val)
case []byte:
s = string(val)
default:
b, _ := json.Marshal(val)
s = string(b)
Expand Down

0 comments on commit 05746a1

Please sign in to comment.