@@ -27,9 +27,10 @@ const (
2727
2828// Client is used to interact with the Azure Cosmos DB database service.
2929type Client struct {
30- endpoint string
31- pipeline azruntime.Pipeline
32- gem * globalEndpointManager
30+ endpoint string
31+ internal * azcore.Client
32+ gem * globalEndpointManager
33+ endpointUrl * url.URL
3334}
3435
3536// Endpoint used to create the client.
@@ -42,24 +43,38 @@ func (c *Client) Endpoint() string {
4243// cred - The credential used to authenticate with the cosmos service.
4344// options - Optional Cosmos client options. Pass nil to accept default values.
4445func NewClientWithKey (endpoint string , cred KeyCredential , o * ClientOptions ) (* Client , error ) {
46+ endpointUrl , err := url .Parse (endpoint )
47+ if err != nil {
48+ return nil , err
49+ }
4550 preferredRegions := []string {}
4651 enableCrossRegionRetries := true
4752 if o != nil {
4853 preferredRegions = o .PreferredRegions
4954 }
55+
5056 gem , err := newGlobalEndpointManager (endpoint , newInternalPipeline (newSharedKeyCredPolicy (cred ), o ), preferredRegions , 0 , enableCrossRegionRetries )
5157 if err != nil {
5258 return nil , err
5359 }
54- return & Client {endpoint : endpoint , pipeline : newPipeline (newSharedKeyCredPolicy (cred ), gem , o ), gem : gem }, nil
60+
61+ internalClient , err := newClient (newSharedKeyCredPolicy (cred ), gem , o )
62+ if err != nil {
63+ return nil , err
64+ }
65+ return & Client {endpoint : endpoint , endpointUrl : endpointUrl , internal : internalClient , gem : gem }, nil
5566}
5667
5768// NewClient creates a new instance of Cosmos client with Azure AD access token authentication. It uses the default pipeline configuration.
5869// endpoint - The cosmos service endpoint to use.
5970// cred - The credential used to authenticate with the cosmos service.
6071// options - Optional Cosmos client options. Pass nil to accept default values.
6172func NewClient (endpoint string , cred azcore.TokenCredential , o * ClientOptions ) (* Client , error ) {
62- scope , err := createScopeFromEndpoint (endpoint )
73+ endpointUrl , err := url .Parse (endpoint )
74+ if err != nil {
75+ return nil , err
76+ }
77+ scope , err := createScopeFromEndpoint (endpointUrl )
6378 if err != nil {
6479 return nil , err
6580 }
@@ -72,7 +87,12 @@ func NewClient(endpoint string, cred azcore.TokenCredential, o *ClientOptions) (
7287 if err != nil {
7388 return nil , err
7489 }
75- return & Client {endpoint : endpoint , pipeline : newPipeline (newCosmosBearerTokenPolicy (cred , scope , nil ), gem , o ), gem : gem }, nil
90+
91+ internalClient , err := newClient (newCosmosBearerTokenPolicy (cred , scope , nil ), gem , o )
92+ if err != nil {
93+ return nil , err
94+ }
95+ return & Client {endpoint : endpoint , endpointUrl : endpointUrl , internal : internalClient , gem : gem }, nil
7696}
7797
7898// NewClientFromConnectionString creates a new instance of Cosmos client from connection string. It uses the default pipeline configuration.
@@ -111,11 +131,11 @@ func NewClientFromConnectionString(connectionString string, o *ClientOptions) (*
111131 return NewClientWithKey (endpoint , cred , o )
112132}
113133
114- func newPipeline (authPolicy policy.Policy , gem * globalEndpointManager , options * ClientOptions ) azruntime. Pipeline {
134+ func newClient (authPolicy policy.Policy , gem * globalEndpointManager , options * ClientOptions ) ( * azcore. Client , error ) {
115135 if options == nil {
116136 options = & ClientOptions {}
117137 }
118- return azruntime . NewPipeline ( "azcosmos" , serviceLibVersion ,
138+ return azcore . NewClient ( moduleName , serviceLibVersion ,
119139 azruntime.PipelineOptions {
120140 AllowedHeaders : getAllowedHeaders (),
121141 PerCall : []policy.Policy {
@@ -128,6 +148,9 @@ func newPipeline(authPolicy policy.Policy, gem *globalEndpointManager, options *
128148 authPolicy ,
129149 & clientRetryPolicy {gem : gem },
130150 },
151+ Tracing : azruntime.TracingOptions {
152+ Namespace : "Microsoft.DocumentDB" ,
153+ },
131154 },
132155 & options .ClientOptions )
133156}
@@ -136,7 +159,7 @@ func newInternalPipeline(authPolicy policy.Policy, options *ClientOptions) azrun
136159 if options == nil {
137160 options = & ClientOptions {}
138161 }
139- return azruntime .NewPipeline ("azcosmos" , serviceLibVersion ,
162+ return azruntime .NewPipeline (moduleName , serviceLibVersion ,
140163 azruntime.PipelineOptions {
141164 AllowedHeaders : getAllowedHeaders (),
142165 PerRetry : []policy.Policy {
@@ -146,13 +169,8 @@ func newInternalPipeline(authPolicy policy.Policy, options *ClientOptions) azrun
146169 & options .ClientOptions )
147170}
148171
149- func createScopeFromEndpoint (endpoint string ) ([]string , error ) {
150- u , err := url .Parse (endpoint )
151- if err != nil {
152- return nil , err
153- }
154-
155- return []string {fmt .Sprintf ("%s://%s/.default" , u .Scheme , u .Hostname ())}, nil
172+ func createScopeFromEndpoint (endpoint * url.URL ) ([]string , error ) {
173+ return []string {fmt .Sprintf ("%s://%s/.default" , endpoint .Scheme , endpoint .Hostname ())}, nil
156174}
157175
158176// NewDatabase returns a struct that represents a database and allows database level operations.
@@ -193,6 +211,14 @@ func (c *Client) CreateDatabase(
193211 ctx context.Context ,
194212 databaseProperties DatabaseProperties ,
195213 o * CreateDatabaseOptions ) (DatabaseResponse , error ) {
214+ var err error
215+ spanName , err := getSpanNameForDatabases (c .accountEndpointUrl (), operationTypeCreate , resourceTypeDatabase , databaseProperties .ID )
216+ if err != nil {
217+ return DatabaseResponse {}, err
218+ }
219+ ctx , endSpan := azruntime .StartSpan (ctx , spanName .name , c .internal .Tracer (), & spanName .options )
220+ defer func () { endSpan (err ) }()
221+
196222 if o == nil {
197223 o = & CreateDatabaseOptions {}
198224 }
@@ -224,7 +250,8 @@ func (c *Client) CreateDatabase(
224250 return DatabaseResponse {}, err
225251 }
226252
227- return newDatabaseResponse (azResponse )
253+ response , err := newDatabaseResponse (azResponse )
254+ return response , err
228255}
229256
230257// NewQueryDatabasesPager executes query for databases.
@@ -249,6 +276,14 @@ func (c *Client) NewQueryDatabasesPager(query string, o *QueryDatabasesOptions)
249276 return page .ContinuationToken != nil
250277 },
251278 Fetcher : func (ctx context.Context , page * QueryDatabasesResponse ) (QueryDatabasesResponse , error ) {
279+ var err error
280+ // Move the span to the pager once https://github.com/Azure/azure-sdk-for-go/issues/23294 is fixed
281+ spanName , err := getSpanNameForClient (c .accountEndpointUrl (), operationTypeQuery , resourceTypeDatabase , c .accountEndpointUrl ().Hostname ())
282+ if err != nil {
283+ return QueryDatabasesResponse {}, err
284+ }
285+ ctx , endSpan := azruntime .StartSpan (ctx , spanName .name , c .internal .Tracer (), & spanName .options )
286+ defer func () { endSpan (err ) }()
252287 if page != nil {
253288 if page .ContinuationToken != nil {
254289 // Use the previous page continuation if available
@@ -271,6 +306,7 @@ func (c *Client) NewQueryDatabasesPager(query string, o *QueryDatabasesOptions)
271306
272307 return newDatabasesQueryResponse (azResponse )
273308 },
309+ Tracer : c .internal .Tracer (),
274310 })
275311}
276312
@@ -473,7 +509,7 @@ func (c *Client) attachContent(content interface{}, req *policy.Request) error {
473509
474510func (c * Client ) executeAndEnsureSuccessResponse (request * policy.Request ) (* http.Response , error ) {
475511 log .Write (azlog .EventResponse , fmt .Sprintf ("\n ===== Client preferred regions:\n %v\n =====\n " , c .gem .preferredLocations ))
476- response , err := c .pipeline .Do (request )
512+ response , err := c .internal . Pipeline () .Do (request )
477513 if err != nil {
478514 return nil , err
479515 }
@@ -486,6 +522,10 @@ func (c *Client) executeAndEnsureSuccessResponse(request *policy.Request) (*http
486522 return nil , azruntime .NewResponseErrorWithErrorCode (response , response .Status )
487523}
488524
525+ func (c * Client ) accountEndpointUrl () * url.URL {
526+ return c .endpointUrl
527+ }
528+
489529type pipelineRequestOptions struct {
490530 headerOptionsOverride * headerOptionsOverride
491531 resourceType resourceType
0 commit comments