Documentation ¶
Index ¶
- Constants
- Variables
- func MustSetup(opts ...SetupOption) func()
- func SetPropagator(p TraceContextPropagator)
- func Setup(opts ...SetupOption) (shutdown func(), err error)
- func SetupDefault() (shutdown func(), err error)
- type Code
- type SetupOption
- func WithEnv(env string) SetupOption
- func WithHostname(hostname string) SetupOption
- func WithNamespace(namespace string) SetupOption
- func WithOtlpExporter(endpoint string) SetupOption
- func WithPodIP(podIP string) SetupOption
- func WithSampleRate(fraction float64) SetupOption
- func WithServerName(name string) SetupOption
- func WithServerVersion(version string) SetupOption
- type Span
- type SpanEventOption
- type SpanStartOption
- type TraceContext
- type TraceContextCarrier
- type TraceContextPropagator
Examples ¶
Constants ¶
const ( // SpanKindUnspecified is an unspecified SpanKind and is not a valid // SpanKind. SpanKindUnspecified should be replaced with SpanKindInternal // if it is received. SpanKindUnspecified spanKind = 0 // SpanKindInternal is a SpanKind for a Span that represents an internal // operation within an application. SpanKindInternal spanKind = 1 // SpanKindServer is a SpanKind for a Span that represents the operation // of handling a request from a client. SpanKindServer spanKind = 2 // SpanKindClient is a SpanKind for a Span that represents the operation // of client making a request to a server. SpanKindClient spanKind = 3 // SpanKindProducer is a SpanKind for a Span that represents the operation // of a producer sending a message to a message broker. Unlike // SpanKindClient and SpanKindServer, there is often no direct // relationship between this kind of Span and a SpanKindConsumer kind. A // SpanKindProducer Span will end once the message is accepted by the // message broker which might not overlap with the processing of that // message. SpanKindProducer spanKind = 4 // SpanKindConsumer is a SpanKind for a Span that represents the operation // of a consumer receiving a message from a message broker. Like // SpanKindProducer Spans, there is often no direct relationship between // this Span and the Span that produced the message. SpanKindConsumer spanKind = 5 )
const (
OTLP exporterEnum = "OTLP"
)
Variables ¶
Functions ¶
func MustSetup ¶
func MustSetup(opts ...SetupOption) func()
MustSetup same as Setup but panic if setup encounter any error.
func SetPropagator ¶
func SetPropagator(p TraceContextPropagator)
SetPropagator sets the trace context propagator. if it never called, the default propagator will be used.
func Setup ¶
func Setup(opts ...SetupOption) (shutdown func(), err error)
Setup would only execute once if it is called multiple times. Of course, if setup failed, it would return an error and allows the caller to retry. After setup, open telemetry's sdk has been initialized with TracerProvider and Propagator across processes.
func SetupDefault ¶
func SetupDefault() (shutdown func(), err error)
SetupDefault setup default tracing, using:
OLTP exporter with default endpoint: localhost:4317 or OTEL_COLLECTOR_ENDPOINT; serverName from environment variable: APP_ID, APP_NAME; version from environment variable: APP_VERSION; env from environment variable: RUN_ENV, DEPLOY_ENV; namespace from environment variable: NAMESPACE; sampleRate set 0.2, means 20% of traces will be sampled, or you can set OTEL_SAMPLE_RATE=[0..1.0];
Example ¶
package main import ( tracing "github.com/yeqown/opentelemetry-quake" ) func main() { name := "default_or_from_env" version := "1.0.0" env := "prod" ns := "my_namespace" otelCollectorEndpoint := "localhost:4317" shutdown, err := tracing.Setup( tracing.WithServerName(name), tracing.WithServerVersion(version), tracing.WithEnv(env), tracing.WithNamespace(ns), tracing.WithOtlpExporter(otelCollectorEndpoint), tracing.WithSampleRate(0.2), ) _, _ = shutdown, err }
Output:
Types ¶
type SetupOption ¶
type SetupOption interface {
// contains filtered or unexported methods
}
func WithEnv ¶
func WithEnv(env string) SetupOption
func WithHostname ¶
func WithHostname(hostname string) SetupOption
func WithNamespace ¶
func WithNamespace(namespace string) SetupOption
func WithOtlpExporter ¶
func WithOtlpExporter(endpoint string) SetupOption
func WithPodIP ¶
func WithPodIP(podIP string) SetupOption
func WithSampleRate ¶
func WithSampleRate(fraction float64) SetupOption
func WithServerName ¶
func WithServerName(name string) SetupOption
func WithServerVersion ¶
func WithServerVersion(version string) SetupOption
type Span ¶
type Span interface { // SpanContext returns the SpanContext of the span, including the // trace ID, span ID, and whether the span is a root span. SpanContext() *TraceContext // RecordError records a span event capture an error. And it always // records stacktrace. RecordError(err error, opts ...SpanEventOption) // SetTag sets a key value pair on the span. to be used for // compatibility with OpenTracing. SetTag(key string, value string) // SetAttributes sets a key value pair on the span. SetAttributes(attributes ...attribute.KeyValue) // LogFields sets a key value pair on the span. LogFields(event string, attributes ...attribute.KeyValue) // SetStatus sets the status of the span. OK, Error, etc. SetStatus(code Code, message string) // Finish ends the span. same as span.End() Finish() // End ends the span. same to Finish End() }
Span is a specification for internal tracing client to use.
func SpanFromContext ¶
SpanFromContext is alias of otel.SpanFromContext() to avoid importing otel library in you project code.
Example ¶
package main import ( "context" "fmt" tracing "github.com/yeqown/opentelemetry-quake" ) func main() { ctx, sp := tracing.StartSpan(context.Background(), "example") defer sp.End() internalCall := func(ctx context.Context) { // launch a RPC call sp2 := tracing.SpanFromContext(ctx) sp2.RecordError(fmt.Errorf("encounter an error in internalCall")) } // pass ctx to another internal call or remote call. internalCall(ctx) }
Output:
func StartSpan ¶
func StartSpan(ctx context.Context, operation string, opts ...SpanStartOption) (context.Context, Span)
StartSpan is alias of otel.Tracer("tracerName").Start() to avoid importing otel library in you project code.
Example ¶
package main import ( "context" tracing "github.com/yeqown/opentelemetry-quake" ) func main() { ctx, sp := tracing.StartSpan(context.Background(), "example") defer sp.End() remoteCall := func(ctx context.Context) { // launch a RPC call } // pass ctx to another internal call or remote call. remoteCall(ctx) }
Output:
type SpanEventOption ¶
type SpanEventOption interface {
// contains filtered or unexported methods
}
func WithStackTrace ¶
func WithStackTrace() SpanEventOption
type SpanStartOption ¶
type SpanStartOption interface {
// contains filtered or unexported methods
}
func WithSpanKind ¶
func WithSpanKind(kind spanKind) SpanStartOption
type TraceContext ¶
type TraceContext struct {
TraceID, SpanID, ParentSpanID string
// contains filtered or unexported fields
}
TraceContext span 的上下文信息,包含 span 的 traceID 和 spanID.
func SpanContextFromContext ¶
func SpanContextFromContext(ctx context.Context) *TraceContext
Example ¶
package main import ( "context" "fmt" tracing "github.com/yeqown/opentelemetry-quake" ) func main() { ctx, sp := tracing.StartSpan(context.Background(), "example") defer sp.End() remoteCall := func(ctx context.Context) { // launch a RPC call sc := tracing.SpanContextFromContext(ctx) fmt.Println(sc.TraceID) } // pass ctx to another internal call or remote call. remoteCall(ctx) }
Output:
func TraceContextFromContext ¶
func TraceContextFromContext(ctx context.Context) *TraceContext
TraceContextFromContext 从 ctx 中尝试获取 TraceContext 方便日志记录
Example ¶
package main import ( "context" "fmt" tracing "github.com/yeqown/opentelemetry-quake" ) func main() { ctx, sp := tracing.StartSpan(context.Background(), "example") defer sp.End() remoteCall := func(ctx context.Context) { // launch a RPC call tc := tracing.TraceContextFromContext(ctx) fmt.Println(tc.TraceID) } // pass ctx to another internal call or remote call. remoteCall(ctx) }
Output:
func (*TraceContext) IsRemote ¶
func (tc *TraceContext) IsRemote() bool
func (*TraceContext) IsValid ¶
func (tc *TraceContext) IsValid() bool
func (*TraceContext) Sampled ¶
func (tc *TraceContext) Sampled() bool
type TraceContextCarrier ¶
TraceContextCarrier is the type of the carrier to be used for propagating the trace context across processes.
func NewMapCarrier ¶
func NewMapCarrier() TraceContextCarrier
NewMapCarrier returns a new TraceContextCarrier that wraps a map[string]string.
type TraceContextPropagator ¶
type TraceContextPropagator interface { Inject(ctx context.Context, carrier TraceContextCarrier) Extract(ctx context.Context, carrier TraceContextCarrier) context.Context }
TraceContextPropagator is the type of the propagator to be used for propagating the trace context across processes.
func GetPropagator ¶
func GetPropagator() TraceContextPropagator
GetPropagator returns the trace context propagator.