1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-05-22 09:35:21 +02:00

Fix receiver-naming issues from revive (#8093)

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
Matthieu MOREL
2026-05-22 09:01:15 +02:00
committed by GitHub
parent 6ceeae804a
commit f6c8baa895
7 changed files with 63 additions and 52 deletions
+9 -5
View File
@@ -134,13 +134,16 @@ linters:
strconcat: true
revive:
confidence: 0.01
enable-all-rules: false
enable-default-rules: true
max-open-files: 2048
rules:
- name: blank-imports
- name: bool-literal-in-expr
- name: constant-logical-expr
- name: context-as-argument
arguments:
- allowTypesBefore: '*testing.T'
- allow-types-before: '*testing.T'
disabled: true
- name: context-keys-type
- name: deep-exit
@@ -152,7 +155,7 @@ linters:
- name: duplicated-imports
- name: early-return
arguments:
- preserveScope
- preserve-scope
- name: empty-block
- name: empty-lines
- name: error-naming
@@ -161,7 +164,7 @@ linters:
- name: errorf
- name: exported
arguments:
- sayRepetitiveInsteadOfStutters
- say-repetitive-instead-of-stutters
- name: flag-parameter
- name: identical-branches
- name: if-return
@@ -169,11 +172,12 @@ linters:
- name: increment-decrement
- name: indent-error-flow
arguments:
- preserveScope
- preserve-scope
- name: package-comments
- name: range
- name: range-val-in-closure
- name: range-val-address
- name: receiver-naming
- name: redefines-builtin-id
- name: string-format
arguments:
@@ -183,7 +187,7 @@ linters:
- name: struct-tag
- name: superfluous-else
arguments:
- preserveScope
- preserve-scope
- name: time-equal
- name: unconditional-recursion
- name: unexported-return
@@ -143,9 +143,9 @@ func (c *client) Start(ctx context.Context) error {
}
// Stop shuts down the client and interrupt any in-flight request.
func (d *client) Stop(ctx context.Context) error {
d.stopOnce.Do(func() {
close(d.stopCh)
func (c *client) Stop(ctx context.Context) error {
c.stopOnce.Do(func() {
close(c.stopCh)
})
select {
case <-ctx.Done():
@@ -156,7 +156,7 @@ func (d *client) Stop(ctx context.Context) error {
}
// UploadTraces sends a batch of spans to the collector.
func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.ResourceSpans) (uploadErr error) {
func (c *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.ResourceSpans) (uploadErr error) {
pbRequest := &coltracepb.ExportTraceServiceRequest{
ResourceSpans: protoSpans,
}
@@ -165,31 +165,31 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc
return err
}
ctx, cancel := d.contextWithStop(ctx)
ctx, cancel := c.contextWithStop(ctx)
defer cancel()
if maxSize := d.cfg.MaxRequestSize; maxSize > 0 && len(rawRequest) > maxSize {
if maxSize := c.cfg.MaxRequestSize; maxSize > 0 && len(rawRequest) > maxSize {
return fmt.Errorf("request body too large: exceeded %d bytes", maxSize)
}
request, err := d.newRequest(rawRequest)
request, err := c.newRequest(rawRequest)
if err != nil {
return err
}
var statusCode int
if d.inst != nil {
if c.inst != nil {
var spanCount int
for _, rs := range protoSpans {
for _, ss := range rs.ScopeSpans {
spanCount += len(ss.Spans)
}
}
op := d.inst.ExportSpans(ctx, spanCount)
op := c.inst.ExportSpans(ctx, spanCount)
defer func() { op.End(uploadErr, statusCode) }()
}
return errors.Join(uploadErr, d.requestFunc(ctx, func(ctx context.Context) error {
return errors.Join(uploadErr, c.requestFunc(ctx, func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
@@ -199,7 +199,7 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc
statusCode = 0
request.reset(ctx)
// nolint:gosec // URL is constructed from validated OTLP endpoint configuration
resp, err := d.client.Do(request.Request)
resp, err := c.client.Do(request.Request)
var urlErr *url.Error
if errors.As(err, &urlErr) && urlErr.Temporary() {
return newResponseError(http.Header{}, err)
@@ -283,8 +283,8 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc
}))
}
func (d *client) newRequest(body []byte) (request, error) {
u := url.URL{Scheme: d.getScheme(), Host: d.cfg.Endpoint, Path: d.cfg.URLPath}
func (c *client) newRequest(body []byte) (request, error) {
u := url.URL{Scheme: c.getScheme(), Host: c.cfg.Endpoint, Path: c.cfg.URLPath}
r, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u.String(), http.NoBody)
if err != nil {
return request{Request: r}, err
@@ -293,13 +293,13 @@ func (d *client) newRequest(body []byte) (request, error) {
userAgent := "OTel OTLP Exporter Go/" + otlptrace.Version()
r.Header.Set("User-Agent", userAgent)
for k, v := range d.cfg.Headers {
for k, v := range c.cfg.Headers {
r.Header.Set(k, v)
}
r.Header.Set("Content-Type", contentTypeProto)
req := request{Request: r}
switch Compression(d.cfg.Compression) {
switch Compression(c.cfg.Compression) {
case NoCompression:
r.ContentLength = int64(len(body))
req.bodyReader = bodyReader(body)
@@ -334,15 +334,15 @@ func (d *client) newRequest(body []byte) (request, error) {
}
// MarshalLog is the marshaling function used by the logging system to represent this Client.
func (d *client) MarshalLog() any {
func (c *client) MarshalLog() any {
return struct {
Type string
Endpoint string
Insecure bool
}{
Type: "otlptracehttp",
Endpoint: d.cfg.Endpoint,
Insecure: d.cfg.Insecure,
Endpoint: c.cfg.Endpoint,
Insecure: c.cfg.Insecure,
}
}
@@ -439,14 +439,14 @@ func evaluate(err error) (bool, time.Duration) {
return true, time.Duration(rErr.throttle)
}
func (d *client) getScheme() string {
if d.cfg.Insecure {
func (c *client) getScheme() string {
if c.cfg.Insecure {
return "http"
}
return "https"
}
func (d *client) contextWithStop(ctx context.Context) (context.Context, context.CancelFunc) {
func (c *client) contextWithStop(ctx context.Context) (context.Context, context.CancelFunc) {
// Unify the parent context Done signal with the client's stop
// channel.
ctx, cancel := context.WithCancel(ctx)
@@ -455,7 +455,7 @@ func (d *client) contextWithStop(ctx context.Context) (context.Context, context.
case <-ctx.Done():
// Nothing to do, either cancelled or deadline
// happened.
case <-d.stopCh:
case <-c.stopCh:
cancel()
}
}(ctx, cancel)
+8 -5
View File
@@ -7,9 +7,9 @@ package decls // import "go.opentelemetry.io/otel/internal/tools/semconvkit/decl
import (
"go/ast"
"go/parser"
"go/token"
"strings"
"golang.org/x/tools/go/packages"
)
// GetNames parses the Go source code in the specified package path and returns
@@ -20,15 +20,18 @@ import (
// lowercased form of the name and the values are the original format of the
// name.
func GetNames(pkgPath string, f Parser) (Names, error) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, pkgPath, nil, 0)
cfg := &packages.Config{
Mode: packages.NeedSyntax | packages.NeedFiles,
Dir: pkgPath,
}
pkgs, err := packages.Load(cfg, ".")
if err != nil {
return nil, err
}
out := make(Names)
for _, pkg := range pkgs {
for _, file := range pkg.Files {
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
for _, name := range f(decl) {
out[NewCanonicalName(name)] = Name(name)
+1 -1
View File
@@ -197,7 +197,7 @@ type SemanticConventions struct {
TagVer string
}
func (sc SemanticConventions) SemVer() string {
func (SemanticConventions) SemVer() string {
return strings.TrimPrefix(*tag, "v")
}
+14 -10
View File
@@ -6,6 +6,7 @@
package main
import (
"errors"
"fmt"
"io/fs"
"os"
@@ -23,13 +24,13 @@ var excludedDirs = []string{
const readmeFilename = "README.md"
// verifyReadme is a [os.WalkFunc] that checks if a README.md exists in the same directory as the go.mod file.
func verifyReadme(path string, info os.FileInfo, err error) error {
// verifyReadme is a [fs.WalkDirFunc] that checks if a README.md exists in the same directory as the go.mod file.
func verifyReadme(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() || info.Name() != "go.mod" {
if !d.Type().IsRegular() || d.Name() != "go.mod" {
return nil
}
@@ -43,7 +44,7 @@ func verifyReadme(path string, info os.FileInfo, err error) error {
readme := filepath.Join(filepath.Dir(path), readmeFilename)
_, err = os.Stat(readme)
if os.IsNotExist(err) {
err = fmt.Errorf("couldn't find %s for %q", readmeFilename, filepath.Dir(path))
return fmt.Errorf("couldn't find %s for %q", readmeFilename, filepath.Dir(path))
}
return err
@@ -60,19 +61,22 @@ func main() {
os.Exit(1)
}
// Clean the path to prevent path traversal issues
root = filepath.Clean(root)
fmt.Println("Verifying READMEs in", root)
var errs []string
filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err := verifyReadme(path, info, err); err != nil {
errs = append(errs, err.Error())
var errs []error
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err := verifyReadme(path, d, err); err != nil {
errs = append(errs, err)
}
return nil // continue walking
})
if len(errs) > 0 {
if err := errors.Join(errs...); err != nil {
fmt.Println("Some readme files couldn't be found.")
fmt.Println(strings.Join(errs, "\n"))
fmt.Println(err.Error())
os.Exit(1)
}
}
+5 -5
View File
@@ -169,17 +169,17 @@ func (mr *ManualReader) Collect(ctx context.Context, rm *metricdata.ResourceMetr
}
// MarshalLog returns logging data about the ManualReader.
func (r *ManualReader) MarshalLog() any {
r.mu.Lock()
down := r.isShutdown
r.mu.Unlock()
func (mr *ManualReader) MarshalLog() any {
mr.mu.Lock()
down := mr.isShutdown
mr.mu.Unlock()
return struct {
Type string
Registered bool
Shutdown bool
}{
Type: "ManualReader",
Registered: r.sdkProducer.Load() != nil,
Registered: mr.sdkProducer.Load() != nil,
Shutdown: down,
}
}
+4 -4
View File
@@ -314,9 +314,9 @@ type SpanEvent struct {
}
// MarshalJSON encodes e into OTLP formatted JSON.
func (e SpanEvent) MarshalJSON() ([]byte, error) {
t := e.Time.UnixNano()
if e.Time.IsZero() || t < 0 {
func (se SpanEvent) MarshalJSON() ([]byte, error) {
t := se.Time.UnixNano()
if se.Time.IsZero() || t < 0 {
t = 0
}
@@ -325,7 +325,7 @@ func (e SpanEvent) MarshalJSON() ([]byte, error) {
Alias
Time uint64 `json:"timeUnixNano,omitempty"`
}{
Alias: Alias(e),
Alias: Alias(se),
Time: uint64(t), // nolint: gosec // >0 checked above
})
}