-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add supporting features to enable distributed tracing
This includes new internal pipeline policies and other supporting types. See the changelog for a full description. Added some missing doc comments.
- Loading branch information
1 parent
265c1a6
commit 1fe803d
Showing
18 changed files
with
657 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/internal/resource" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" | ||
) | ||
|
||
// httpTraceNamespacePolicy is a policy that adds the az.namespace attribute to the current Span | ||
func httpTraceNamespacePolicy(req *policy.Request) (resp *http.Response, err error) { | ||
rawTracer := req.Raw().Context().Value(shared.CtxWithTracingTracer{}) | ||
if tracer, ok := rawTracer.(tracing.Tracer); ok { | ||
rt, err := resource.ParseResourceType(req.Raw().URL.Path) | ||
if err == nil { | ||
// add the namespace attribute to the current span | ||
if span, ok := tracer.SpanFromContext(req.Raw().Context()); ok { | ||
span.SetAttributes(tracing.Attribute{Key: "az.namespace", Value: rt.Namespace}) | ||
} | ||
} | ||
} | ||
return req.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/exported" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" | ||
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHTTPTraceNamespacePolicy(t *testing.T) { | ||
srv, close := mock.NewServer() | ||
defer close() | ||
|
||
pl := exported.NewPipeline(srv, exported.PolicyFunc(httpTraceNamespacePolicy)) | ||
|
||
// no tracer | ||
req, err := exported.NewRequest(context.Background(), http.MethodGet, srv.URL()) | ||
require.NoError(t, err) | ||
srv.AppendResponse() | ||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
|
||
// wrong tracer type | ||
req, err = exported.NewRequest(context.WithValue(context.Background(), shared.CtxWithTracingTracer{}, 0), http.MethodGet, srv.URL()) | ||
require.NoError(t, err) | ||
srv.AppendResponse() | ||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
|
||
// no SpanFromContext impl | ||
tr := tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) { | ||
return ctx, tracing.Span{} | ||
}, nil) | ||
req, err = exported.NewRequest(context.WithValue(context.Background(), shared.CtxWithTracingTracer{}, tr), http.MethodGet, srv.URL()) | ||
require.NoError(t, err) | ||
srv.AppendResponse() | ||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
|
||
// failed to parse resource ID, shouldn't call SetAttributes | ||
var attrString string | ||
tr = tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) { | ||
return ctx, tracing.Span{} | ||
}, &tracing.TracerOptions{ | ||
SpanFromContext: func(ctx context.Context) (tracing.Span, bool) { | ||
spanImpl := tracing.SpanImpl{ | ||
SetAttributes: func(a ...tracing.Attribute) { | ||
require.Len(t, a, 1) | ||
v, ok := a[0].Value.(string) | ||
require.True(t, ok) | ||
attrString = a[0].Key + ":" + v | ||
}, | ||
} | ||
return tracing.NewSpan(spanImpl), true | ||
}, | ||
}) | ||
req, err = exported.NewRequest(context.WithValue(context.Background(), shared.CtxWithTracingTracer{}, tr), http.MethodGet, srv.URL()) | ||
require.NoError(t, err) | ||
srv.AppendResponse() | ||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
require.Empty(t, attrString) | ||
|
||
// success | ||
tr = tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) { | ||
return ctx, tracing.Span{} | ||
}, &tracing.TracerOptions{ | ||
SpanFromContext: func(ctx context.Context) (tracing.Span, bool) { | ||
spanImpl := tracing.SpanImpl{ | ||
SetAttributes: func(a ...tracing.Attribute) { | ||
require.Len(t, a, 1) | ||
v, ok := a[0].Value.(string) | ||
require.True(t, ok) | ||
attrString = a[0].Key + ":" + v | ||
}, | ||
} | ||
return tracing.NewSpan(spanImpl), true | ||
}, | ||
}) | ||
req, err = exported.NewRequest(context.WithValue(context.Background(), shared.CtxWithTracingTracer{}, tr), http.MethodGet, srv.URL()+requestEndpoint) | ||
require.NoError(t, err) | ||
srv.AppendResponse() | ||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
require.EqualValues(t, "az.namespace:Microsoft.Storage", attrString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.