Skip to content

Commit

Permalink
Consolidate tracing options within pipeline options (#21865)
Browse files Browse the repository at this point in the history
Use a shared constant for the tracing namespace attribute.
  • Loading branch information
jhendrixMSFT authored Nov 2, 2023
1 parent 183147b commit ca71847
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions sdk/azcore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes
> These changes affect only code written against previous beta versions of `v1.7.0` and `v1.8.0`
* The function `NewTokenCredential` has been removed from the `fake` package. Use a literal `&fake.TokenCredential{}` instead.
* The field `TracingNamespace` in `runtime.PipelineOptions` has been replaced by `TracingOptions`.

### Bugs Fixed

Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/arm/runtime/policy_trace_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func httpTraceNamespacePolicy(req *policy.Request) (resp *http.Response, err err
if err == nil {
// add the namespace attribute to the current span
span := tracer.SpanFromContext(req.Raw().Context())
span.SetAttributes(tracing.Attribute{Key: "az.namespace", Value: rt.Namespace})
span.SetAttributes(tracing.Attribute{Key: shared.TracingNamespaceAttrName, Value: rt.Namespace})
}
}
return req.Next()
Expand Down
8 changes: 4 additions & 4 deletions sdk/azcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ func NewClient(clientName, moduleVersion string, plOpts runtime.PipelineOptions,
pl := runtime.NewPipeline(mod, moduleVersion, plOpts, options)

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

return &Client{
pl: pl,
tr: tr,
tp: options.TracingProvider,
modVer: moduleVersion,
namespace: plOpts.TracingNamespace,
namespace: plOpts.Tracing.Namespace,
}, nil
}

Expand All @@ -154,7 +154,7 @@ func (c *Client) Tracer() tracing.Tracer {
func (c *Client) WithClientName(clientName string) *Client {
tr := c.tp.NewTracer(clientName, c.modVer)
if tr.Enabled() && c.namespace != "" {
tr.SetAttributes(tracing.Attribute{Key: "az.namespace", Value: c.namespace})
tr.SetAttributes(tracing.Attribute{Key: shared.TracingNamespaceAttrName, Value: c.namespace})
}
return &Client{pl: c.pl, tr: tr, tp: c.tp, modVer: c.modVer, namespace: c.namespace}
}
16 changes: 12 additions & 4 deletions sdk/azcore/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@ func TestNewClientTracingEnabled(t *testing.T) {
defer close()

var attrString string
client, err := NewClient("package.Client", "v1.0.0", runtime.PipelineOptions{TracingNamespace: "Widget.Factory"}, &policy.ClientOptions{
client, err := NewClient("package.Client", "v1.0.0", runtime.PipelineOptions{
Tracing: runtime.TracingOptions{
Namespace: "Widget.Factory",
},
}, &policy.ClientOptions{
TracingProvider: tracing.NewProvider(func(name, version string) tracing.Tracer {
return tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) {
require.NotNil(t, options)
for _, attr := range options.Attributes {
if attr.Key == "az.namespace" {
if attr.Key == shared.TracingNamespaceAttrName {
v, ok := attr.Value.(string)
require.True(t, ok)
attrString = attr.Key + ":" + v
Expand Down Expand Up @@ -180,14 +184,18 @@ func TestClientWithClientName(t *testing.T) {
var clientName string
var modVersion string
var attrString string
client, err := NewClient("module/package.Client", "v1.0.0", runtime.PipelineOptions{TracingNamespace: "Widget.Factory"}, &policy.ClientOptions{
client, err := NewClient("module/package.Client", "v1.0.0", runtime.PipelineOptions{
Tracing: runtime.TracingOptions{
Namespace: "Widget.Factory",
},
}, &policy.ClientOptions{
TracingProvider: tracing.NewProvider(func(name, version string) tracing.Tracer {
clientName = name
modVersion = version
return tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) {
require.NotNil(t, options)
for _, attr := range options.Attributes {
if attr.Key == "az.namespace" {
if attr.Key == shared.TracingNamespaceAttrName {
v, ok := attr.Value.(string)
require.True(t, ok)
attrString = attr.Key + ":" + v
Expand Down
2 changes: 2 additions & 0 deletions sdk/azcore/internal/shared/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (

const BearerTokenPrefix = "Bearer "

const TracingNamespaceAttrName = "az.namespace"

const (
// Module is the name of the calling module used in telemetry data.
Module = "azcore"
Expand Down
10 changes: 8 additions & 2 deletions sdk/azcore/runtime/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ type PipelineOptions struct {
// Each policy is executed once per request, and for each retry of that request.
PerRetry []policy.Policy

// TracingNamespace contains the value to use for the az.namespace span attribute.
TracingNamespace string
// Tracing contains options used to configure distributed tracing.
Tracing TracingOptions
}

// TracingOptions contains tracing options for SDK developers.
type TracingOptions struct {
// Namespace contains the value to use for the az.namespace span attribute.
Namespace string
}

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

0 comments on commit ca71847

Please sign in to comment.