@@ -13,6 +13,7 @@ import (
13
13
"encoding/json"
14
14
"fmt"
15
15
"os"
16
+ "regexp"
16
17
"strings"
17
18
18
19
"github.com/DataDog/datadog-lambda-go/internal/extension"
@@ -91,6 +92,58 @@ func (l *Listener) HandlerStarted(ctx context.Context, msg json.RawMessage) cont
91
92
tracer .WithGlobalTag ("_dd.origin" , "lambda" ),
92
93
tracer .WithSendRetries (2 ),
93
94
}, l .tracerOptions ... )
95
+
96
+ // Begin parsing DD_TRACE_SAMPLING_RULES
97
+ if samplingRulesEnv := os .Getenv ("DD_TRACE_SAMPLING_RULES" ); samplingRulesEnv != "" {
98
+ var rawRules []struct {
99
+ Service string `json:"service,omitempty"`
100
+ Name string `json:"name,omitempty"`
101
+ SampleRate float64 `json:"sample_rate"`
102
+ }
103
+ err := json .Unmarshal ([]byte (samplingRulesEnv ), & rawRules )
104
+ if err != nil {
105
+ logger .Error (fmt .Errorf ("Error parsing sampling rules from DD_TRACE_SAMPLING_RULES: %v" , err ))
106
+ } else {
107
+ var samplingRules []tracer.SamplingRule
108
+ for _ , rule := range rawRules {
109
+ // Validate the sample rate
110
+ if rule .SampleRate < 0 || rule .SampleRate > 1 {
111
+ logger .Error (fmt .Errorf ("Invalid sample rate %v in sampling rules" , rule .SampleRate ))
112
+ continue
113
+ }
114
+
115
+ // Compile regex patterns for Service and Name if provided
116
+ var serviceRegex , nameRegex * regexp.Regexp
117
+ if rule .Service != "" {
118
+ serviceRegex , err = regexp .Compile (rule .Service )
119
+ if err != nil {
120
+ logger .Error (fmt .Errorf ("Invalid service regex %v: %v" , rule .Service , err ))
121
+ continue
122
+ }
123
+ }
124
+ if rule .Name != "" {
125
+ nameRegex , err = regexp .Compile (rule .Name )
126
+ if err != nil {
127
+ logger .Error (fmt .Errorf ("Invalid name regex %v: %v" , rule .Name , err ))
128
+ continue
129
+ }
130
+ }
131
+
132
+ // Create the sampling rule
133
+ samplingRule := tracer.SamplingRule {
134
+ Service : serviceRegex ,
135
+ Name : nameRegex ,
136
+ Rate : rule .SampleRate ,
137
+ }
138
+ samplingRules = append (samplingRules , samplingRule )
139
+ }
140
+ if len (samplingRules ) > 0 {
141
+ opts = append (opts , tracer .WithSamplingRules (samplingRules ... ))
142
+ }
143
+ }
144
+ }
145
+ // End parsing DD_TRACE_SAMPLING_RULES
146
+
94
147
if l .otelTracerEnabled {
95
148
provider := ddotel .NewTracerProvider (
96
149
opts ... ,
0 commit comments