Skip to content

Commit df7503e

Browse files
committed
fix(otel): add nil context handling and remove unused functions
- Added nil context handling to all NewRequest methods to prevent panics - Removed unused helper functions from package-level otel.go files - Fixed linter warnings about unused functions - All packages now build successfully
1 parent 4858e7c commit df7503e

18 files changed

Lines changed: 36 additions & 234 deletions

admin/api_client_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ type Client struct {
9393
// NewRequest creates a new HTTP request with the given context, method, URL string, content type, and body.
9494
// It returns an HTTP request and an error.
9595
func (c *Client) NewRequest(ctx context.Context, method, urlStr, contentType string, body interface{}) (*http.Request, error) {
96+
if ctx == nil {
97+
ctx = context.Background()
98+
}
9699
ctx, span := tracer().Start(ctx, "(*Client).NewRequest")
97100
defer span.End()
98101

admin/otel.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,11 @@ package admin
22

33
import (
44
"go.opentelemetry.io/otel"
5-
"go.opentelemetry.io/otel/attribute"
6-
"go.opentelemetry.io/otel/codes"
75
"go.opentelemetry.io/otel/trace"
86
)
97

108
const tracerName = "github.com/ctreminiom/go-atlassian/v2/admin"
119

1210
func tracer(opts ...trace.TracerOption) trace.Tracer {
1311
return otel.Tracer(tracerName, opts...)
14-
}
15-
16-
// recordError records an error on the span and sets the span status to error
17-
func recordError(span trace.Span, err error) {
18-
if err != nil {
19-
span.RecordError(err)
20-
span.SetStatus(codes.Error, err.Error())
21-
}
22-
}
23-
24-
// setOK sets the span status to OK
25-
func setOK(span trace.Span) {
26-
span.SetStatus(codes.Ok, "")
27-
}
28-
29-
// spanWithKind creates a span with the specified kind
30-
func spanWithKind(kind trace.SpanKind) trace.SpanStartOption {
31-
return trace.WithSpanKind(kind)
32-
}
33-
34-
// addAttributes adds multiple attributes to a span
35-
func addAttributes(span trace.Span, attrs ...attribute.KeyValue) {
36-
span.SetAttributes(attrs...)
37-
}
12+
}

assets/api_client_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ type Client struct {
9696
// NewRequest creates a new HTTP request with the given context, method, URL string, content type, and body.
9797
// It returns an HTTP request and an error.
9898
func (c *Client) NewRequest(ctx context.Context, method, urlStr, contentType string, body interface{}) (*http.Request, error) {
99+
if ctx == nil {
100+
ctx = context.Background()
101+
}
99102
ctx, span := tracer().Start(ctx, "(*Client).NewRequest")
100103
defer span.End()
101104

assets/otel.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,11 @@ package assets
22

33
import (
44
"go.opentelemetry.io/otel"
5-
"go.opentelemetry.io/otel/attribute"
6-
"go.opentelemetry.io/otel/codes"
75
"go.opentelemetry.io/otel/trace"
86
)
97

108
const tracerName = "github.com/ctreminiom/go-atlassian/v2/assets"
119

1210
func tracer(opts ...trace.TracerOption) trace.Tracer {
1311
return otel.Tracer(tracerName, opts...)
14-
}
15-
16-
// recordError records an error on the span and sets the span status to error
17-
func recordError(span trace.Span, err error) {
18-
if err != nil {
19-
span.RecordError(err)
20-
span.SetStatus(codes.Error, err.Error())
21-
}
22-
}
23-
24-
// setOK sets the span status to OK
25-
func setOK(span trace.Span) {
26-
span.SetStatus(codes.Ok, "")
27-
}
28-
29-
// spanWithKind creates a span with the specified kind
30-
func spanWithKind(kind trace.SpanKind) trace.SpanStartOption {
31-
return trace.WithSpanKind(kind)
32-
}
33-
34-
// addAttributes adds multiple attributes to a span
35-
func addAttributes(span trace.Span, attrs ...attribute.KeyValue) {
36-
span.SetAttributes(attrs...)
37-
}
12+
}

bitbucket/api_client_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type Client struct {
7878

7979
// NewRequest creates an API request.
8080
func (c *Client) NewRequest(ctx context.Context, method, urlStr, typ string, body interface{}) (*http.Request, error) {
81+
if ctx == nil {
82+
ctx = context.Background()
83+
}
8184
ctx, span := tracer().Start(ctx, "(*Client).NewRequest")
8285
defer span.End()
8386

bitbucket/otel.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,11 @@ package bitbucket
22

33
import (
44
"go.opentelemetry.io/otel"
5-
"go.opentelemetry.io/otel/attribute"
6-
"go.opentelemetry.io/otel/codes"
75
"go.opentelemetry.io/otel/trace"
86
)
97

108
const tracerName = "github.com/ctreminiom/go-atlassian/v2/bitbucket"
119

1210
func tracer(opts ...trace.TracerOption) trace.Tracer {
1311
return otel.Tracer(tracerName, opts...)
14-
}
15-
16-
// recordError records an error on the span and sets the span status to error
17-
func recordError(span trace.Span, err error) {
18-
if err != nil {
19-
span.RecordError(err)
20-
span.SetStatus(codes.Error, err.Error())
21-
}
22-
}
23-
24-
// setOK sets the span status to OK
25-
func setOK(span trace.Span) {
26-
span.SetStatus(codes.Ok, "")
27-
}
28-
29-
// spanWithKind creates a span with the specified kind
30-
func spanWithKind(kind trace.SpanKind) trace.SpanStartOption {
31-
return trace.WithSpanKind(kind)
32-
}
33-
34-
// addAttributes adds multiple attributes to a span
35-
func addAttributes(span trace.Span, attrs ...attribute.KeyValue) {
36-
span.SetAttributes(attrs...)
37-
}
12+
}

confluence/api_client_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ type Client struct {
9494
}
9595

9696
func (c *Client) NewRequest(ctx context.Context, method, urlStr, contentType string, body interface{}) (*http.Request, error) {
97+
if ctx == nil {
98+
ctx = context.Background()
99+
}
97100
ctx, span := tracer().Start(ctx, "(*Client).NewRequest")
98101
defer span.End()
99102

confluence/otel.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,11 @@ package confluence
22

33
import (
44
"go.opentelemetry.io/otel"
5-
"go.opentelemetry.io/otel/attribute"
6-
"go.opentelemetry.io/otel/codes"
75
"go.opentelemetry.io/otel/trace"
86
)
97

108
const tracerName = "github.com/ctreminiom/go-atlassian/v2/confluence"
119

1210
func tracer(opts ...trace.TracerOption) trace.Tracer {
1311
return otel.Tracer(tracerName, opts...)
14-
}
15-
16-
// recordError records an error on the span and sets the span status to error
17-
func recordError(span trace.Span, err error) {
18-
if err != nil {
19-
span.RecordError(err)
20-
span.SetStatus(codes.Error, err.Error())
21-
}
22-
}
23-
24-
// setOK sets the span status to OK
25-
func setOK(span trace.Span) {
26-
span.SetStatus(codes.Ok, "")
27-
}
28-
29-
// spanWithKind creates a span with the specified kind
30-
func spanWithKind(kind trace.SpanKind) trace.SpanStartOption {
31-
return trace.WithSpanKind(kind)
32-
}
33-
34-
// addAttributes adds multiple attributes to a span
35-
func addAttributes(span trace.Span, attrs ...attribute.KeyValue) {
36-
span.SetAttributes(attrs...)
37-
}
12+
}

confluence/v2/api_client_impl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ type Client struct {
7474
}
7575

7676
func (c *Client) NewRequest(ctx context.Context, method, urlStr, contentType string, body interface{}) (*http.Request, error) {
77+
if ctx == nil {
78+
ctx = context.Background()
79+
}
7780
ctx, span := tracer().Start(ctx, "(*Client).NewRequest")
7881
defer span.End()
7982

confluence/v2/otel.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,11 @@ package v2
22

33
import (
44
"go.opentelemetry.io/otel"
5-
"go.opentelemetry.io/otel/attribute"
6-
"go.opentelemetry.io/otel/codes"
75
"go.opentelemetry.io/otel/trace"
86
)
97

108
const tracerName = "github.com/ctreminiom/go-atlassian/v2/confluence/v2"
119

1210
func tracer(opts ...trace.TracerOption) trace.Tracer {
1311
return otel.Tracer(tracerName, opts...)
14-
}
15-
16-
// recordError records an error on the span and sets the span status to error
17-
func recordError(span trace.Span, err error) {
18-
if err != nil {
19-
span.RecordError(err)
20-
span.SetStatus(codes.Error, err.Error())
21-
}
22-
}
23-
24-
// setOK sets the span status to OK
25-
func setOK(span trace.Span) {
26-
span.SetStatus(codes.Ok, "")
27-
}
28-
29-
// spanWithKind creates a span with the specified kind
30-
func spanWithKind(kind trace.SpanKind) trace.SpanStartOption {
31-
return trace.WithSpanKind(kind)
32-
}
33-
34-
// addAttributes adds multiple attributes to a span
35-
func addAttributes(span trace.Span, attrs ...attribute.KeyValue) {
36-
span.SetAttributes(attrs...)
37-
}
12+
}

0 commit comments

Comments
 (0)