Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[azservicebus] Enable distributed tracing #23860

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
feffdf8
add internal tracing wrapper and fake tracer for UT
karenychen Dec 11, 2024
2f4a2b2
set up tracer in SB client and traces in sender methods
karenychen Dec 11, 2024
7c2e4ce
add more unit tests
karenychen Dec 11, 2024
23d7e94
linting
karenychen Dec 11, 2024
1be63f7
move matcher to sdk/internal folder and add callback function for sta…
karenychen Dec 11, 2024
b026074
address comments and moved startspan snippet to retrier layer
karenychen Dec 12, 2024
3d07a0a
reverting some files
karenychen Dec 12, 2024
1bb68ed
added receiver traces and some UT
karenychen Dec 12, 2024
e653088
add session traces
karenychen Dec 12, 2024
992d9b9
linting
karenychen Dec 12, 2024
af668bf
reverting some files
karenychen Dec 12, 2024
eaf0389
Merge remote-tracking branch 'origin/main' into karenchen/azservicebu…
karenychen Jan 13, 2025
bd50b2a
linting
karenychen Jan 13, 2025
ab81102
move tracer to sender/receiver level, refractor SetAttrFn patter, add…
karenychen Jan 17, 2025
09f971e
include span kind in our spans and tests + more unit test
karenychen Jan 17, 2025
14c6576
add internal tracing for NegotiateClaim
karenychen Jan 17, 2025
aa0f561
Merge remote-tracking branch 'origin/main' into karenchen/azservicebu…
karenychen Feb 18, 2025
338275d
pushing staged changes that were verified in live test
karenychen Feb 18, 2025
b9631d9
clean up
karenychen Feb 18, 2025
d2953cd
clean up
karenychen Feb 18, 2025
0f6cf28
Merge remote-tracking branch 'origin/main' into karenchen/azservicebu…
karenychen Feb 24, 2025
3eed895
merge with main changes and fix ut
karenychen Feb 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions sdk/internal/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module github.com/Azure/azure-sdk-for-go/sdk/internal

go 1.18
go 1.22.7

toolchain go1.23.1

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.0
github.com/stretchr/testify v1.10.0
golang.org/x/net v0.33.0
golang.org/x/net v0.34.0
golang.org/x/text v0.21.0
)

