Skip to content

Commit 3c50bdd

Browse files
authored
feat: support client and overall tracing sampling (#9402)
* feat: support client and overall tracing sampling Signed-off-by: Zakhar Zakharov <zakhar.zakharov.zz16@gmail.com>
1 parent a094082 commit 3c50bdd

40 files changed

Lines changed: 1000 additions & 78 deletions

api/v1alpha1/shared_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,18 @@ type Tracing struct {
947947
//
948948
// +optional
949949
SamplingFraction *gwapiv1.Fraction `json:"samplingFraction,omitempty"`
950+
// ClientSamplingFraction represents the fraction of requests that should be
951+
// selected for tracing when requested by the client.
952+
// If unspecified, client-forced tracing is disabled by default and users must
953+
// set this field to opt in.
954+
//
955+
// +optional
956+
ClientSamplingFraction *gwapiv1.Fraction `json:"clientSamplingFraction,omitempty"`
957+
// OverallSamplingFraction represents the fraction of requests that should be
958+
// selected for tracing after all other sampling checks have been applied.
959+
//
960+
// +optional
961+
OverallSamplingFraction *gwapiv1.Fraction `json:"overallSamplingFraction,omitempty"`
950962
// CustomTags defines the custom tags to add to each span.
951963
// If provider is kubernetes, pod name and namespace are added by default.
952964
//

api/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.

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,28 @@ spec:
31523152
31533153
This takes precedence over EnvoyProxy tracing when set.
31543154
properties:
3155+
clientSamplingFraction:
3156+
description: |-
3157+
ClientSamplingFraction represents the fraction of requests that should be
3158+
selected for tracing when requested by the client.
3159+
If unspecified, client-forced tracing is disabled by default and users must
3160+
set this field to opt in.
3161+
properties:
3162+
denominator:
3163+
default: 100
3164+
format: int32
3165+
minimum: 1
3166+
type: integer
3167+
numerator:
3168+
format: int32
3169+
minimum: 0
3170+
type: integer
3171+
required:
3172+
- numerator
3173+
type: object
3174+
x-kubernetes-validations:
3175+
- message: numerator must be less than or equal to denominator
3176+
rule: self.numerator <= self.denominator
31553177
customTags:
31563178
additionalProperties:
31573179
properties:
@@ -3216,6 +3238,26 @@ spec:
32163238
32173239
Deprecated: Use Tags instead.
32183240
type: object
3241+
overallSamplingFraction:
3242+
description: |-
3243+
OverallSamplingFraction represents the fraction of requests that should be
3244+
selected for tracing after all other sampling checks have been applied.
3245+
properties:
3246+
denominator:
3247+
default: 100
3248+
format: int32
3249+
minimum: 1
3250+
type: integer
3251+
numerator:
3252+
format: int32
3253+
minimum: 0
3254+
type: integer
3255+
required:
3256+
- numerator
3257+
type: object
3258+
x-kubernetes-validations:
3259+
- message: numerator must be less than or equal to denominator
3260+
rule: self.numerator <= self.denominator
32193261
samplingFraction:
32203262
description: |-
32213263
SamplingFraction represents the fraction of requests that should be

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_envoyproxies.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16329,6 +16329,28 @@ spec:
1632916329
Tracing defines tracing configuration for managed proxies.
1633016330
If unspecified, will not send tracing data.
1633116331
properties:
16332+
clientSamplingFraction:
16333+
description: |-
16334+
ClientSamplingFraction represents the fraction of requests that should be
16335+
selected for tracing when requested by the client.
16336+
If unspecified, client-forced tracing is disabled by default and users must
16337+
set this field to opt in.
16338+
properties:
16339+
denominator:
16340+
default: 100
16341+
format: int32
16342+
minimum: 1
16343+
type: integer
16344+
numerator:
16345+
format: int32
16346+
minimum: 0
16347+
type: integer
16348+
required:
16349+
- numerator
16350+
type: object
16351+
x-kubernetes-validations:
16352+
- message: numerator must be less than or equal to denominator
16353+
rule: self.numerator <= self.denominator
1633216354
customTags:
1633316355
additionalProperties:
1633416356
properties:
@@ -16393,6 +16415,26 @@ spec:
1639316415

1639416416
Deprecated: Use Tags instead.
1639516417
type: object
16418+
overallSamplingFraction:
16419+
description: |-
16420+
OverallSamplingFraction represents the fraction of requests that should be
16421+
selected for tracing after all other sampling checks have been applied.
16422+
properties:
16423+
denominator:
16424+
default: 100
16425+
format: int32
16426+
minimum: 1
16427+
type: integer
16428+
numerator:
16429+
format: int32
16430+
minimum: 0
16431+
type: integer
16432+
required:
16433+
- numerator
16434+
type: object
16435+
x-kubernetes-validations:
16436+
- message: numerator must be less than or equal to denominator
16437+
rule: self.numerator <= self.denominator
1639616438
provider:
1639716439
description: Provider defines the tracing provider.
1639816440
properties:

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,28 @@ spec:
31513151
31523152
This takes precedence over EnvoyProxy tracing when set.
31533153
properties:
3154+
clientSamplingFraction:
3155+
description: |-
3156+
ClientSamplingFraction represents the fraction of requests that should be
3157+
selected for tracing when requested by the client.
3158+
If unspecified, client-forced tracing is disabled by default and users must
3159+
set this field to opt in.
3160+
properties:
3161+
denominator:
3162+
default: 100
3163+
format: int32
3164+
minimum: 1
3165+
type: integer
3166+
numerator:
3167+
format: int32
3168+
minimum: 0
3169+
type: integer
3170+
required:
3171+
- numerator
3172+
type: object
3173+
x-kubernetes-validations:
3174+
- message: numerator must be less than or equal to denominator
3175+
rule: self.numerator <= self.denominator
31543176
customTags:
31553177
additionalProperties:
31563178
properties:
@@ -3215,6 +3237,26 @@ spec:
32153237
32163238
Deprecated: Use Tags instead.
32173239
type: object
3240+
overallSamplingFraction:
3241+
description: |-
3242+
OverallSamplingFraction represents the fraction of requests that should be
3243+
selected for tracing after all other sampling checks have been applied.
3244+
properties:
3245+
denominator:
3246+
default: 100
3247+
format: int32
3248+
minimum: 1
3249+
type: integer
3250+
numerator:
3251+
format: int32
3252+
minimum: 0
3253+
type: integer
3254+
required:
3255+
- numerator
3256+
type: object
3257+
x-kubernetes-validations:
3258+
- message: numerator must be less than or equal to denominator
3259+
rule: self.numerator <= self.denominator
32183260
samplingFraction:
32193261
description: |-
32203262
SamplingFraction represents the fraction of requests that should be

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_envoyproxies.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16328,6 +16328,28 @@ spec:
1632816328
Tracing defines tracing configuration for managed proxies.
1632916329
If unspecified, will not send tracing data.
1633016330
properties:
16331+
clientSamplingFraction:
16332+
description: |-
16333+
ClientSamplingFraction represents the fraction of requests that should be
16334+
selected for tracing when requested by the client.
16335+
If unspecified, client-forced tracing is disabled by default and users must
16336+
set this field to opt in.
16337+
properties:
16338+
denominator:
16339+
default: 100
16340+
format: int32
16341+
minimum: 1
16342+
type: integer
16343+
numerator:
16344+
format: int32
16345+
minimum: 0
16346+
type: integer
16347+
required:
16348+
- numerator
16349+
type: object
16350+
x-kubernetes-validations:
16351+
- message: numerator must be less than or equal to denominator
16352+
rule: self.numerator <= self.denominator
1633116353
customTags:
1633216354
additionalProperties:
1633316355
properties:
@@ -16392,6 +16414,26 @@ spec:
1639216414

1639316415
Deprecated: Use Tags instead.
1639416416
type: object
16417+
overallSamplingFraction:
16418+
description: |-
16419+
OverallSamplingFraction represents the fraction of requests that should be
16420+
selected for tracing after all other sampling checks have been applied.
16421+
properties:
16422+
denominator:
16423+
default: 100
16424+
format: int32
16425+
minimum: 1
16426+
type: integer
16427+
numerator:
16428+
format: int32
16429+
minimum: 0
16430+
type: integer
16431+
required:
16432+
- numerator
16433+
type: object
16434+
x-kubernetes-validations:
16435+
- message: numerator must be less than or equal to denominator
16436+
rule: self.numerator <= self.denominator
1639516437
provider:
1639616438
description: Provider defines the tracing provider.
1639716439
properties:

internal/gatewayapi/backendtrafficpolicy.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,10 +1191,12 @@ func buildBackendTracing(tracing *egv1a1.Tracing) *ir.BackendTracing {
11911191
return nil
11921192
}
11931193
return &ir.BackendTracing{
1194-
SamplingFraction: tracing.SamplingFraction,
1195-
CustomTags: ir.CustomTagMapToSlice(tracing.CustomTags),
1196-
Tags: ir.MapToSlice(tracing.Tags),
1197-
SpanName: tracing.SpanName,
1194+
SamplingFraction: tracing.SamplingFraction,
1195+
ClientSamplingFraction: tracing.ClientSamplingFraction,
1196+
OverallSamplingFraction: tracing.OverallSamplingFraction,
1197+
CustomTags: ir.CustomTagMapToSlice(tracing.CustomTags),
1198+
Tags: ir.MapToSlice(tracing.Tags),
1199+
SpanName: tracing.SpanName,
11981200
}
11991201
}
12001202

internal/gatewayapi/listener.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,14 @@ func (t *Translator) processTracing(gwCtx *GatewayContext, envoyproxy *egv1a1.En
10151015
}
10161016

10171017
return &ir.Tracing{
1018-
Authority: authority,
1019-
ServiceName: serviceName,
1020-
SamplingRate: proxySamplingRate(tracing),
1021-
CustomTags: ir.CustomTagMapToSlice(tracing.CustomTags),
1022-
Tags: ir.MapToSlice(tracing.Tags),
1023-
ResourceAttributes: ir.MapToSlice(getOpenTelemetryTracingResourceAttributes(&tracing.Provider)),
1018+
Authority: authority,
1019+
ServiceName: serviceName,
1020+
SamplingRate: proxySamplingRate(tracing.SamplingRate, tracing.SamplingFraction),
1021+
ClientSamplingRate: proxySamplingFractionPtr(tracing.ClientSamplingFraction),
1022+
OverallSamplingRate: proxySamplingFractionPtr(tracing.OverallSamplingFraction),
1023+
CustomTags: ir.CustomTagMapToSlice(tracing.CustomTags),
1024+
Tags: ir.MapToSlice(tracing.Tags),
1025+
ResourceAttributes: ir.MapToSlice(getOpenTelemetryTracingResourceAttributes(&tracing.Provider)),
10241026
Destination: ir.RouteDestination{
10251027
Name: destName,
10261028
Settings: ds,
@@ -1033,20 +1035,29 @@ func (t *Translator) processTracing(gwCtx *GatewayContext, envoyproxy *egv1a1.En
10331035
}, nil
10341036
}
10351037

1036-
func proxySamplingRate(tracing *egv1a1.ProxyTracing) float64 {
1037-
rate := 100.0
1038-
if tracing.SamplingRate != nil {
1039-
rate = float64(*tracing.SamplingRate)
1040-
} else if tracing.SamplingFraction != nil {
1041-
numerator := float64(tracing.SamplingFraction.Numerator)
1042-
denominator := ptr.Deref(tracing.SamplingFraction.Denominator, 100)
1043-
1044-
rate = numerator * 100 / float64(denominator)
1045-
// Identifies a percentage, in the range [0.0, 100.0]
1046-
rate = math.Max(0, rate)
1047-
rate = math.Min(100, rate)
1038+
func proxySamplingFractionPtr(fraction *gwapiv1.Fraction) *float64 {
1039+
if fraction == nil {
1040+
return nil
1041+
}
1042+
return new(proxySamplingRate(nil, fraction))
1043+
}
1044+
1045+
func proxySamplingRate(rate *uint32, fraction *gwapiv1.Fraction) float64 {
1046+
if rate != nil {
1047+
return float64(*rate)
10481048
}
1049-
return rate
1049+
if fraction == nil {
1050+
return 100.0
1051+
}
1052+
1053+
numerator := float64(fraction.Numerator)
1054+
denominator := ptr.Deref(fraction.Denominator, 100)
1055+
1056+
value := numerator * 100 / float64(denominator)
1057+
// Identifies a percentage, in the range [0.0, 100.0]
1058+
value = math.Max(0, value)
1059+
value = math.Min(100, value)
1060+
return value
10501061
}
10511062

10521063
// getAuthorityFromDestination extracts the gRPC authority from a destination setting.

0 commit comments

Comments
 (0)