Skip to content

Commit

Permalink
Use storage account in destination.service.resource
Browse files Browse the repository at this point in the history
  • Loading branch information
russcam committed Apr 30, 2021
1 parent 3b17744 commit b73a9b2
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 44 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
6 changes: 3 additions & 3 deletions src/Elastic.Apm.MongoDb/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ Apache License
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

limitations under the License.
==========
Elastic.Apm.MongoDb
----------

The code for Elastic.Apm.MongoDb is based on the elastic-apm-mongo project by Vadim Hatsura (@vhatsura),
licensed under the Apache 2.0 License. https://github.com/vhatsura/elastic-apm-mongo
licensed under the Apache 2.0 License. https://github.com/vhatsura/elastic-apm-mongo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void AssertSpan(string action, string resource)

destination.Address.Should().Be(_environment.StorageAccountConnectionStringProperties.FileUrl);
destination.Service.Name.Should().Be(AzureFileStorage.SubType);
destination.Service.Resource.Should().Be($"{AzureFileStorage.SubType}/{resource}");
destination.Service.Resource.Should().Be($"{AzureFileStorage.SubType}/{_environment.StorageAccountConnectionStringProperties.AccountName}");
destination.Service.Type.Should().Be(ApiConstants.TypeStorage);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected void AssertSpan(string action, string resource, int count = 1)

destination.Address.Should().Be(Environment.StorageAccountConnectionStringProperties.BlobUrl);
destination.Service.Name.Should().Be(AzureBlobStorage.SubType);
destination.Service.Resource.Should().Be($"{AzureBlobStorage.SubType}/{resource}");
destination.Service.Resource.Should().Be($"{AzureBlobStorage.SubType}/{Environment.StorageAccountConnectionStringProperties.AccountName}");
destination.Service.Type.Should().Be(ApiConstants.TypeStorage);
}

Expand Down

0 comments on commit b73a9b2

Please sign in to comment.