Skip to content

Commit b73a9b2

Browse files
committed
Use storage account in destination.service.resource
Relates: elastic/apm#426
1 parent 3b17744 commit b73a9b2

File tree

8 files changed

+33
-44
lines changed

8 files changed

+33
-44
lines changed

src/Elastic.Apm.Azure.Storage/AzureBlobStorageDiagnosticListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private void OnStart(KeyValuePair<string, object> kv, string action)
212212
Service = new Destination.DestinationService
213213
{
214214
Name = AzureBlobStorage.SubType,
215-
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.ResourceName}",
215+
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.StorageAccountName}",
216216
Type = ApiConstants.TypeStorage
217217
}
218218
};

src/Elastic.Apm.Azure.Storage/AzureFileShareStorageDiagnosticListener.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void OnStart(KeyValuePair<string, object> kv, string action)
9999
}
100100

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

105105
var span = currentSegment.StartSpan(spanName, ApiConstants.TypeStorage, AzureFileStorage.SubType, action);
@@ -112,7 +112,7 @@ private void OnStart(KeyValuePair<string, object> kv, string action)
112112
Service = new Destination.DestinationService
113113
{
114114
Name = AzureFileStorage.SubType,
115-
Resource = $"{AzureFileStorage.SubType}/{fileShareUrl.ResourceName}",
115+
Resource = $"{AzureFileStorage.SubType}/{fileShareUrl.StorageAccountName}",
116116
Type = ApiConstants.TypeStorage
117117
}
118118
};
@@ -175,17 +175,9 @@ private void OnException(KeyValuePair<string, object> kv)
175175
segment.End();
176176
}
177177

178-
private class FileShareUrl
178+
private class FileShareUrl : StorageUrl
179179
{
180-
public FileShareUrl(string url)
181-
{
182-
var builder = new UriBuilder(url);
183-
184-
FullyQualifiedNamespace = builder.Uri.GetLeftPart(UriPartial.Authority) + "/";
185-
ResourceName = builder.Uri.AbsolutePath.TrimStart('/');
186-
}
187-
188-
public string FullyQualifiedNamespace { get; }
180+
public FileShareUrl(Uri url) : base(url) => ResourceName = url.AbsolutePath.TrimStart('/');
189181

190182
public string ResourceName { get; }
191183
}

src/Elastic.Apm.Azure.Storage/AzureQueueStorageDiagnosticListener.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ private void OnSendStart(KeyValuePair<string, object> kv)
9797
string destinationAddress = null;
9898

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

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

153153
if (MatchesIgnoreMessageQueues(queueName))
@@ -243,20 +243,12 @@ private void OnException(KeyValuePair<string, object> kv)
243243
/// <summary>
244244
/// Working with a queue url to extract the queue name and address.
245245
/// </summary>
246-
private class QueueUrl
246+
private class QueueUrl : StorageUrl
247247
{
248-
public QueueUrl(string url)
249-
{
250-
var builder = new UriBuilder(url);
251-
252-
FullyQualifiedNamespace = builder.Uri.GetLeftPart(UriPartial.Authority) + "/";
253-
254-
QueueName = builder.Uri.Segments.Length > 1
255-
? builder.Uri.Segments[1].TrimEnd('/')
248+
public QueueUrl(Uri url) : base(url) =>
249+
QueueName = url.Segments.Length > 1
250+
? url.Segments[1].TrimEnd('/')
256251
: null;
257-
}
258-
259-
public string FullyQualifiedNamespace { get; }
260252

261253
public string QueueName { get; }
262254
}

src/Elastic.Apm.Azure.Storage/BlobUrl.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@
77

