diff --git a/.golangci.yml b/.golangci.yml index e3962bf918f..aa36b79b317 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,6 +12,7 @@ linters: enable: - depguard - errcheck + - errorlint - godot - gofumpt - goimports diff --git a/config/metric.go b/config/metric.go index 53676330994..72f21cf3850 100644 --- a/config/metric.go +++ b/config/metric.go @@ -262,7 +262,7 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet } go func() { - if err := server.Serve(lis); err != nil && err != http.ErrServerClosed { + if err := server.Serve(lis); err != nil && errors.Is(err, http.ErrServerClosed) { otel.Handle(fmt.Errorf("the Prometheus HTTP server exited unexpectedly: %w", err)) } }() diff --git a/detectors/aws/ec2/ec2.go b/detectors/aws/ec2/ec2.go index ee8aade2ed6..e88fe8c2351 100644 --- a/detectors/aws/ec2/ec2.go +++ b/detectors/aws/ec2/ec2.go @@ -5,6 +5,7 @@ package ec2 // import "go.opentelemetry.io/contrib/detectors/aws/ec2" import ( "context" + "errors" "fmt" "net/http" @@ -139,7 +140,8 @@ func (m *metadata) add(k attribute.Key, n string) { return } - rf, ok := err.(awserr.RequestFailure) + var rf awserr.RequestFailure + ok := errors.As(err, &rf) if !ok { m.errs = append(m.errs, fmt.Errorf("%q: %w", n, err)) return diff --git a/detectors/gcp/cloud-function_test.go b/detectors/gcp/cloud-function_test.go index 351f8044e27..240c072e0c9 100644 --- a/detectors/gcp/cloud-function_test.go +++ b/detectors/gcp/cloud-function_test.go @@ -134,7 +134,7 @@ func TestCloudFunctionDetect(t *testing.T) { cloudRun: test.cr, } res, err := detector.Detect(context.Background()) - if err != test.expected.err { + if !errors.Is(err, test.expected.err) { t.Fatalf("got unexpected failure: %v", err) } else if diff := cmp.Diff(test.expected.res, res); diff != "" { t.Errorf("detected resource differ from expected (-want, +got)\n%s", diff) diff --git a/detectors/gcp/gce.go b/detectors/gcp/gce.go index c655dc80a88..694991a1625 100644 --- a/detectors/gcp/gce.go +++ b/detectors/gcp/gce.go @@ -5,6 +5,7 @@ package gcp // import "go.opentelemetry.io/contrib/detectors/gcp" import ( "context" + "errors" "fmt" "os" "strings" @@ -90,7 +91,9 @@ func hasProblem(err error) bool { if err == nil { return false } - if _, undefined := err.(metadata.NotDefinedError); undefined { + + var nde metadata.NotDefinedError + if undefined := errors.As(err, &nde); undefined { return false } return true diff --git a/exporters/autoexport/metrics.go b/exporters/autoexport/metrics.go index 25270de7f9a..5e16b170b4e 100644 --- a/exporters/autoexport/metrics.go +++ b/exporters/autoexport/metrics.go @@ -193,7 +193,7 @@ func init() { } go func() { - if err := server.Serve(lis); err != nil && err != http.ErrServerClosed { + if err := server.Serve(lis); err != nil && !errors.Is(err, http.ErrServerClosed) { otel.Handle(fmt.Errorf("the Prometheus HTTP server exited unexpectedly: %w", err)) } }() diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go b/instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go index 2f681b1d270..31240c2e438 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go @@ -5,6 +5,7 @@ package main import ( "context" + "errors" "fmt" "io" "log" @@ -121,7 +122,7 @@ func callSayHelloServerStream(c api.HelloServiceClient) error { for { response, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { break } else if err != nil { return fmt.Errorf("receiving from SayHelloServerStream: %w", err) @@ -172,7 +173,7 @@ func callSayHelloBidiStream(c api.HelloServiceClient) error { go func() { for { response, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { break } else if err != nil { // nolint: revive // This acts as its own main func. diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go b/instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go index 447c24ed6a6..5f6eecb131f 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go @@ -5,6 +5,7 @@ package main import ( "context" + "errors" "fmt" "io" "log" @@ -68,7 +69,7 @@ func (s *server) SayHelloClientStream(stream api.HelloService_SayHelloClientStre for { in, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { break } else if err != nil { log.Printf("Non EOF error: %v\n", err) @@ -88,7 +89,7 @@ func (s *server) SayHelloBidiStream(stream api.HelloService_SayHelloBidiStreamSe for { in, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { break } else if err != nil { log.Printf("Non EOF error: %v\n", err) diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go b/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go index 58c3a4bfada..7d5ed058082 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go @@ -7,6 +7,7 @@ package otelgrpc // import "go.opentelemetry.io/contrib/instrumentation/google.g // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/rpc.md import ( "context" + "errors" "io" "net" "strconv" @@ -136,7 +137,7 @@ func (w *clientStream) RecvMsg(m interface{}) error { if err == nil && !w.desc.ServerStreams { w.endSpan(nil) - } else if err == io.EOF { + } else if errors.Is(err, io.EOF) { w.endSpan(nil) } else if err != nil { w.endSpan(err) diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go b/instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go index c5697a88322..8feea135dae 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go @@ -28,6 +28,7 @@ package test // import "go.opentelemetry.io/contrib/instrumentation/google.golan import ( "context" + "errors" "fmt" "io" "time" @@ -162,7 +163,7 @@ func DoServerStreaming(ctx context.Context, tc testpb.TestServiceClient, args .. index++ respCnt++ } - if rpcStatus != io.EOF { + if !errors.Is(rpcStatus, io.EOF) { logger.Fatalf("Failed to finish the server streaming rpc: %v", rpcStatus) } if respCnt != len(respSizes) { @@ -209,7 +210,7 @@ func DoPingPong(ctx context.Context, tc testpb.TestServiceClient, args ...grpc.C if err := stream.CloseSend(); err != nil { logger.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil) } - if _, err := stream.Recv(); err != io.EOF { + if _, err := stream.Recv(); !errors.Is(err, io.EOF) { logger.Fatalf("%v failed to complele the ping pong test: %v", stream, err) } } @@ -223,7 +224,7 @@ func DoEmptyStream(ctx context.Context, tc testpb.TestServiceClient, args ...grp if err := stream.CloseSend(); err != nil { logger.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil) } - if _, err := stream.Recv(); err != io.EOF { + if _, err := stream.Recv(); !errors.Is(err, io.EOF) { logger.Fatalf("%v failed to complete the empty stream test: %v", stream, err) } } @@ -306,7 +307,7 @@ func (s *testServer) StreamingInputCall(stream testpb.TestService_StreamingInput var sum int for { in, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { return stream.SendAndClose(&testpb.StreamingInputCallResponse{ AggregatedPayloadSize: int32(sum), }) @@ -332,7 +333,7 @@ func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServ } for { in, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { // read done. return nil } @@ -366,7 +367,7 @@ func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServ var msgBuf []*testpb.StreamingOutputCallRequest for { in, err := stream.Recv() - if err == io.EOF { + if errors.Is(err, io.EOF) { // read done. break } diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go b/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go index 72f9de68758..7d951d6b2a3 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go @@ -5,6 +5,7 @@ package test import ( "context" + "errors" "io" "net" "strconv" @@ -1438,7 +1439,7 @@ func TestStatsHandlerConcurrentSafeContextCancellation(t *testing.T) { Payload: pl, } err := stream.Send(req) - if err == io.EOF { // possible due to context cancellation + if errors.Is(err, io.EOF) { // possible due to context cancellation require.ErrorIs(t, ctx.Err(), context.Canceled) } else { require.NoError(t, err) diff --git a/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go b/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go index cc4d6e12e29..c3e838aaa54 100644 --- a/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go +++ b/instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go @@ -4,6 +4,7 @@ package semconv // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconv" import ( + "errors" "io" "net/http" @@ -43,7 +44,7 @@ func (o oldHTTPServer) ResponseTraceAttrs(resp ResponseTelemetry) []attribute.Ke if resp.ReadBytes > 0 { attributes = append(attributes, semconv.HTTPRequestContentLength(int(resp.ReadBytes))) } - if resp.ReadError != nil && resp.ReadError != io.EOF { + if resp.ReadError != nil && !errors.Is(resp.ReadError, io.EOF) { // This is not in the semantic conventions, but is historically provided attributes = append(attributes, attribute.String("http.read_error", resp.ReadError.Error())) } @@ -53,7 +54,7 @@ func (o oldHTTPServer) ResponseTraceAttrs(resp ResponseTelemetry) []attribute.Ke if resp.StatusCode > 0 { attributes = append(attributes, semconv.HTTPStatusCode(resp.StatusCode)) } - if resp.WriteError != nil && resp.WriteError != io.EOF { + if resp.WriteError != nil && !errors.Is(resp.WriteError, io.EOF) { // This is not in the semantic conventions, but is historically provided attributes = append(attributes, attribute.String("http.write_error", resp.WriteError.Error())) }