Skip to content

Commit f03ad29

Browse files
committed
apis/projectcontour/v1alpha1: add client and random sampling config for tracing
Add ClientSampling and RandomSampling fields to TracingConfig to allow configurable client_sampling and random_sampling in Envoy tracing. Updates #7271 Signed-off-by: Muxian Wu <muxianw@twitter.com>
1 parent 00812a4 commit f03ad29

21 files changed

Lines changed: 356 additions & 21 deletions

File tree

apis/projectcontour/v1alpha1/contourconfig.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,16 @@ type TracingConfig struct {
816816
// +optional
817817
OverallSampling *string `json:"overallSampling,omitempty"`
818818

819+
// ClientSampling defines the sampling rate when x-client-trace-id header is set.
820+
// contour's default is 100.
821+
// +optional
822+
ClientSampling *string `json:"clientSampling,omitempty"`
823+
824+
// RandomSampling defines the random sampling rate for all requests.
825+
// contour's default is 100.
826+
// +optional
827+
RandomSampling *string `json:"randomSampling,omitempty"`
828+
819829
// MaxPathTagLength defines maximum length of the request path
820830
// to extract and include in the HttpUrl tag.
821831
// contour's default is 256.

apis/projectcontour/v1alpha1/contourconfig_helpers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ func (t *TracingConfig) Validate() error {
6464
}
6565
}
6666

67+
if t.ClientSampling != nil {
68+
_, err := strconv.ParseFloat(*t.ClientSampling, 64)
69+
if err != nil {
70+
return fmt.Errorf("invalid tracing client sampling: %v", err)
71+
}
72+
}
73+
74+
if t.RandomSampling != nil {
75+
_, err := strconv.ParseFloat(*t.RandomSampling, 64)
76+
if err != nil {
77+
return fmt.Errorf("invalid tracing random sampling: %v", err)
78+
}
79+
}
80+
6781
var customTagNames []string
6882

