Skip to content

Commit

Permalink
Start enforcing grpc server implementation to embed UnimplementedGRPC…
Browse files Browse the repository at this point in the history
…Server, dissallow client implementation (open-telemetry#6967)

* Enforce grpc server implementation to embed UnimplementedGRPCServer, dissallow client implementation

Fixes open-telemetry#6966

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>

* Update disallowimpl.yaml

* Update grpc.go

* Update grpc.go

* Update grpc.go

* Update grpc.go

* Update grpc.go

* Update grpc.go

* Update grpc.go

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jan 19, 2023
1 parent fb50717 commit 5358eb9
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .chloggen/disallowimpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Start enforcing grpc server implementation to embed UnimplementedGRPCServer, dissallow client implementation

# One or more tracking issues or pull requests related to the change
issues: [6966]
1 change: 1 addition & 0 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ func (m *mockServerStream) Context() context.Context {
}

type grpcTraceServer struct {
ptraceotlp.UnimplementedGRPCServer
recordedContext context.Context
}

Expand Down
3 changes: 3 additions & 0 deletions exporter/otlpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (r *mockReceiver) setExportError(err error) {
}

type mockTracesReceiver struct {
ptraceotlp.UnimplementedGRPCServer
mockReceiver
lastRequest ptrace.Traces
}
Expand Down Expand Up @@ -125,6 +126,7 @@ func otlpTracesReceiverOnGRPCServer(ln net.Listener, useTLS bool) (*mockTracesRe
}

type mockLogsReceiver struct {
plogotlp.UnimplementedGRPCServer
mockReceiver
lastRequest plog.Logs
}
Expand Down Expand Up @@ -165,6 +167,7 @@ func otlpLogsReceiverOnGRPCServer(ln net.Listener) *mockLogsReceiver {
}

type mockMetricsReceiver struct {
pmetricotlp.UnimplementedGRPCServer
mockReceiver
lastRequest pmetric.Metrics
}
Expand Down
17 changes: 17 additions & 0 deletions pdata/plog/plogotlp/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

otlpcollectorlog "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/logs/v1"
"go.opentelemetry.io/collector/pdata/internal/otlp"
Expand All @@ -32,6 +34,9 @@ type GRPCClient interface {
// For performance reasons, it is recommended to keep this RPC
// alive for the entire life of the application.
Export(ctx context.Context, request ExportRequest, opts ...grpc.CallOption) (ExportResponse, error)

// unexported disallow implementation of the GRPCClient.
unexported()
}

// NewGRPCClient returns a new GRPCClient connected using the given connection.
Expand All @@ -48,7 +53,10 @@ func (c *grpcClient) Export(ctx context.Context, request ExportRequest, opts ...
return ExportResponse{orig: rsp}, err
}

func (c *grpcClient) unexported() {}

// GRPCServer is the server API for OTLP gRPC LogsService service.
// Implementations MUST embed UnimplementedGRPCServer.
type GRPCServer interface {
// Export is called every time a new request is received.
//
Expand All @@ -57,6 +65,15 @@ type GRPCServer interface {
Export(context.Context, ExportRequest) (ExportResponse, error)
}

var _ GRPCServer = (*UnimplementedGRPCServer)(nil)

// UnimplementedGRPCServer MUST be embedded to have forward compatible implementations.
type UnimplementedGRPCServer struct{}

func (*UnimplementedGRPCServer) Export(context.Context, ExportRequest) (ExportResponse, error) {
return ExportResponse{}, status.Errorf(codes.Unimplemented, "method Export not implemented")
}

// RegisterGRPCServer registers the Server to the grpc.Server.
func RegisterGRPCServer(s *grpc.Server, srv GRPCServer) {
otlpcollectorlog.RegisterLogsServiceServer(s, &rawLogsServer{srv: srv})
Expand Down
1 change: 1 addition & 0 deletions pdata/plog/plogotlp/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestGrpcError(t *testing.T) {
}

type fakeLogsServer struct {
UnimplementedGRPCServer
t *testing.T
err error
}
Expand Down
16 changes: 16 additions & 0 deletions pdata/pmetric/pmetricotlp/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

otlpcollectormetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/metrics/v1"
"go.opentelemetry.io/collector/pdata/internal/otlp"
Expand All @@ -32,6 +34,9 @@ type GRPCClient interface {
// For performance reasons, it is recommended to keep this RPC
// alive for the entire life of the application.
Export(ctx context.Context, request ExportRequest, opts ...grpc.CallOption) (ExportResponse, error)

// unexported disallow implementation of the GRPCClient.
unexported()
}

// NewGRPCClient returns a new GRPCClient connected using the given connection.
Expand All @@ -48,6 +53,8 @@ func (c *grpcClient) Export(ctx context.Context, request ExportRequest, opts ...
return ExportResponse{orig: rsp}, err
}

func (c *grpcClient) unexported() {}

// GRPCServer is the server API for OTLP gRPC MetricsService service.
type GRPCServer interface {
// Export is called every time a new request is received.
Expand All @@ -57,6 +64,15 @@ type GRPCServer interface {
Export(context.Context, ExportRequest) (ExportResponse, error)
}

var _ GRPCServer = (*UnimplementedGRPCServer)(nil)

// UnimplementedGRPCServer MUST be embedded to have forward compatible implementations.
type UnimplementedGRPCServer struct{}

func (*UnimplementedGRPCServer) Export(context.Context, ExportRequest) (ExportResponse, error) {
return ExportResponse{}, status.Errorf(codes.Unimplemented, "method Export not implemented")
}

// RegisterGRPCServer registers the GRPCServer to the grpc.Server.
func RegisterGRPCServer(s *grpc.Server, srv GRPCServer) {
otlpcollectormetrics.RegisterMetricsServiceServer(s, &rawMetricsServer{srv: srv})
Expand Down
1 change: 1 addition & 0 deletions pdata/pmetric/pmetricotlp/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestGrpcError(t *testing.T) {
}

type fakeMetricsServer struct {
UnimplementedGRPCServer
t *testing.T
err error
}
Expand Down
16 changes: 16 additions & 0 deletions pdata/ptrace/ptraceotlp/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

otlpcollectortrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/trace/v1"
"go.opentelemetry.io/collector/pdata/internal/otlp"
Expand All @@ -32,6 +34,9 @@ type GRPCClient interface {
// For performance reasons, it is recommended to keep this RPC
// alive for the entire life of the application.
Export(ctx context.Context, request ExportRequest, opts ...grpc.CallOption) (ExportResponse, error)

// unexported disallow implementation of the GRPCClient.
unexported()
}

// NewGRPCClient returns a new GRPCClient connected using the given connection.
Expand All @@ -49,6 +54,8 @@ func (c *grpcClient) Export(ctx context.Context, request ExportRequest, opts ...
return ExportResponse{orig: rsp}, err
}

func (c *grpcClient) unexported() {}

// GRPCServer is the server API for OTLP gRPC TracesService service.
type GRPCServer interface {
// Export is called every time a new request is received.
Expand All @@ -58,6 +65,15 @@ type GRPCServer interface {
Export(context.Context, ExportRequest) (ExportResponse, error)
}

var _ GRPCServer = (*UnimplementedGRPCServer)(nil)

// UnimplementedGRPCServer MUST be embedded to have forward compatible implementations.
type UnimplementedGRPCServer struct{}

func (*UnimplementedGRPCServer) Export(context.Context, ExportRequest) (ExportResponse, error) {
return ExportResponse{}, status.Errorf(codes.Unimplemented, "method Export not implemented")
}

// RegisterGRPCServer registers the GRPCServer to the grpc.Server.
func RegisterGRPCServer(s *grpc.Server, srv GRPCServer) {
otlpcollectortrace.RegisterTraceServiceServer(s, &rawTracesServer{srv: srv})
Expand Down
1 change: 1 addition & 0 deletions pdata/ptrace/ptraceotlp/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestGrpcError(t *testing.T) {
}

type fakeTracesServer struct {
UnimplementedGRPCServer
t *testing.T
err error
}
Expand Down
1 change: 1 addition & 0 deletions receiver/otlpreceiver/internal/logs/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (

// Receiver is the type used to handle spans from OpenTelemetry exporters.
type Receiver struct {
plogotlp.UnimplementedGRPCServer
nextConsumer consumer.Logs
obsrecv *obsreport.Receiver
}
Expand Down
1 change: 1 addition & 0 deletions receiver/otlpreceiver/internal/metrics/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (

// Receiver is the type used to handle metrics from OpenTelemetry exporters.
type Receiver struct {
pmetricotlp.UnimplementedGRPCServer
nextConsumer consumer.Metrics
obsrecv *obsreport.Receiver
}
Expand Down
1 change: 1 addition & 0 deletions receiver/otlpreceiver/internal/trace/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (

// Receiver is the type used to handle spans from OpenTelemetry exporters.
type Receiver struct {
ptraceotlp.UnimplementedGRPCServer
nextConsumer consumer.Traces
obsrecv *obsreport.Receiver
}
Expand Down

0 comments on commit 5358eb9

Please sign in to comment.