-
Notifications
You must be signed in to change notification settings - Fork 3
/
span.go
32 lines (29 loc) · 1 KB
/
span.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package otelKit
import (
"context"
"github.com/richelieu-yang/chimera/v3/src/core/interfaceKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"go.opentelemetry.io/otel/trace"
)
// NewSpan
/*
@param tracer 不能为nil
@param parentCtx 父span的Context(没有的话,可以使用 context.TODO())
@param spanName span的名称(UI页面中的Operation)
@param opts e.g. UI页面中的Tags
trace.WithAttributes(attribute.String("222", "222"))
@return err == nil的情况下,spanCtx 可以作为子span的传参parentCtx
*/
func NewSpan(tracer trace.Tracer, parentSpanCtx context.Context, spanName string, opts ...trace.SpanStartOption) (spanCtx context.Context, span trace.Span, err error) {
if err = interfaceKit.AssertNotNil(tracer, "tracer"); err != nil {
return
}
if parentSpanCtx == nil {
parentSpanCtx = context.TODO()
}
if err = strKit.AssertNotBlank(spanName, "spanName"); err != nil {
return
}
spanCtx, span = tracer.Start(parentSpanCtx, spanName, opts...)
return
}