6983
for _, customTag := range t.CustomTags {

apis/projectcontour/v1alpha1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Allow configurable client_sampling and random_sampling for tracing
2+
3+
Added support for configuring client_sampling and random_sampling in Contour's tracing configuration, allowing more granular control over trace sampling rates as per Envoy's tracing documentation.

cmd/contour/serve.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,19 @@ func (s *Server) getExtensionSvcConfig(name, namespace string) (xdscache_v3.Exte
748748
return extensionSvcConfig, nil
749749
}
750750

751+
// parseSamplingRate parses a sampling rate string and returns the float value,
752+
// defaulting to 100.0 for invalid values or zero values.
753+
func parseSamplingRate(rateStr *string) float64 {
754+
if rateStr == nil {
755+
return 100.0
756+
}
757+
rate, err := strconv.ParseFloat(*rateStr, 64)
758+
if err != nil || rate == 0 {
759+
return 100.0
760+
}
761+
return rate
762+
}
763+
751764
func (s *Server) setupTracingService(tracingConfig *contour_v1alpha1.TracingConfig) (*xdscache_v3.TracingConfig, error) {
752765
if tracingConfig == nil {
753766
return nil, nil
@@ -779,15 +792,16 @@ func (s *Server) setupTracingService(tracingConfig *contour_v1alpha1.TracingConf
779792
})
780793
}
781794

782-
overallSampling, err := strconv.ParseFloat(ptr.Deref(tracingConfig.OverallSampling, "100"), 64)
783-
if err != nil || overallSampling == 0 {
784-
overallSampling = 100.0
785-
}
795+
overallSampling := parseSamplingRate(tracingConfig.OverallSampling)
796+
clientSampling := parseSamplingRate(tracingConfig.ClientSampling)
797+
randomSampling := parseSamplingRate(tracingConfig.RandomSampling)
786798

787799
return &xdscache_v3.TracingConfig{
788800
ServiceName: ptr.Deref(tracingConfig.ServiceName, "contour"),
789801
ExtensionServiceConfig: extensionSvcConfig,
790802
OverallSampling: overallSampling,
803+
ClientSampling: clientSampling,
804+
RandomSampling: randomSampling,
791805
MaxPathTagLength: ptr.Deref(tracingConfig.MaxPathTagLength, 256),
792806
CustomTags: customTags,
793807
}, nil

cmd/contour/serve_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,57 @@ func TestGetDAGBuilder(t *testing.T) {
218218
// TODO(3453): test additional properties of the DAG builder (processor fields, cache fields, Gateway tests (requires a client fake))
219219
}
220220

221+
func TestParseSamplingRate(t *testing.T) {
222+
tests := map[string]struct {
223+
input *string
224+
want float64
225+
}{
226+
"nil input": {
227+
input: nil,
228+
want: 100.0,
229+
},
230+
"empty string": {
231+
input: ptr.To(""),
232+
want: 100.0,
233+
},
234+
"valid number": {
235+
input: ptr.To("50.5"),
236+
want: 50.5,
237+
},
238+
"zero value": {
239+
input: ptr.To("0"),
240+
want: 100.0,
241+
},
242+
"negative number": {
243+
input: ptr.To("-10"),
244+
want: -10.0,
245+
},
246+
"invalid string": {
247+
input: ptr.To("invalid"),
248+
want: 100.0,
249+
},
250+
"non-numeric string": {
251+
input: ptr.To("not-a-number"),
252+
want: 100.0,
253+
},
254+
"decimal zero": {
255+
input: ptr.To("0.0"),
256+
want: 100.0,
257+
},
258+
"large number": {
259+
input: ptr.To("999.99"),
260+
want: 999.99,
261+
},
262+
}
263+
264+
for name, tc := range tests {
265+
t.Run(name, func(t *testing.T) {
266+
got := parseSamplingRate(tc.input)
267+
assert.InDelta(t, tc.want, got, 0.001)
268+
})
269+
}
270+
}
271+
221272
func mustGetGatewayAPIProcessor(t *testing.T, builder *dag.Builder) *dag.GatewayAPIProcessor {
222273
t.Helper()
223274
for i := range builder.Processors {

cmd/contour/servecontext.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ func (ctx *serveContext) convertToContourConfigurationSpec() contour_v1alpha1.Co
413413
IncludePodDetail: ctx.Config.Tracing.IncludePodDetail,
414414
ServiceName: ctx.Config.Tracing.ServiceName,
415415
OverallSampling: ctx.Config.Tracing.OverallSampling,
416+
ClientSampling: ctx.Config.Tracing.ClientSampling,
417+
RandomSampling: ctx.Config.Tracing.RandomSampling,
416418
MaxPathTagLength: ctx.Config.Tracing.MaxPathTagLength,
417419
CustomTags: customTags,
418420
ExtensionService: &contour_v1alpha1.NamespacedName{

cmd/contour/servecontext_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ func TestConvertServeContext(t *testing.T) {
833833
IncludePodDetail: ptr.To(false),
834834
ServiceName: ptr.To("contour"),
835835
OverallSampling: ptr.To("100"),
836+
ClientSampling: ptr.To("100"),
837+
RandomSampling: ptr.To("100"),
836838
MaxPathTagLength: ptr.To(uint32(256)),
837839
CustomTags: []config.CustomTag{
838840
{
@@ -853,6 +855,8 @@ func TestConvertServeContext(t *testing.T) {
853855
IncludePodDetail: ptr.To(false),
854856
ServiceName: ptr.To("contour"),
855857
OverallSampling: ptr.To("100"),
858+
ClientSampling: ptr.To("100"),
859+
RandomSampling: ptr.To("100"),
856860
MaxPathTagLength: ptr.To(uint32(256)),
857861
CustomTags: []*contour_v1alpha1.CustomTag{
858862
{

examples/contour/01-crds.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,11 @@ spec:
10931093
description: Tracing defines properties for exporting trace data to
10941094
OpenTelemetry.
10951095
properties:
1096+
clientSampling:
1097+
description: |-
1098+
ClientSampling defines the sampling rate when x-client-trace-id header is set.
1099+
contour's default is 100.
1100+
type: string
10961101
customTags:
10971102
description: CustomTags defines a list of custom tags with unique
10981103
tag name.
@@ -1150,6 +1155,11 @@ spec:
11501155
OverallSampling defines the sampling rate of trace data.
11511156
contour's default is 100.
11521157
type: string
1158+
randomSampling:
1159+
description: |-
1160+
RandomSampling defines the random sampling rate for all requests.
1161+
contour's default is 100.
1162+
type: string
11531163
serviceName:
11541164
description: |-
11551165
ServiceName defines the name for the service.
@@ -5042,6 +5052,11 @@ spec:
50425052
description: Tracing defines properties for exporting trace data
50435053
to OpenTelemetry.
50445054
properties:
5055+
clientSampling:
5056+
description: |-
5057+
ClientSampling defines the sampling rate when x-client-trace-id header is set.
5058+
contour's default is 100.
5059+
type: string
50455060
customTags:
50465061
description: CustomTags defines a list of custom tags with
50475062
unique tag name.
@@ -5100,6 +5115,11 @@ spec:
51005115
OverallSampling defines the sampling rate of trace data.
51015116
contour's default is 100.
51025117
type: string
5118+
randomSampling:
5119+
description: |-
5120+
RandomSampling defines the random sampling rate for all requests.
5121+
contour's default is 100.
5122+
type: string
51035123
serviceName:
51045124
description: |-
51055125
ServiceName defines the name for the service.

examples/render/contour-deployment.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,11 @@ spec:
13121312
description: Tracing defines properties for exporting trace data to
13131313
OpenTelemetry.
13141314
properties:
1315+
clientSampling:
1316+
description: |-
1317+
ClientSampling defines the sampling rate when x-client-trace-id header is set.
1318+
contour's default is 100.
1319+
type: string
13151320
customTags:
13161321
description: CustomTags defines a list of custom tags with unique
13171322
tag name.
@@ -1369,6 +1374,11 @@ spec:
13691374
OverallSampling defines the sampling rate of trace data.
13701375
contour's default is 100.
13711376
type: string
1377+
randomSampling:
1378+
description: |-
1379+
RandomSampling defines the random sampling rate for all requests.
1380+
contour's default is 100.
1381+
type: string
13721382
serviceName:
13731383
description: |-
13741384
ServiceName defines the name for the service.
@@ -5261,6 +5271,11 @@ spec:
52615271
description: Tracing defines properties for exporting trace data
52625272
to OpenTelemetry.
52635273
properties:
5274+
clientSampling:
5275+
description: |-
5276+
ClientSampling defines the sampling rate when x-client-trace-id header is set.
5277+
contour's default is 100.
5278+
type: string
52645279
customTags:
52655280
description: CustomTags defines a list of custom tags with
52665281
unique tag name.
@@ -5319,6 +5334,11 @@ spec:
53195334
OverallSampling defines the sampling rate of trace data.
53205335
contour's default is 100.
53215336
type: string
5337+
randomSampling:
5338+
description: |-
5339+
RandomSampling defines the random sampling rate for all requests.
5340+
contour's default is 100.
5341+
type: string
53225342
serviceName:
53235343
description: |-
53245344
ServiceName defines the name for the service.

0 commit comments

Comments
 (0)