Expand All @@ -20,8 +23,14 @@ require (
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
go.opentelemetry.io/otel/trace v1.33.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/sys v0.29.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/Azure/azure-sdk-for-go/sdk/azcore => github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.1-0.20250213005623-521dc9bd0933

replace github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel => github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.1-0.20250214223549-8a6d539f1772
60 changes: 52 additions & 8 deletions sdk/internal/go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 h1:JZg6HRh6W6U4OLl6lk7BZ7BLisIzM9dG1R50zUk9C/M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0/go.mod h1:YL1xnZ6QejvQHWJrX/AvhFl4WW4rqHVoKspWNVwFk0M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.1-0.20250213005623-521dc9bd0933 h1:w3PuIQw+HSZY7174TvDH33muZ7/sxO5Dw5VmahwsqyI=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.1-0.20250213005623-521dc9bd0933/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 h1:B/dfvscEQtew9dVuoxqxrUKKv8Ih2f55PydknDamU+g=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0/go.mod h1:fiPSssYvltE08HJchL04dOy+RD4hgrjph0cwGGMntdI=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0 h1:+m0M/LFxN43KvULkDNfdXOgrjtg6UYJPFBJyuEcRCAw=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0/go.mod h1:PwOyop78lveYMRs6oCxjiVyBdyCgIYH6XHIVZO9/SFQ=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0 h1:Dd+RhdJn0OTtVGaeDLZpcumkIVCtA/3/Fo42+eoYvVM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0/go.mod h1:5kakwfW5CjC9KK+Q4wjXAg+ShuIm2mBMua0ZFj2C8PE=
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.1-0.20250214223549-8a6d539f1772 h1:zCaxa0u9BdlFJ7Fe8GKz6JewrSDZXWNRIyqsO1SXvfI=
github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel v0.4.1-0.20250214223549-8a6d539f1772/go.mod h1:YulGnXodu+bjzDCkRYGS4onU8upR0Ncd0rCm7TvLahU=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 h1:kYRSnvJju5gYVyhkij+RTJ/VR6QIUaCfWeaFm2ycsjQ=
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 h1:TmHmbvxPmaegwhDubVz0lICL0J5Ka2vwTzhoePEXsGE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0/go.mod h1:qztMSjm835F2bXf+5HKAPIS5qsmQDqZna/PgVt4rWtI=
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs=
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6/go.mod h1:3VeWNIJaW+O5xpRQbPp0Ybqu1vJd/pm7s2F473HRrkw=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -31,20 +50,45 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 h1:Vh5HayB/0HHfOQA7Ctx69E/Y/DcQSMPpKANYVMQ7fBA=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0/go.mod h1:cpgtDBaqD/6ok/UG0jT15/uKjAY8mRA53diogHBg3UI=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.33.0 h1:wpMfgF8E1rkrT1Z6meFh1NDtownE9Ii3n3X2GJYjsaU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.33.0/go.mod h1:wAy0T/dUbs468uOlkT31xjvqQgEVXv58BRFWEgn5v/0=
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
go.opentelemetry.io/otel/sdk v1.33.0 h1:iax7M131HuAm9QkZotNHEfstof92xM+N8sr3uHXc2IM=
go.opentelemetry.io/otel/sdk v1.33.0/go.mod h1:A1Q5oi7/9XaMlIWzPSxLRWOI8nG3FnzHJNbiENQuihM=
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
go.opentelemetry.io/proto/otlp v1.4.0 h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1jgeg=
go.opentelemetry.io/proto/otlp v1.4.0/go.mod h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/genproto/googleapis/api v0.0.0-20241219184827-bd154493cd20 h1:VtcT0lutoLuZk634hxhu2lc6ryh5pmn1pPOTuWReKZI=
google.golang.org/genproto/googleapis/api v0.0.0-20241219184827-bd154493cd20/go.mod h1:KRUmxRI4JmbpAm8gcZM4Jsffi859fo5LQjILwuqj9z8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241219184827-bd154493cd20 h1:aiR3J9AR6kgxKXDbsWpnjcNbN8dsYMAw6IJVl40rzX8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241219184827-bd154493cd20/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
127 changes: 127 additions & 0 deletions sdk/internal/test/tracing/validator.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a span validator used to validate spans in unit tests. I will make a separate PR on it but putting it here for reference

Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package tracing

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing"
"github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel"
"github.com/stretchr/testify/require"
)

type Span = tracing.Span
type SpanStatus = tracing.SpanStatus

// NewSpanValidator creates a Provider that verifies a span was created that matches the specified SpanMatcher.
// The returned Provider can be used to create a client with a tracing provider that will validate spans in unit tests.
func NewSpanValidator(t *testing.T, matcher SpanMatcher) tracing.Provider {
return tracing.NewProvider(func(name, version string) tracing.Tracer {
tt := matchingTracer{
matcher: matcher,
}

t.Cleanup(func() {
require.NotNil(t, tt.match, "didn't find a span with name %s", tt.matcher.Name)
require.True(t, tt.match.ended, "span wasn't ended")
if tt.matcher.Kind != 0 {
require.EqualValues(t, tt.matcher.Kind, tt.match.kind, "span kind values don't match")
}
require.EqualValues(t, matcher.Status, tt.match.status, "span status values don't match")
require.ElementsMatch(t, matcher.Attributes, tt.match.attrs, "span attributes don't match")
require.ElementsMatch(t, matcher.Links, tt.match.links, "span links don't match")
})

return tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, Span) {
kind := tracing.SpanKindInternal
attrs := []tracing.Attribute{}
links := []tracing.Link{}
if options != nil {
kind = options.Kind
attrs = append(attrs, options.Attributes...)
links = append(links, options.Links...)
}
return tt.Start(ctx, spanName, kind, attrs, links)
}, &tracing.TracerOptions{
SpanFromContext: func(ctx context.Context) Span {
return convertSpan(tt.match)
},
LinkFromContext: func(ctx context.Context, attrs ...tracing.Attribute) tracing.Link {
return tracing.Link{Attributes: attrs}
},
})
}, &tracing.ProviderOptions{
// use the wrapped propagation.TraceContext propagator
NewPropagatorFn: func() tracing.Propagator {
return azotel.NewTracingProvider(nil, nil).NewPropagator()
},
})
}

