Skip to content

Commit e7eb9dd

Browse files
authored
impl(sidekick/rust): feature flag tracing attrs (#2311)
We are introducing detailed tracing attributes to the generate Rust code. This change makes these additional details optional until we have stabilized the APIs. Mainly this is to make other changes to the generator easier to grok and apply. The observability changes look low risk but are noisy.
1 parent dca5878 commit e7eb9dd

File tree

4 files changed

+85
-33
lines changed

4 files changed

+85
-33
lines changed

internal/sidekick/internal/rust/annotate.go

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,21 @@ type messageAnnotation struct {
188188
}
189189

190190
type methodAnnotation struct {
191-
Name string
192-
BuilderName string
193-
DocLines []string
194-
PathInfo *api.PathInfo
195-
Body string
196-
ServiceNameToPascal string
197-
ServiceNameToCamel string
198-
ServiceNameToSnake string
199-
OperationInfo *operationInfo
200-
SystemParameters []systemParameter
201-
ReturnType string
202-
HasVeneer bool
203-
Attributes []string
204-
RoutingRequired bool
191+
Name string
192+
BuilderName string
193+
DocLines []string
194+
PathInfo *api.PathInfo
195+
Body string
196+
ServiceNameToPascal string
197+
ServiceNameToCamel string
198+
ServiceNameToSnake string
199+
OperationInfo *operationInfo
200+
SystemParameters []systemParameter
201+
ReturnType string
202+
HasVeneer bool
203+
Attributes []string
204+
RoutingRequired bool
205+
DetailedTracingAttributes bool
205206
}
206207

207208
type pathInfoAnnotation struct {
@@ -312,6 +313,9 @@ type pathBindingAnnotation struct {
312313

313314
// The variables to be substituted into the path
314315
Substitutions []*bindingSubstitution
316+
317+
// The codec is configured to generated detailed tracing attributes.
318+
DetailedTracingAttributes bool
315319
}
316320

317321
// QueryParamsCanFail returns true if we serialize certain query parameters, which can fail. The code we generate
@@ -694,7 +698,7 @@ func (c *codec) annotateMessage(m *api.Message, model *api.API) {
694698
}
695699

696700
func (c *codec) annotateMethod(m *api.Method) {
697-
annotatePathInfo(m)
701+
c.annotatePathInfo(m)
698702
for _, routing := range m.Routing {
699703
for _, variant := range routing.Variants {
700704
routingVariantAnnotations := &routingVariantAnnotations{
@@ -712,18 +716,19 @@ func (c *codec) annotateMethod(m *api.Method) {
712716
}
713717
serviceName := c.ServiceName(m.Service)
714718
annotation := &methodAnnotation{
715-
Name: strcase.ToSnake(m.Name),
716-
BuilderName: toPascal(m.Name),
717-
Body: bodyAccessor(m),
718-
DocLines: c.formatDocComments(m.Documentation, m.ID, m.Model.State, m.Service.Scopes()),
719-
PathInfo: m.PathInfo,
720-
ServiceNameToPascal: toPascal(serviceName),
721-
ServiceNameToCamel: toCamel(serviceName),
722-
ServiceNameToSnake: toSnake(serviceName),
723-
SystemParameters: c.systemParameters,
724-
ReturnType: returnType,
725-
HasVeneer: c.hasVeneer,
726-
RoutingRequired: c.routingRequired,
719+
Name: strcase.ToSnake(m.Name),
720+
BuilderName: toPascal(m.Name),
721+
Body: bodyAccessor(m),
722+
DocLines: c.formatDocComments(m.Documentation, m.ID, m.Model.State, m.Service.Scopes()),
723+
PathInfo: m.PathInfo,
724+
ServiceNameToPascal: toPascal(serviceName),
725+
ServiceNameToCamel: toCamel(serviceName),
726+
ServiceNameToSnake: toSnake(serviceName),
727+
SystemParameters: c.systemParameters,
728+
ReturnType: returnType,
729+
HasVeneer: c.hasVeneer,
730+
RoutingRequired: c.routingRequired,
731+
DetailedTracingAttributes: c.detailedTracingAttributes,
727732
}
728733
if annotation.Name == "clone" {
729734
// Some methods look too similar to standard Rust traits. Clippy makes
@@ -831,7 +836,7 @@ func makeBindingSubstitution(v *api.PathVariable, m *api.Method) bindingSubstitu
831836
}
832837
}
833838

834-
func annotatePathBinding(b *api.PathBinding, m *api.Method) *pathBindingAnnotation {
839+
func (c *codec) annotatePathBinding(b *api.PathBinding, m *api.Method) *pathBindingAnnotation {
835840
var subs []*bindingSubstitution
836841
for _, s := range b.PathTemplate.Segments {
837842
if s.Variable != nil {
@@ -840,19 +845,20 @@ func annotatePathBinding(b *api.PathBinding, m *api.Method) *pathBindingAnnotati
840845
}
841846
}
842847
return &pathBindingAnnotation{
843-
PathFmt: httpPathFmt(b.PathTemplate),
844-
QueryParams: language.QueryParams(m, b),
845-
Substitutions: subs,
848+
PathFmt: httpPathFmt(b.PathTemplate),
849+
QueryParams: language.QueryParams(m, b),
850+
Substitutions: subs,
851+
DetailedTracingAttributes: c.detailedTracingAttributes,
846852
}
847853
}
848854

849855
// annotatePathInfo annotates the `PathInfo` and all of its `PathBinding`s.
850-
func annotatePathInfo(m *api.Method) {
856+
func (c *codec) annotatePathInfo(m *api.Method) {
851857
seen := make(map[string]bool)
852858
var uniqueParameters []*bindingSubstitution
853859

854860
for _, b := range m.PathInfo.Bindings {
855-
ann := annotatePathBinding(b, m)
861+
ann := c.annotatePathBinding(b, m)
856862

857863
// We need to keep track of unique path parameters to support
858864
// implicit routing over gRPC. This is go/aip/4222.

internal/sidekick/internal/rust/codec.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ func newCodec(protobufSource bool, options map[string]string) (*codec, error) {
130130
return nil, fmt.Errorf("cannot convert `per-service-features` value %q to boolean: %w", definition, err)
131131
}
132132
codec.perServiceFeatures = value
133+
case key == "detailed-tracing-attributes":
134+
value, err := strconv.ParseBool(definition)
135+
if err != nil {
136+
return nil, fmt.Errorf("cannot convert `detailed-tracing-attributes` value %q to boolean: %w", definition, err)
137+
}
138+
codec.detailedTracingAttributes = value
133139
case key == "has-veneer":
134140
value, err := strconv.ParseBool(definition)
135141
if err != nil {
@@ -243,6 +249,12 @@ type codec struct {
243249
includeGrpcOnlyMethods bool
244250
// If true, the generator will produce per-client features.
245251
perServiceFeatures bool
252+
// If true, the generated code includes detailed tracing attributes on HTTP
253+
// requests. This feature flag exists to reduce unexpected changes to the
254+
// generated code until the feature is ready and well-tested.
255+
// TODO(https://github.com/googleapis/google-cloud-rust/issues/3239) -
256+
// remove this flag once we switch the default.
257+
detailedTracingAttributes bool
246258
// If true, there is a handwritten client surface.
247259
hasVeneer bool
248260
// A list of types which should only be `pub(crate)`.

internal/sidekick/internal/rust/codec_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ func TestParseOptionsTemplateOverride(t *testing.T) {
183183
checkRustPackages(t, got, want)
184184
}
185185

186+
func TestParseOptionsDetailedTracingAttributes(t *testing.T) {
187+
options := map[string]string{
188+
"detailed-tracing-attributes": "true",
189+
}
190+
got, err := newCodec(false, options)
191+
if err != nil {
192+
t.Fatal(err)
193+
}
194+
if !got.detailedTracingAttributes {
195+
t.Errorf("expected tracing attributes to be enabled")
196+
}
197+
198+
options = map[string]string{
199+
"detailed-tracing-attributes": "--invalid--",
200+
}
201+
if got, err := newCodec(false, options); err == nil {
202+
t.Errorf("expected an error with an invalid detailed-tracing-attributes flag, got=%v", got)
203+
}
204+
}
205+
186206
func TestPackageName(t *testing.T) {
187207
rustPackageNameImpl(t, "test-only-overridden", map[string]string{
188208
"package-name-override": "test-only-overridden",

internal/sidekick/internal/rust/templates/crate/src/transport.rs.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
8080
true,
8181
);
8282
{{/HasAutoPopulatedFields}}
83+
{{#Codec.DetailedTracingAttributes}}
8384
let (builder, method, path_template) = None
85+
{{/Codec.DetailedTracingAttributes}}
86+
{{^Codec.DetailedTracingAttributes}}
87+
let (builder, method) = None
88+
{{/Codec.DetailedTracingAttributes}}
8489
{{#PathInfo.Bindings}}
8590
.or_else(|| {
8691
{{#Codec.HasVariablePath}}
@@ -94,7 +99,9 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
9499
{{^Codec.HasVariablePath}}
95100
let path = "{{Codec.PathFmt}}".to_string();
96101
{{/Codec.HasVariablePath}}
102+
{{#Codec.DetailedTracingAttributes}}
97103
let path_template = "{{Codec.PathTemplate}}";
104+
{{/Codec.DetailedTracingAttributes}}
98105
99106
let builder = self
100107
.inner
@@ -113,7 +120,12 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
113120
{{/Codec.QueryParams}}
114121
let builder = Ok(builder);
115122
{{/Codec.QueryParamsCanFail}}
123+
{{#Codec.DetailedTracingAttributes}}
116124
Some(builder.map(|b| (b, reqwest::Method::{{Verb}}, path_template)))
125+
{{/Codec.DetailedTracingAttributes}}
126+
{{^Codec.DetailedTracingAttributes}}
127+
Some(builder.map(|b| (b, reqwest::Method::{{Verb}})))
128+
{{/Codec.DetailedTracingAttributes}}
117129
})
118130
{{/PathInfo.Bindings}}
119131
.ok_or_else(|| {
@@ -133,7 +145,9 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
133145
{{/PathInfo.Bindings}}
134146
gax::error::Error::binding(BindingError { paths })
135147
})??;
148+
{{#Codec.DetailedTracingAttributes}}
136149
let options = gax::options::internal::set_path_template(options, path_template);
150+
{{/Codec.DetailedTracingAttributes}}
137151
let options = gax::options::internal::set_default_idempotency(
138152
options,
139153
{{! TODO(#2588) - return idempotency from the above closure }}

0 commit comments

Comments
 (0)