Skip to content

Commit b708de3

Browse files
committed
test changes
1 parent 5bc9390 commit b708de3

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

internal/trace/listener.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"encoding/json"
1414
"fmt"
1515
"os"
16+
"regexp"
1617
"strings"
1718

1819
"github.com/DataDog/datadog-lambda-go/internal/extension"
@@ -91,6 +92,58 @@ func (l *Listener) HandlerStarted(ctx context.Context, msg json.RawMessage) cont
9192
tracer.WithGlobalTag("_dd.origin", "lambda"),
9293
tracer.WithSendRetries(2),
9394
}, 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+
94147
if l.otelTracerEnabled {
95148
provider := ddotel.NewTracerProvider(
96149
opts...,

0 commit comments

Comments
 (0)