// SpanMatcher contains the values to match when a span is created.
type SpanMatcher struct {
Name string
Kind tracing.SpanKind
Status SpanStatus
Attributes []tracing.Attribute
Links []tracing.Link
}

type matchingTracer struct {
matcher SpanMatcher
match *span
}

func (mt *matchingTracer) Start(ctx context.Context, spanName string, kind tracing.SpanKind, attrs []tracing.Attribute, links []tracing.Link) (context.Context, Span) {
if spanName != mt.matcher.Name {
return ctx, Span{}
}
// span name matches our matcher, track it
mt.match = &span{
name: spanName,
kind: kind,
attrs: attrs,
links: links,
}

return ctx, convertSpan(mt.match)
}

func convertSpan(sp *span) Span {
return tracing.NewSpan(tracing.SpanImpl{
End: sp.End,
SetStatus: sp.SetStatus,
SetAttributes: sp.SetAttributes,
AddLink: sp.AddLink,
})
}

type span struct {
name string
kind tracing.SpanKind
status SpanStatus
attrs []tracing.Attribute
links []tracing.Link
desc string
ended bool
}

func (s *span) End() {
s.ended = true
}

func (s *span) SetAttributes(attrs ...tracing.Attribute) {
s.attrs = append(s.attrs, attrs...)
}

func (s *span) AddLink(link tracing.Link) {
s.links = append(s.links, link)
}

func (s *span) SetStatus(code SpanStatus, desc string) {
s.status = code
s.desc = desc
s.ended = true
}
69 changes: 69 additions & 0 deletions sdk/internal/test/tracing/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package tracing

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing"
"github.com/stretchr/testify/require"
)

func TestNewSpanValidator(t *testing.T) {
provider := NewSpanValidator(t, SpanMatcher{
Name: "TestSpan",
Kind: tracing.SpanKindClient,
Status: tracing.SpanStatusUnset,
Attributes: []tracing.Attribute{
{Key: "testKey", Value: "testValue"},
},
})
tracer := provider.NewTracer("module", "version")
require.NotNil(t, tracer)
require.True(t, tracer.Enabled())

ctx, endSpan := runtime.StartSpan(context.Background(), "TestSpan", tracer, &runtime.StartSpanOptions{
Kind: tracing.SpanKindClient,
Attributes: []tracing.Attribute{
{Key: "testKey", Value: "testValue"},
},
})
defer func() { endSpan(nil) }()

require.NotNil(t, tracer.SpanFromContext(ctx))
require.NotNil(t, tracer.LinkFromContext(ctx))
}

func TestMatchingTracerStart(t *testing.T) {
matcher := SpanMatcher{
Name: "TestSpan",
Kind: tracing.SpanKindProducer,
Status: tracing.SpanStatusUnset,
Attributes: []tracing.Attribute{
{Key: "testKey1", Value: "testValue1"},
{Key: "testKey2", Value: "testValue2"},
},
}
tracer := matchingTracer{
matcher: matcher,
}
ctx := context.Background()
// no-op when SpanName doesn't match
_, spn := tracer.Start(ctx, "BadSpanName", tracing.SpanKindProducer, nil, nil)
require.EqualValues(t, spn, tracing.Span{})
// tracks span when SpanName matches
_, spn = tracer.Start(ctx, "TestSpan", tracing.SpanKindProducer, []tracing.Attribute{
{Key: "testKey1", Value: "testValue1"},
{Key: "testKey2", Value: "testValue2"},
}, nil)
require.NotNil(t, spn)
spn.SetAttributes(tracing.Attribute{
Key: "TestAttributeKey",
Value: "TestAttributeValue",
})
spn.AddLink(tracing.Link{})
spn.SetStatus(tracing.SpanStatusOK, "ok")
}
Loading
Loading