Skip to content

Commit ca71847

Browse files
authored
Consolidate tracing options within pipeline options (#21865)
Use a shared constant for the tracing namespace attribute.
1 parent 183147b commit ca71847

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

sdk/azcore/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88
> These changes affect only code written against previous beta versions of `v1.7.0` and `v1.8.0`
99
* The function `NewTokenCredential` has been removed from the `fake` package. Use a literal `&fake.TokenCredential{}` instead.
10+
* The field `TracingNamespace` in `runtime.PipelineOptions` has been replaced by `TracingOptions`.
1011

1112
### Bugs Fixed
1213

sdk/azcore/arm/runtime/policy_trace_namespace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func httpTraceNamespacePolicy(req *policy.Request) (resp *http.Response, err err
2323
if err == nil {
2424
// add the namespace attribute to the current span
2525
span := tracer.SpanFromContext(req.Raw().Context())
26-
span.SetAttributes(tracing.Attribute{Key: "az.namespace", Value: rt.Namespace})
26+
span.SetAttributes(tracing.Attribute{Key: shared.TracingNamespaceAttrName, Value: rt.Namespace})
2727
}
2828
}
2929
return req.Next()

sdk/azcore/core.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ func NewClient(clientName, moduleVersion string, plOpts runtime.PipelineOptions,
125125
pl := runtime.NewPipeline(mod, moduleVersion, plOpts, options)
126126

127127
tr := options.TracingProvider.NewTracer(client, moduleVersion)
128-
if tr.Enabled() && plOpts.TracingNamespace != "" {
129-
tr.SetAttributes(tracing.Attribute{Key: "az.namespace", Value: plOpts.TracingNamespace})
128+
if tr.Enabled() && plOpts.Tracing.Namespace != "" {
129+
tr.SetAttributes(tracing.Attribute{Key: shared.TracingNamespaceAttrName, Value: plOpts.Tracing.Namespace})
130130
}
131131

132132
return &Client{
133133
pl: pl,
134134
tr: tr,
135135
tp: options.TracingProvider,
136136
modVer: moduleVersion,
137-
namespace: plOpts.TracingNamespace,
137+
namespace: plOpts.Tracing.Namespace,
138138
}, nil
139139
}
140140

@@ -154,7 +154,7 @@ func (c *Client) Tracer() tracing.Tracer {
154154
func (c *Client) WithClientName(clientName string) *Client {
155155
tr := c.tp.NewTracer(clientName, c.modVer)
156156
if tr.Enabled() && c.namespace != "" {
157-
tr.SetAttributes(tracing.Attribute{Key: "az.namespace", Value: c.namespace})
157+
tr.SetAttributes(tracing.Attribute{Key: shared.TracingNamespaceAttrName, Value: c.namespace})
158158
}
159159
return &Client{pl: c.pl, tr: tr, tp: c.tp, modVer: c.modVer, namespace: c.namespace}
160160
}

sdk/azcore/core_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,16 @@ func TestNewClientTracingEnabled(t *testing.T) {
143143
defer close()
144144

145145
var attrString string
146-
client, err := NewClient("package.Client", "v1.0.0", runtime.PipelineOptions{TracingNamespace: "Widget.Factory"}, &policy.ClientOptions{
146+
client, err := NewClient("package.Client", "v1.0.0", runtime.PipelineOptions{
147+
Tracing: runtime.TracingOptions{
148+
Namespace: "Widget.Factory",
149+
},
150+
}, &policy.ClientOptions{
147151
TracingProvider: tracing.NewProvider(func(name, version string) tracing.Tracer {
148152
return tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) {
149153
require.NotNil(t, options)
150154
for _, attr := range options.Attributes {
151-
if attr.Key == "az.namespace" {
155+
if attr.Key == shared.TracingNamespaceAttrName {
152156
v, ok := attr.Value.(string)
153157
require.True(t, ok)
154158
attrString = attr.Key + ":" + v
@@ -180,14 +184,18 @@ func TestClientWithClientName(t *testing.T) {
180184
var clientName string
181185
var modVersion string
182186
var attrString string
183-
client, err := NewClient("module/package.Client", "v1.0.0", runtime.PipelineOptions{TracingNamespace: "Widget.Factory"}, &policy.ClientOptions{
187+
client, err := NewClient("module/package.Client", "v1.0.0", runtime.PipelineOptions{
188+
Tracing: runtime.TracingOptions{
189+
Namespace: "Widget.Factory",
190+
},
191+
}, &policy.ClientOptions{
184192
TracingProvider: tracing.NewProvider(func(name, version string) tracing.Tracer {
185193
clientName = name
186194
modVersion = version
187195
return tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) {
188196
require.NotNil(t, options)
189197
for _, attr := range options.Attributes {
190-
if attr.Key == "az.namespace" {
198+
if attr.Key == shared.TracingNamespaceAttrName {
191199
v, ok := attr.Value.(string)
192200
require.True(t, ok)
193201
attrString = attr.Key + ":" + v

sdk/azcore/internal/shared/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131

3232
const BearerTokenPrefix = "Bearer "
3333

34+
const TracingNamespaceAttrName = "az.namespace"
35+
3436
const (
3537
// Module is the name of the calling module used in telemetry data.
3638
Module = "azcore"

sdk/azcore/runtime/pipeline.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ type PipelineOptions struct {
3434
// Each policy is executed once per request, and for each retry of that request.
3535
PerRetry []policy.Policy
3636

37-
// TracingNamespace contains the value to use for the az.namespace span attribute.
38-
TracingNamespace string
37+
// Tracing contains options used to configure distributed tracing.
38+
Tracing TracingOptions
39+
}
40+
41+
// TracingOptions contains tracing options for SDK developers.
42+
type TracingOptions struct {
43+
// Namespace contains the value to use for the az.namespace span attribute.
44+
Namespace string
3945
}
4046

4147
// Pipeline represents a primitive for sending HTTP requests and receiving responses.

0 commit comments

Comments
 (0)