package observ
import (
"context"
"fmt"
"sync"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk"
"go.opentelemetry.io/otel/sdk/internal/x"
semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv"
)
var measureAttrsPool = sync .Pool {
New : func () any {
const n = 1 + 1 + 1
s := make ([]attribute .KeyValue , 0 , n )
return &s
},
}
type SSP struct {
spansProcessedCounter metric .Int64Counter
addOpts []metric .AddOption
attrs []attribute .KeyValue
}
func SSPComponentName (id int64 ) attribute .KeyValue {
t := otelconv .ComponentTypeSimpleSpanProcessor
name := fmt .Sprintf ("%s/%d" , t , id )
return semconv .OTelComponentName (name )
}
func NewSSP (id int64 ) (*SSP , error ) {
if !x .Observability .Enabled () {
return nil , nil
}
meter := otel .GetMeterProvider ().Meter (
ScopeName ,
metric .WithInstrumentationVersion (sdk .Version ()),
metric .WithSchemaURL (SchemaURL ),
)
spansProcessedCounter , err := otelconv .NewSDKProcessorSpanProcessed (meter )
if err != nil {
err = fmt .Errorf ("failed to create SSP processed spans metric: %w" , err )
}
componentName := SSPComponentName (id )
componentType := spansProcessedCounter .AttrComponentType (otelconv .ComponentTypeSimpleSpanProcessor )
attrs := []attribute .KeyValue {componentName , componentType }
addOpts := []metric .AddOption {metric .WithAttributeSet (attribute .NewSet (attrs ...))}
return &SSP {
spansProcessedCounter : spansProcessedCounter .Inst (),
addOpts : addOpts ,
attrs : attrs ,
}, err
}
func (ssp *SSP ) SpanProcessed (ctx context .Context , err error ) {
ssp .spansProcessedCounter .Add (ctx , 1 , ssp .addOption (err )...)
}
func (ssp *SSP ) addOption (err error ) []metric .AddOption {
if err == nil {
return ssp .addOpts
}
attrs := measureAttrsPool .Get ().(*[]attribute .KeyValue )
defer func () {
*attrs = (*attrs )[:0 ]
measureAttrsPool .Put (attrs )
}()
*attrs = append (*attrs , ssp .attrs ...)
*attrs = append (*attrs , semconv .ErrorType (err ))
return []metric .AddOption {metric .WithAttributeSet (attribute .NewSet (*attrs ...))}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .