Skip to content

Commit

Permalink
Use storage account in destination.service.resource (#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
russcam authored May 26, 2021
1 parent f47798b commit bd080ee
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private void OnStart(KeyValuePair<string, object> kv, string action)
Service = new Destination.DestinationService
{
Name = AzureBlobStorage.SubType,
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.ResourceName}",
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.StorageAccountName}",
Type = ApiConstants.TypeStorage
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void OnStart(KeyValuePair<string, object> kv, string action)
}

var urlTag = activity.Tags.FirstOrDefault(t => t.Key == "url").Value;
var fileShareUrl = new FileShareUrl(urlTag);
var fileShareUrl = new FileShareUrl(new Uri(urlTag));
var spanName = $"{AzureFileStorage.SpanName} {action} {fileShareUrl.ResourceName}";

var span = currentSegment.StartSpan(spanName, ApiConstants.TypeStorage, AzureFileStorage.SubType, action);
Expand All @@ -112,7 +112,7 @@ private void OnStart(KeyValuePair<string, object> kv, string action)
Service = new Destination.DestinationService
{
Name = AzureFileStorage.SubType,
Resource = $"{AzureFileStorage.SubType}/{fileShareUrl.ResourceName}",
Resource = $"{AzureFileStorage.SubType}/{fileShareUrl.StorageAccountName}",
Type = ApiConstants.TypeStorage
}
};
Expand Down Expand Up @@ -175,17 +175,9 @@ private void OnException(KeyValuePair<string, object> kv)
segment.End();
}

private class FileShareUrl
private class FileShareUrl : StorageUrl
{
public FileShareUrl(string url)
{
var builder = new UriBuilder(url);

FullyQualifiedNamespace = builder.Uri.GetLeftPart(UriPartial.Authority) + "/";
ResourceName = builder.Uri.AbsolutePath.TrimStart('/');
}

public string FullyQualifiedNamespace { get; }
public FileShareUrl(Uri url) : base(url) => ResourceName = url.AbsolutePath.TrimStart('/');

public string ResourceName { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private void OnSendStart(KeyValuePair<string, object> kv)
string destinationAddress = null;

var urlTag = activity.Tags.FirstOrDefault(t => t.Key == "url").Value;
if (!string.IsNullOrEmpty(urlTag))
if (!string.IsNullOrEmpty(urlTag) && Uri.TryCreate(urlTag, UriKind.Absolute, out var url))
{
var queueUrl = new QueueUrl(urlTag);
var queueUrl = new QueueUrl(url);
queueName = queueUrl.QueueName;
destinationAddress = queueUrl.FullyQualifiedNamespace;
}
Expand Down Expand Up @@ -146,8 +146,8 @@ private void OnReceiveStart(KeyValuePair<string, object> kv)
}

var urlTag = activity.Tags.FirstOrDefault(t => t.Key == "url").Value;
var queueName = !string.IsNullOrEmpty(urlTag)
? new QueueUrl(urlTag).QueueName
var queueName = !string.IsNullOrEmpty(urlTag) && Uri.TryCreate(urlTag, UriKind.Absolute, out var url)
? new QueueUrl(url).QueueName
: null;

if (MatchesIgnoreMessageQueues(queueName))
Expand Down Expand Up @@ -243,20 +243,12 @@ private void OnException(KeyValuePair<string, object> kv)
/// <summary>
/// Working with a queue url to extract the queue name and address.
/// </summary>
private class QueueUrl
private class QueueUrl : StorageUrl
{
public QueueUrl(string url)
{
var builder = new UriBuilder(url);

FullyQualifiedNamespace = builder.Uri.GetLeftPart(UriPartial.Authority) + "/";

QueueName = builder.Uri.Segments.Length > 1
? builder.Uri.Segments[1].TrimEnd('/')
public QueueUrl(Uri url) : base(url) =>
QueueName = url.Segments.Length > 1
? url.Segments[1].TrimEnd('/')
: null;
}

public string FullyQualifiedNamespace { get; }

public string QueueName { get; }
}
Expand Down
22 changes: 14 additions & 8 deletions src/Elastic.Apm.Azure.Storage/BlobUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@

namespace Elastic.Apm.Azure.Storage
{
internal class BlobUrl
internal class BlobUrl : StorageUrl
{
public BlobUrl(Uri url)
{
var builder = new UriBuilder(url);

FullyQualifiedNamespace = builder.Uri.GetLeftPart(UriPartial.Authority) + "/";
ResourceName = builder.Uri.AbsolutePath.TrimStart('/');
}
public BlobUrl(Uri url) : base(url) => ResourceName = url.AbsolutePath.TrimStart('/');

public BlobUrl(string url) : this(new Uri(url))
{
}

public string ResourceName { get; }
}

internal abstract class StorageUrl
{
private static char[] SplitDomain = { '.' };

protected StorageUrl(Uri url)
{
StorageAccountName = url.Host.Split(SplitDomain, 2)[0];
FullyQualifiedNamespace = url.GetLeftPart(UriPartial.Authority) + "/";
}

public string StorageAccountName { get; }
public string FullyQualifiedNamespace { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public ISpan StartSpan(IApmAgent agent, string method, Uri requestUrl, Func<stri
return null;

var name = $"{AzureBlobStorage.SpanName} {action} {blobUrl.ResourceName}";

var span = ExecutionSegmentCommon.StartSpanOnCurrentExecutionSegment(agent, name,
ApiConstants.TypeStorage, AzureBlobStorage.SubType, InstrumentationFlag.Azure, true);
span.Action = action;
Expand All @@ -119,7 +118,7 @@ public ISpan StartSpan(IApmAgent agent, string method, Uri requestUrl, Func<stri
Service = new Destination.DestinationService
{
Name = AzureBlobStorage.SubType,
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.ResourceName}",
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.StorageAccountName}",
Type = ApiConstants.TypeStorage
}
};
Expand Down
Loading

0 comments on commit bd080ee

Please sign in to comment.