Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 170 additions & 1 deletion internal/gengapic/client_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,29 @@ func TestClientInit(t *testing.T) {
imports map[pbinfo.ImportSpec]bool
wantNumSnps int
setExt func() (protoreflect.ExtensionType, interface{})
features []featureID
}{
{
tstName: "foo_client_init_default",
mixins: mixins{
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
},
servName: "Foo",
serv: servPlain,
parameter: proto.String("go-gapic-package=path;mypackage"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/api/option"}: true,
{Name: "gtransport", Path: "google.golang.org/api/transport/grpc"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
{Name: "locationpb", Path: "google.golang.org/genproto/googleapis/cloud/location"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
},
{
tstName: "foo_client_init",
mixins: mixins{
Expand All @@ -436,6 +458,73 @@ func TestClientInit(t *testing.T) {
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
features: []featureID{OpenTelemetryTracingFeature},
},
{
tstName: "foo_client_init_logging",
mixins: mixins{
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
},
servName: "Foo",
serv: servPlain,
parameter: proto.String("go-gapic-package=path;mypackage"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/api/option"}: true,
{Name: "gtransport", Path: "google.golang.org/api/transport/grpc"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
{Name: "locationpb", Path: "google.golang.org/genproto/googleapis/cloud/location"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
features: []featureID{OpenTelemetryLoggingFeature},
},
{
tstName: "foo_client_init_metrics",
mixins: mixins{
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
},
servName: "Foo",
serv: servPlain,
parameter: proto.String("go-gapic-package=path;mypackage"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/api/option"}: true,
{Name: "gtransport", Path: "google.golang.org/api/transport/grpc"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
{Name: "locationpb", Path: "google.golang.org/genproto/googleapis/cloud/location"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
features: []featureID{OpenTelemetryMetricsFeature},
},
{
tstName: "foo_client_init_all_telemetry",
mixins: mixins{
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
},
servName: "Foo",
serv: servPlain,
parameter: proto.String("go-gapic-package=path;mypackage"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/api/option"}: true,
{Name: "gtransport", Path: "google.golang.org/api/transport/grpc"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
{Name: "locationpb", Path: "google.golang.org/genproto/googleapis/cloud/location"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
features: []featureID{OpenTelemetryTracingFeature, OpenTelemetryLoggingFeature, OpenTelemetryMetricsFeature},
},
{
tstName: "foo_rest_client_init",
Expand All @@ -460,6 +549,54 @@ func TestClientInit(t *testing.T) {
},
wantNumSnps: 6,
},
{
tstName: "foo_rest_client_init_default",
mixins: mixins{
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
},
servName: "Foo",
serv: servPlain,
parameter: proto.String("go-gapic-package=path;mypackage,transport=rest"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/api/option/internaloption"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "net/http"}: true,
{Name: "httptransport", Path: "google.golang.org/api/transport/http"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
{Name: "locationpb", Path: "google.golang.org/genproto/googleapis/cloud/location"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
features: []featureID{""},
},
{
tstName: "foo_rest_client_init_metrics",
mixins: mixins{
"google.cloud.location.Locations": locationMethods(),
"google.iam.v1.IAMPolicy": iamPolicyMethods(),
},
servName: "Foo",
serv: servPlain,
parameter: proto.String("go-gapic-package=path;mypackage,transport=rest"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/api/option/internaloption"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "net/http"}: true,
{Name: "httptransport", Path: "google.golang.org/api/transport/http"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
{Name: "locationpb", Path: "google.golang.org/genproto/googleapis/cloud/location"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 6,
features: []featureID{OpenTelemetryTracingFeature, OpenTelemetryMetricsFeature},
},
{
tstName: "empty_client_init",
servName: "",
Expand Down Expand Up @@ -537,6 +674,28 @@ func TestClientInit(t *testing.T) {
return extendedops.E_OperationService, opS.GetName()
},
},
{
tstName: "custom_op_init_metrics",
servName: "",
serv: servCustomOp,
customOpServ: opS,
parameter: proto.String("go-gapic-package=path;mypackage,transport=rest,diregapic"),
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/api/option/internaloption"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "net/http"}: true,
{Name: "httptransport", Path: "google.golang.org/api/transport/http"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "log/slog"}: true,
},
wantNumSnps: 1,
setExt: func() (protoreflect.ExtensionType, interface{}) {
return extendedops.E_OperationService, opS.GetName()
},
features: []featureID{OpenTelemetryTracingFeature, OpenTelemetryMetricsFeature},
},
} {
t.Run(tst.tstName, func(t *testing.T) {
setExt := tst.setExt
Expand Down Expand Up @@ -602,7 +761,17 @@ func TestClientInit(t *testing.T) {
}

g.reset()
g.cfg.featureEnablement = map[featureID]struct{}{OpenTelemetryTracingFeature: {}}
g.cfg.featureEnablement = make(map[featureID]struct{})
if len(tst.features) == 0 {
g.cfg.featureEnablement[OpenTelemetryTracingFeature] = struct{}{}
}
for _, f := range tst.features {
if f == "" {
g.cfg.featureEnablement = make(map[featureID]struct{})
} else {
g.cfg.featureEnablement[f] = struct{}{}
}
}
sm := snippets.NewMetadata("mypackage", "github.com/googleapis/mypackage", "mypackagego")
sm.AddService(tst.serv.GetName(), "mypackage.googleapis.com")
for _, m := range tst.serv.GetMethod() {
Expand Down
4 changes: 4 additions & 0 deletions internal/gengapic/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
MTLSHardBoundTokensFeature featureID = "mtls_hard_bound_tokens"
OpenTelemetryTracingFeature featureID = "open_telemetry_tracing"
OpenTelemetryLoggingFeature featureID = "open_telemetry_logging"
OpenTelemetryMetricsFeature featureID = "open_telemetry_metrics"
)

// featureRegistry contains the registry of defined features.
Expand All @@ -50,6 +51,9 @@ var featureRegistry = map[featureID]*featureInfo{
Description: "Enable OpenTelemetry logging support (Service Identity, Resource Names, URL Templates).",
TrackingID: "b/476980971",
},
OpenTelemetryMetricsFeature: {
Description: "Enable OpenTelemetry M1 metrics support (Duration, RPC Method, URL Template, Error Mapping).",
},
MTLSHardBoundTokensFeature: {
Description: "support MTLS hard bound tokens",
TrackingID: "b/327916505",
Expand Down
18 changes: 18 additions & 0 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,24 @@ func (g *generator) appendCallOpts(m *descriptorpb.MethodDescriptorProto) {
g.printf("opts = append(%[1]s[0:len(%[1]s):len(%[1]s)], opts...)", "(*c.CallOptions)."+*m.Name)
}

func (g *generator) injectTelemetryContext(m *descriptorpb.MethodDescriptorProto, info *httpInfo) {
if m == nil {
return
}
if g.featureEnabled(OpenTelemetryMetricsFeature) || g.featureEnabled(OpenTelemetryTracingFeature) {
g.imports[pbinfo.ImportSpec{Path: "github.com/googleapis/gax-go/v2/callctx"}] = true
g.imports[pbinfo.ImportSpec{Name: "gax", Path: "github.com/googleapis/gax-go/v2"}] = true
serv := g.descInfo.ParentElement[m].(*descriptorpb.ServiceDescriptorProto)
fqn := fmt.Sprintf("%s.%s/%s", g.descInfo.ParentFile[serv].GetPackage(), serv.GetName(), m.GetName())
g.printf("if gax.IsFeatureEnabled(\"METRICS\") || gax.IsFeatureEnabled(\"TRACING\") {")
g.printf(" ctx = callctx.WithTelemetryContext(ctx, \"rpc_method\", %q)", fqn)
Comment thread
quartzmo marked this conversation as resolved.
if info != nil && info.url != "" {
g.printf(" ctx = callctx.WithTelemetryContext(ctx, \"url_template\", %q)", info.url)
}
g.printf("}")
}
}

// This is a helper function that checks whether a description contains a Deprecated header.
func containsDeprecated(com string) bool {
for _, s := range strings.Split(com, "\n") {
Expand Down
Loading
Loading