Skip to content

Commit 7a559b3

Browse files
mmorel-35pellared
andauthored
chore: several rules from go-critic (#7635)
Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Robert Pająk <[email protected]>
1 parent 1eabfaa commit 7a559b3

File tree

62 files changed

+278
-266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+278
-266
lines changed

.golangci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linters:
1010
- depguard
1111
- errcheck
1212
- errorlint
13+
- gocritic
1314
- godot
1415
- gosec
1516
- govet
@@ -50,6 +51,22 @@ linters:
5051
- pkg: crypto/md5
5152
- pkg: crypto/sha1
5253
- pkg: crypto/**/pkix
54+
gocritic:
55+
disabled-checks:
56+
- appendAssign
57+
- appendCombine
58+
- assignOp
59+
- elseif
60+
- exitAfterDefer
61+
- exposedSyncMutex
62+
- hugeParam
63+
- paramTypeCombine
64+
- rangeValCopy
65+
- typeUnparen
66+
- unnamedResult
67+
- unslice
68+
- whyNoLint
69+
enable-all: true
5370
godot:
5471
exclude:
5572
# Exclude sentence fragments for lists.

bridges/otelslog/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func (g *group) KeyValue(kvs ...log.KeyValue) log.KeyValue {
361361
// Clone returns a copy of g.
362362
func (g *group) Clone() *group {
363363
if g == nil {
364-
return g
364+
return nil
365365
}
366366
g2 := *g
367367
g2.attrs = g2.attrs.Clone()

bridges/prometheus/producer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,12 @@ func convertExemplar(exemplar *dto.Exemplar) metricdata.Exemplar[float64] {
367367
var traceID, spanID []byte
368368
// find the trace ID and span ID in attributes, if it exists
369369
for _, label := range exemplar.GetLabel() {
370-
if label.GetName() == traceIDLabel {
370+
switch label.GetName() {
371+
case traceIDLabel:
371372
traceID = []byte(label.GetValue())
372-
} else if label.GetName() == spanIDLabel {
373+
case spanIDLabel:
373374
spanID = []byte(label.GetValue())
374-
} else {
375+
default:
375376
attrs = append(attrs, attribute.String(label.GetName(), label.GetValue()))
376377
}
377378
}

detectors/autodetect/autodetect.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,9 @@ var (
121121
IDAWSEC2: func() resource.Detector {
122122
return ec2.NewResourceDetector()
123123
},
124-
IDAWSECS: func() resource.Detector {
125-
return ecs.NewResourceDetector()
126-
},
127-
IDAWSEKS: func() resource.Detector {
128-
return eks.NewResourceDetector()
129-
},
130-
IDAWSLambda: func() resource.Detector {
131-
return lambda.NewResourceDetector()
132-
},
124+
IDAWSECS: ecs.NewResourceDetector,
125+
IDAWSEKS: eks.NewResourceDetector,
126+
IDAWSLambda: lambda.NewResourceDetector,
133127

134128
IDAzureVM: func() resource.Detector {
135129
return azurevm.New()

detectors/aws/ecs/ecs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc
7373
metadataURIV3 := os.Getenv(metadataV3EnvVar)
7474
metadataURIV4 := os.Getenv(metadataV4EnvVar)
7575

76-
if len(metadataURIV3) == 0 && len(metadataURIV4) == 0 {
76+
if metadataURIV3 == "" && metadataURIV4 == "" {
7777
return nil, nil
7878
}
7979
hostName, err := detector.utils.getContainerName()
@@ -91,7 +91,7 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc
9191
semconv.ContainerID(containerID),
9292
}
9393

94-
if len(metadataURIV4) > 0 {
94+
if metadataURIV4 != "" {
9595
containerMetadata, err := detector.utils.getContainerMetadataV4(ctx)
9696
if err != nil {
9797
return empty, err
@@ -133,7 +133,7 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc
133133
)
134134

135135
availabilityZone := taskMetadata.AvailabilityZone
136-
if len(availabilityZone) > 0 {
136+
if availabilityZone != "" {
137137
attributes = append(
138138
attributes,
139139
semconv.CloudAvailabilityZone(availabilityZone),

detectors/aws/eks/detector.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ var _ resource.Detector = (*resourceDetector)(nil)
5656
// Compile time assertion that eksDetectorUtils implements the detectorUtils interface.
5757
var _ detectorUtils = (*eksDetectorUtils)(nil)
5858

59+
// is this going to stop working with 1.20 when Docker is deprecated?
60+
var containerIDRegex = regexp.MustCompile(`^.*/docker/(.+)$`)
61+
5962
// NewResourceDetector returns a resource detector that will detect AWS EKS resources.
6063
func NewResourceDetector() resource.Detector {
6164
utils, err := newK8sDetectorUtils()
@@ -180,16 +183,10 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) {
180183
return "", fmt.Errorf("getContainerID() error: cannot read file with path %s: %w", defaultCgroupPath, err)
181184
}
182185

183-
// is this going to stop working with 1.20 when Docker is deprecated?
184-
r, err := regexp.Compile(`^.*/docker/(.+)$`)
185-
if err != nil {
186-
return "", err
187-
}
188-
189186
// Retrieve containerID from file
190187
splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n")
191188
for _, str := range splitData {
192-
if r.MatchString(str) {
189+
if containerIDRegex.MatchString(str) {
193190
return str[len(str)-containerIDLength:], nil
194191
}
195192
}

detectors/aws/lambda/detector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func NewResourceDetector() resource.Detector {
4545
func (detector *resourceDetector) Detect(context.Context) (*resource.Resource, error) {
4646
// Lambda resources come from ENV
4747
lambdaName := os.Getenv(lambdaFunctionNameEnvVar)
48-
if len(lambdaName) == 0 {
48+
if lambdaName == "" {
4949
return empty, errNotOnLambda
5050
}
5151
awsRegion := os.Getenv(awsRegionEnvVar)

detectors/azure/azurevm/vm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (detector *ResourceDetector) getJSONMetadata(ctx context.Context) ([]byte,
8989

9090
client := http.Client{Transport: pTransport}
9191

92-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, detector.endpoint, nil)
92+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, detector.endpoint, http.NoBody)
9393
if err != nil {
9494
return nil, false, err
9595
}

examples/passthrough/handler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (h *Handler) HandleHTTPReq(r *http.Request) {
5353
// makeOutgoingRequest mimics what an instrumented http client does.
5454
func (h *Handler) makeOutgoingRequest(ctx context.Context) {
5555
// make a new http request
56-
r, err := http.NewRequest("", "", nil)
56+
r, err := http.NewRequest("", "", http.NoBody)
5757
if err != nil {
5858
panic(err)
5959
}

examples/passthrough/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232
defer func() { _ = tp.Shutdown(ctx) }()
3333

3434
// make an initial http request
35-
r, err := http.NewRequest("", "", nil)
35+
r, err := http.NewRequest("", "", http.NoBody)
3636
if err != nil {
3737
panic(err)
3838
}

0 commit comments

Comments
 (0)