88
namespace Elastic.Apm.Azure.Storage
99
{
10-
internal class BlobUrl
10+
internal class BlobUrl : StorageUrl
1111
{
12-
public BlobUrl(Uri url)
13-
{
14-
var builder = new UriBuilder(url);
15-
16-
FullyQualifiedNamespace = builder.Uri.GetLeftPart(UriPartial.Authority) + "/";
17-
ResourceName = builder.Uri.AbsolutePath.TrimStart('/');
18-
}
12+
public BlobUrl(Uri url) : base(url) => ResourceName = url.AbsolutePath.TrimStart('/');
1913

2014
public BlobUrl(string url) : this(new Uri(url))
2115
{
2216
}
2317

2418
public string ResourceName { get; }
19+
}
20+
21+
internal abstract class StorageUrl
22+
{
23+
private static char[] SplitDomain = { '.' };
24+
25+
protected StorageUrl(Uri url)
26+
{
27+
StorageAccountName = url.Host.Split(SplitDomain, 2)[0];
28+
FullyQualifiedNamespace = url.GetLeftPart(UriPartial.Authority) + "/";
29+
}
2530

31+
public string StorageAccountName { get; }
2632
public string FullyQualifiedNamespace { get; }
2733
}
2834
}

src/Elastic.Apm.Azure.Storage/MicrosoftAzureBlobStorageTracer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public ISpan StartSpan(IApmAgent agent, string method, Uri requestUrl, Func<stri
109109
return null;
110110

111111
var name = $"{AzureBlobStorage.SpanName} {action} {blobUrl.ResourceName}";
112-
113112
var span = ExecutionSegmentCommon.StartSpanOnCurrentExecutionSegment(agent, name,
114113
ApiConstants.TypeStorage, AzureBlobStorage.SubType, InstrumentationFlag.Azure, true);
115114
span.Action = action;
@@ -119,7 +118,7 @@ public ISpan StartSpan(IApmAgent agent, string method, Uri requestUrl, Func<stri
119118
Service = new Destination.DestinationService
120119
{
121120
Name = AzureBlobStorage.SubType,
122-
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.ResourceName}",
121+
Resource = $"{AzureBlobStorage.SubType}/{blobUrl.StorageAccountName}",
123122
Type = ApiConstants.TypeStorage
124123
}
125124
};

src/Elastic.Apm.MongoDb/LICENSE

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ Apache License
198198
distributed under the License is distributed on an "AS IS" BASIS,
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201-
limitations under the License.
202-
201+
limitations under the License.
202+
203203
==========
204204
Elastic.Apm.MongoDb
205205
----------
206206

207207
The code for Elastic.Apm.MongoDb is based on the elastic-apm-mongo project by Vadim Hatsura (@vhatsura),
208-
licensed under the Apache 2.0 License. https://github.com/vhatsura/elastic-apm-mongo
208+
licensed under the Apache 2.0 License. https://github.com/vhatsura/elastic-apm-mongo

test/Elastic.Apm.Azure.Storage.Tests/AzureFileShareStorageDiagnosticListenerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private void AssertSpan(string action, string resource)
178178

179179
destination.Address.Should().Be(_environment.StorageAccountConnectionStringProperties.FileUrl);
180180
destination.Service.Name.Should().Be(AzureFileStorage.SubType);
181-
destination.Service.Resource.Should().Be($"{AzureFileStorage.SubType}/{resource}");
181+
destination.Service.Resource.Should().Be($"{AzureFileStorage.SubType}/{_environment.StorageAccountConnectionStringProperties.AccountName}");
182182
destination.Service.Type.Should().Be(ApiConstants.TypeStorage);
183183
}
184184
}

test/Elastic.Apm.Azure.Storage.Tests/BlobStorageTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected void AssertSpan(string action, string resource, int count = 1)
4545

4646
destination.Address.Should().Be(Environment.StorageAccountConnectionStringProperties.BlobUrl);
4747
destination.Service.Name.Should().Be(AzureBlobStorage.SubType);
48-
destination.Service.Resource.Should().Be($"{AzureBlobStorage.SubType}/{resource}");
48+
destination.Service.Resource.Should().Be($"{AzureBlobStorage.SubType}/{Environment.StorageAccountConnectionStringProperties.AccountName}");
4949
destination.Service.Type.Should().Be(ApiConstants.TypeStorage);
5050
}
5151

0 commit comments

Comments
 (0)