Skip to content

Commit

Permalink
Add identity client distributed tracing instrumentation (Azure#6972)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Jul 19, 2019
1 parent 0e369a2 commit 2bc2990
Show file tree
Hide file tree
Showing 7 changed files with 543 additions and 383 deletions.
49 changes: 45 additions & 4 deletions sdk/identity/Azure.Identity/src/AadIdentityClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ internal class AadIdentityClient

private const string AuthenticationRequestFailedError = "The request to the identity service failed. See inner exception for details.";

protected AadIdentityClient()
{
}

public AadIdentityClient(IdentityClientOptions options = null)
{
_options = options ?? new IdentityClientOptions();
Expand All @@ -42,8 +46,12 @@ public AadIdentityClient(IdentityClientOptions options = null)

public virtual async Task<AccessToken> AuthenticateAsync(string tenantId, string clientId, string clientSecret, string[] scopes, CancellationToken cancellationToken = default)
{
using (Request request = CreateClientSecretAuthRequest(tenantId, clientId, clientSecret, scopes))
using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Identity.AadIdentityClient.Authenticate");
scope.Start();

try
{
using Request request = CreateClientSecretAuthRequest(tenantId, clientId, clientSecret, scopes);
try
{
return await SendAuthRequestAsync(request, cancellationToken).ConfigureAwait(false);
Expand All @@ -53,12 +61,21 @@ public virtual async Task<AccessToken> AuthenticateAsync(string tenantId, string
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

public virtual AccessToken Authenticate(string tenantId, string clientId, string clientSecret, string[] scopes, CancellationToken cancellationToken = default)
{
using (Request request = CreateClientSecretAuthRequest(tenantId, clientId, clientSecret, scopes))
using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Identity.AadIdentityClient.Authenticate");
scope.Start();

try
{
using Request request = CreateClientSecretAuthRequest(tenantId, clientId, clientSecret, scopes);
try
{
return SendAuthRequest(request, cancellationToken);
Expand All @@ -68,12 +85,21 @@ public virtual AccessToken Authenticate(string tenantId, string clientId, string
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

public virtual async Task<AccessToken> AuthenticateAsync(string tenantId, string clientId, X509Certificate2 clientCertificate, string[] scopes, CancellationToken cancellationToken = default)
{
using (Request request = CreateClientCertificateAuthRequest(tenantId, clientId, clientCertificate, scopes))
using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Identity.AadIdentityClient.Authenticate");
scope.Start();

try
{
using Request request = CreateClientCertificateAuthRequest(tenantId, clientId, clientCertificate, scopes);
try
{
return await SendAuthRequestAsync(request, cancellationToken).ConfigureAwait(false);
Expand All @@ -83,12 +109,21 @@ public virtual async Task<AccessToken> AuthenticateAsync(string tenantId, string
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

public virtual AccessToken Authenticate(string tenantId, string clientId, X509Certificate2 clientCertificate, string[] scopes, CancellationToken cancellationToken = default)
{
using (Request request = CreateClientCertificateAuthRequest(tenantId, clientId, clientCertificate, scopes))
using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Identity.AadIdentityClient.Authenticate");
scope.Start();

try
{
using Request request = CreateClientCertificateAuthRequest(tenantId, clientId, clientCertificate, scopes);
try
{
return SendAuthRequest(request, cancellationToken);
Expand All @@ -98,7 +133,13 @@ public virtual AccessToken Authenticate(string tenantId, string clientId, X509Ce
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

private async Task<AccessToken> SendAuthRequestAsync(Request request, CancellationToken cancellationToken)
{
var response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
Expand Down
38 changes: 32 additions & 6 deletions sdk/identity/Azure.Identity/src/ManagedIdentityClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ internal class ManagedIdentityClient
private readonly IdentityClientOptions _options;
private readonly HttpPipeline _pipeline;

protected ManagedIdentityClient()
{
}

public ManagedIdentityClient(IdentityClientOptions options = null)
{
_options = options ?? new IdentityClientOptions();
Expand Down Expand Up @@ -66,13 +70,24 @@ public virtual async Task<AccessToken> AuthenticateAsync(string[] scopes, string
return default;
}

using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Identity.ManagedIdentityClient.Authenticate");
scope.Start();

try
{
return await SendAuthRequestAsync(msiType, scopes, clientId, cancellationToken).ConfigureAwait(false);
try
{
return await SendAuthRequestAsync(msiType, scopes, clientId, cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex)
{
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
}
}
catch (RequestFailedException ex)
catch (Exception e)
{
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
scope.Failed(e);
throw;
}
}

Expand All @@ -86,13 +101,24 @@ public virtual AccessToken Authenticate(string[] scopes, string clientId = null,
return default;
}

using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Identity.ManagedIdentityClient.Authenticate");
scope.Start();

try
{
return SendAuthRequest(msiType, scopes, clientId, cancellationToken);
try
{
return SendAuthRequest(msiType, scopes, clientId, cancellationToken);
}
catch(RequestFailedException ex)
{
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
}
}
catch(RequestFailedException ex)
catch (Exception e)
{
throw new AuthenticationFailedException(AuthenticationRequestFailedError, ex);
scope.Failed(e);
throw;
}
}

Expand Down
1 change: 1 addition & 0 deletions sdk/identity/Azure.Identity/src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Azure.Identity.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Loading

0 comments on commit 2bc2990

Please sign in to comment.