Files
tasks/common/metrics/grpc_test.go
Stephan BehnkeandGitHub b43c4b0f8e Enforce import alias for protobufs (#6968)
## What changed?
<!-- Describe what has changed in this PR -->

Linter to enforce import aliases for protobuf imports.

Enforced rules across entire codebase.

## Why?
<!-- Tell your future self why have you made these changes -->

Consistency. Relief reviewers from pointing it out.

We had 100+ violations.

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->

Running
```
make lint-code
.bin/golangci-lint-dafd65537336fdce063c492a7ab2a68cc89f8d52 run --config=.golangci.yml | grep importas
```

comes up empty (compared to say `grep typecheck`).

Plus compiler and tests ofc.

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->

Me smuggling in an easter egg into the code (I didn't, I swear 🙃)

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
2024-12-12 12:45:30 -08:00

212 lines
6.3 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
metricsspb "go.temporal.io/server/api/metrics/v1"
"go.temporal.io/server/common/log"
"go.uber.org/mock/gomock"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type (
grpcSuite struct {
suite.Suite
*require.Assertions
controller *gomock.Controller
}
)
func TestGrpcSuite(t *testing.T) {
s := new(grpcSuite)
suite.Run(t, s)
}
func (s *grpcSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
}
func (s *grpcSuite) TearDownTest() {}
func (s *grpcSuite) TestMetadataMetricInjection() {
logger := log.NewMockLogger(s.controller)
ctx := context.Background()
ssts := newMockServerTransportStream()
ctx = grpc.NewContextWithServerTransportStream(ctx, ssts)
anyMetricName := "any_metric_name"
smcii := NewServerMetricsContextInjectorInterceptor()
s.NotNil(smcii)
res, err := smcii(
ctx, nil, nil,
func(ctx context.Context, req interface{}) (interface{}, error) {
res, err := NewServerMetricsTrailerPropagatorInterceptor(logger)(
ctx, req, nil,
func(ctx context.Context, req interface{}) (interface{}, error) {
cmtpi := NewClientMetricsTrailerPropagatorInterceptor(logger)
s.NotNil(cmtpi)
cmtpi(
ctx, "any_value", nil, nil, nil,
func(
ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
opts ...grpc.CallOption,
) error {
trailer := opts[0].(grpc.TrailerCallOption)
propagationContext := &metricsspb.Baggage{CountersInt: make(map[string]int64)}
propagationContext.CountersInt[anyMetricName] = 1234
data, err := propagationContext.Marshal()
if err != nil {
s.Fail("failed to marshal values")
}
*trailer.TrailerAddr = metadata.MD{}
trailer.TrailerAddr.Append(metricsTrailerKey, string(data))
return nil
},
)
return 10, nil
},
)
s.Nil(err)
s.Equal(len(ssts.trailers), 1)
propagationContextBlobs := ssts.trailers[0].Get(metricsTrailerKey)
s.NotNil(propagationContextBlobs)
s.Equal(1, len(propagationContextBlobs))
baggage := &metricsspb.Baggage{}
err = baggage.Unmarshal(([]byte)(propagationContextBlobs[0]))
s.Nil(err)
s.Equal(int64(1234), baggage.CountersInt[anyMetricName])
return res, err
},
)
s.Nil(err)
s.Equal(10, res)
s.Assert()
}
func (s *grpcSuite) TestMetadataMetricInjection_NoMetricPresent() {
logger := log.NewMockLogger(s.controller)
ctx := context.Background()
ssts := newMockServerTransportStream()
ctx = grpc.NewContextWithServerTransportStream(ctx, ssts)
smcii := NewServerMetricsContextInjectorInterceptor()
s.NotNil(smcii)
res, err := smcii(
ctx, nil, nil,
func(ctx context.Context, req interface{}) (interface{}, error) {
res, err := NewServerMetricsTrailerPropagatorInterceptor(logger)(
ctx, req, nil,
func(ctx context.Context, req interface{}) (interface{}, error) {
cmtpi := NewClientMetricsTrailerPropagatorInterceptor(logger)
s.NotNil(cmtpi)
cmtpi(
ctx, "any_value", nil, nil, nil,
func(
ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
opts ...grpc.CallOption,
) error {
trailer := opts[0].(grpc.TrailerCallOption)
propagationContext := &metricsspb.Baggage{}
data, err := propagationContext.Marshal()
if err != nil {
s.Fail("failed to marshal values")
}
trailer.TrailerAddr = &metadata.MD{}
trailer.TrailerAddr.Append(metricsTrailerKey, string(data))
return nil
},
)
return 10, nil
},
)
s.Nil(err)
s.Equal(len(ssts.trailers), 1)
propagationContextBlobs := ssts.trailers[0].Get(metricsTrailerKey)
s.NotNil(propagationContextBlobs)
s.Equal(1, len(propagationContextBlobs))
baggage := &metricsspb.Baggage{}
err = baggage.Unmarshal(([]byte)(propagationContextBlobs[0]))
s.Nil(err)
s.Nil(baggage.CountersInt)
return res, err
},
)
s.Nil(err)
s.Equal(10, res)
s.Assert()
}
func (s *grpcSuite) TestContextCounterAdd() {
ctx := AddMetricsContext(context.Background())
testCounterName := "test_counter"
ContextCounterAdd(ctx, testCounterName, 100)
ContextCounterAdd(ctx, testCounterName, 20)
ContextCounterAdd(ctx, testCounterName, 3)
value, ok := ContextCounterGet(ctx, testCounterName)
s.True(ok)
s.Equal(int64(123), value)
}
func (s *grpcSuite) TestContextCounterAddNoMetricsContext() {
testCounterName := "test_counter"
ContextCounterAdd(context.Background(), testCounterName, 3)
}
func newMockServerTransportStream() *mockServerTransportStream {
return &mockServerTransportStream{trailers: []*metadata.MD{}}
}
type mockServerTransportStream struct {
trailers []*metadata.MD
}
func (s *mockServerTransportStream) Method() string {
return "mockssts"
}
func (s *mockServerTransportStream) SetHeader(md metadata.MD) error {
return nil
}
func (s *mockServerTransportStream) SendHeader(md metadata.MD) error {
return nil
}
func (s *mockServerTransportStream) SetTrailer(md metadata.MD) error {
mdCopy := md.Copy()
s.trailers = append(s.trailers, &mdCopy)
return nil
}