gocommon/tracing/tracing.go
Matthieu 'JP' DERASSE 06f0722cdb
All checks were successful
continuous-integration/drone/push Build is passing
feat(tracing): Start to implement tracing system
2022-12-14 17:09:05 +00:00

82 lines
1.8 KiB
Go

package tracing
import (
"github.com/juju/errors"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)
// Init will try to initialize tracing by trying to retrieve config from multiple source.
func Init() (*sdktrace.TracerProvider, error) {
// loading configuration
c, err := loadConfig()
if err != nil {
return nil, errors.Trace(err)
}
return initFromSource(c)
}
// InitFromCustomVaultSecret will initialize tracing with a vault secret.
func InitFromCustomVaultSecret(secret string) (*sdktrace.TracerProvider, error) {
c, err := loadConfigFromVault(secret)
if err != nil {
return nil, errors.Trace(err)
}
return initFromSource(c)
}
// InitFromCustomFile will initialize tracing with a config file.
func InitFromCustomFile(path string) (*sdktrace.TracerProvider, error) {
c, err := loadConfigFromFile(path)
if err != nil {
return nil, errors.Trace(err)
}
return initFromSource(c)
}
func initFromSource(c *ConfigStruct) (*sdktrace.TracerProvider, error) {
err := c.applyEnv()
if err != nil {
return nil, errors.Trace(err)
}
return InitFromCustomConfig(c)
}
// InitFromCustomConfig will initialize tracing from a gaven config.
func InitFromCustomConfig(c *ConfigStruct) (*sdktrace.TracerProvider, error) {
err := c.IsValid()
if err != nil {
return nil, errors.Trace(err)
}
// Ensure default SDK resources and the required service name are set.
r, err := resource.Merge(
resource.Default(),
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("myService"),
semconv.ServiceVersionKey.String("1.0.0"),
semconv.ServiceInstanceIDKey.String("abcdef12345"),
),
)
if err != nil {
panic(err)
}
return sdktrace.NewTracerProvider(
sdktrace.WithResource(r),
), nil
}