Skip to content

Commit

Permalink
Allow any registered gRPC load balancer to be used (#8262)
Browse files Browse the repository at this point in the history
gRPC-Go's `balancer` package includes a static
registration mechanism and a way to inspect whether a balancer name is
registered. We should use this mechanism instead of hard-coding an
allowlist of balancer names.

Custom collector configurations may have additional balancers linked in,
and we should allow them to be used.
  • Loading branch information
jmacd authored Aug 24, 2023
1 parent f453c51 commit 2fe11f5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
25 changes: 25 additions & 0 deletions .chloggen/any_balancer_name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configgrpc

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow any registered gRPC load balancer name to be used.

# One or more tracking issues or pull requests related to the change
issues: [8262]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
12 changes: 2 additions & 10 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding/gzip"
Expand All @@ -38,9 +38,6 @@ import (

var errMetadataNotFound = errors.New("no request metadata found")

// Allowed balancer names to be set in grpclb_policy to discover the servers.
var allowedBalancerNames = []string{roundrobin.Name, grpc.PickFirstBalancerName}

// KeepaliveClientConfig exposes the keepalive.ClientParameters to be used by the exporter.
// Refer to the original data-structure for the meaning of each parameter:
// https://godoc.org/google.golang.org/grpc/keepalive#ClientParameters
Expand Down Expand Up @@ -269,12 +266,7 @@ func (gcs *GRPCClientSettings) toDialOptions(host component.Host, settings compo
}

func validateBalancerName(balancerName string) bool {
for _, item := range allowedBalancerNames {
if item == balancerName {
return true
}
}
return false
return balancer.Get(balancerName) != nil
}

// ToListener returns the net.Listener constructed from the settings.
Expand Down
18 changes: 17 additions & 1 deletion config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"

Expand All @@ -35,6 +36,21 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
)

// testBalancerBuilder facilitates testing validateBalancerName().
type testBalancerBuilder struct{}

func (testBalancerBuilder) Build(_ balancer.ClientConn, _ balancer.BuildOptions) balancer.Balancer {
return nil
}

func (testBalancerBuilder) Name() string {
return "configgrpc_balancer_test"
}

func init() {
balancer.Register(testBalancerBuilder{})
}

func TestDefaultGrpcClientSettings(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry(component.NewID("component"))
require.NoError(t, err)
Expand Down Expand Up @@ -137,7 +153,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
ReadBufferSize: 1024,
WriteBufferSize: 1024,
WaitForReady: true,
BalancerName: "round_robin",
BalancerName: "configgrpc_balancer_test",
Authority: "pseudo-authority",
Auth: &configauth.Authentication{AuthenticatorID: component.NewID("testauth")},
},
Expand Down

0 comments on commit 2fe11f5

Please sign in to comment.