1// Unless explicitly stated otherwise all files in this repository are licensed 2// under the Apache License Version 2.0. 3// This product includes software developed at Datadog (https://www.datadoghq.com/). 4// Copyright 2016 Datadog, Inc. 5 6package buntdb 7 8import ( 9 "context" 10 "math" 11 12 "gopkg.in/DataDog/dd-trace-go.v1/internal" 13) 14 15type config struct { 16 ctx context.Context 17 serviceName string 18 analyticsRate float64 19} 20 21func defaults(cfg *config) { 22 cfg.serviceName = "buntdb" 23 cfg.ctx = context.Background() 24 // cfg.analyticsRate = globalconfig.AnalyticsRate() 25 if internal.BoolEnv("DD_TRACE_BUNTDB_ANALYTICS_ENABLED", false) { 26 cfg.analyticsRate = 1.0 27 } else { 28 cfg.analyticsRate = math.NaN() 29 } 30} 31 32// An Option customizes the config. 33type Option func(cfg *config) 34 35// WithContext sets the context for the transaction. 36func WithContext(ctx context.Context) Option { 37 return func(cfg *config) { 38 cfg.ctx = ctx 39 } 40} 41 42// WithServiceName sets the given service name for the transaction. 43func WithServiceName(serviceName string) Option { 44 return func(cfg *config) { 45 cfg.serviceName = serviceName 46 } 47} 48 49// WithAnalytics enables Trace Analytics for all started spans. 50func WithAnalytics(on bool) Option { 51 return func(cfg *config) { 52 if on { 53 cfg.analyticsRate = 1.0 54 } else { 55 cfg.analyticsRate = math.NaN() 56 } 57 } 58} 59 60// WithAnalyticsRate sets the sampling rate for Trace Analytics events 61// correlated to started spans. 62func WithAnalyticsRate(rate float64) Option { 63 return func(cfg *config) { 64 if rate >= 0.0 && rate <= 1.0 { 65 cfg.analyticsRate = rate 66 } else { 67 cfg.analyticsRate = math.NaN() 68 } 69 } 70} 71