Skip to content

Commit

Permalink
add errorlint linter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Jun 26, 2024
1 parent 669ca60 commit 2b6025f
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
enable:
- depguard
- errcheck
- errorlint
- godot
- gofumpt
- goimports
Expand Down
2 changes: 1 addition & 1 deletion config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}()
Expand Down
4 changes: 3 additions & 1 deletion detectors/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ec2 // import "go.opentelemetry.io/contrib/detectors/aws/ec2"

import (
"context"
"errors"
"fmt"
"net/http"

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion detectors/gcp/cloud-function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion detectors/gcp/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gcp // import "go.opentelemetry.io/contrib/detectors/gcp"

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion exporters/autoexport/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -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) {

Check warning on line 125 in instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go#L125

Added line #L125 was not covered by tests
break
} else if err != nil {
return fmt.Errorf("receiving from SayHelloServerStream: %w", err)
Expand Down Expand Up @@ -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) {

Check warning on line 176 in instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/example/client/main.go#L176

Added line #L176 was not covered by tests
break
} else if err != nil {
// nolint: revive // This acts as its own main func.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -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) {

Check warning on line 72 in instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go#L72

Added line #L72 was not covered by tests
break
} else if err != nil {
log.Printf("Non EOF error: %v\n", err)
Expand All @@ -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) {

Check warning on line 92 in instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/example/server/main.go#L92

Added line #L92 was not covered by tests
break
} else if err != nil {
log.Printf("Non EOF error: %v\n", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package test // import "go.opentelemetry.io/contrib/instrumentation/google.golan

import (
"context"
"errors"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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) {

Check warning on line 227 in instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go#L227

Added line #L227 was not covered by tests
logger.Fatalf("%v failed to complete the empty stream test: %v", stream, err)
}
}
Expand Down Expand Up @@ -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),
})
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {

Check warning on line 370 in instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/google.golang.org/grpc/otelgrpc/internal/test/test_utils.go#L370

Added line #L370 was not covered by tests
// read done.
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package test

import (
"context"
"errors"
"io"
"net"
"strconv"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions instrumentation/net/http/otelhttp/internal/semconv/v1.20.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package semconv // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconv"

import (
"errors"
"io"
"net/http"

Expand Down Expand Up @@ -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()))
}
Expand All @@ -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()))
}
Expand Down

0 comments on commit 2b6025f

Please sign in to comment.