SpanTimer returns a function used to record the duration of the given span. It supports both OpenCensus and OpenTelemetry spans
(spanInterface interface{}, name string)
| 988 | // SpanTimer returns a function used to record the duration of the given span. |
| 989 | // It supports both OpenCensus and OpenTelemetry spans |
| 990 | func SpanTimer(spanInterface interface{}, name string) func() { |
| 991 | // OpenCensus span case |
| 992 | if span, ok := spanInterface.(*trace.Span); ok && span != nil { |
| 993 | uniq := int64(rand.Int31()) //nolint:gosec // unique id for tracing does not require cryptographic precision |
| 994 | attrs := trace.WithAttributes( |
| 995 | attribute.Int64("funcId", uniq), |
| 996 | attribute.String("funcName", name), |
| 997 | ) |
| 998 | (*span).AddEvent("Start", attrs) |
| 999 | start := time.Now() |
| 1000 | |
| 1001 | return func() { |
| 1002 | (*span).AddEvent("End", trace.WithAttributes( |
| 1003 | attribute.Int64("funcId", uniq), |
| 1004 | attribute.String("funcName", name), |
| 1005 | attribute.String("duration", time.Since(start).String()), |
| 1006 | )) |
| 1007 | // TODO: We can look into doing a latency record here. |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | // OpenTelemetry span case |
| 1012 | if span, ok := spanInterface.(trace.Span); ok { |
| 1013 | uniq := int64(rand.Int31()) //nolint:gosec // unique id for tracing does not require cryptographic precision |
| 1014 | attrs := trace.WithAttributes( |
| 1015 | attribute.Int64("funcId", uniq), |
| 1016 | attribute.String("funcName", name), |
| 1017 | ) |
| 1018 | span.AddEvent("Start", attrs) |
| 1019 | start := time.Now() |
| 1020 | |
| 1021 | return func() { |
| 1022 | span.AddEvent("End", trace.WithAttributes( |
| 1023 | attribute.Int64("funcId", uniq), |
| 1024 | attribute.String("funcName", name), |
| 1025 | attribute.String("duration", time.Since(start).String()), |
| 1026 | )) |
| 1027 | // TODO: We can look into doing a latency record here. |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | // Fall back for nil or unknown span type |
| 1032 | return func() {} |
| 1033 | } |
| 1034 | |
| 1035 | // CloseFunc needs to be called to close all the client connections. |
| 1036 | type CloseFunc func() |
no test coverage detected
searching dependent graphs…