Skip to content

Commit

Permalink
Fix default operation name on ServiceBus requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Liudmila Molkova committed Oct 10, 2018
1 parent 3b8a29e commit 4dce071
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private static void ApplyFunctionResultActivityTags(IEnumerable<KeyValuePair<str
case LogConstants.DurationKey:
if (prop.Value is TimeSpan duration)
{
currentActivity.AddTag(prop.Key, duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
currentActivity.AddTag(LogConstants.FunctionExecutionTimeKey, duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,20 @@ public void Initialize(ITelemetry telemetry)
request.Context.Operation.Name = tag.Value;
request.Name = tag.Value;
break;
case LogConstants.DurationKey:
request.Properties[LogConstants.FunctionExecutionTimeKey] = tag.Value;
break;
default:
request.Properties[tag.Key] = tag.Value;
break;
}
}
}
else // workaround for https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1038
{
if (request.Properties.TryGetValue(LogConstants.NameKey, out var functionName))
{
request.Context.Operation.Name = functionName;
request.Name = functionName;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.DataContracts;
Expand Down Expand Up @@ -78,6 +79,33 @@ await host.GetJobHost()
ValidateServiceBusRequest(sbTriggerRequest, _endpoint, _queueName, nameof(ServiceBusTrigger), operationId, sbOutDependency.Id);
}

[Fact]
public async Task ServiceBusRequestWithoutParent()
{
var sender = new MessageSender(_connectionString, _queueName);
await sender.SendAsync(new Message { Body = Encoding.UTF8.GetBytes("message"), ContentType = "text/plain" });

using (var host = ConfigureHost())
{
await host.StartAsync();

_functionWaitHandle.WaitOne();
await Task.Delay(1000);

await host.StopAsync();
}

List<RequestTelemetry> requests = _channel.Telemetries.OfType<RequestTelemetry>().ToList();
List<DependencyTelemetry> dependencies = _channel.Telemetries.OfType<DependencyTelemetry>().ToList();

Assert.Single(requests);
Assert.Empty(dependencies);

Assert.NotNull(requests.Single().Context.Operation.Id);

ValidateServiceBusRequest(requests.Single(), _endpoint, _queueName, nameof(ServiceBusTrigger), null, null);
}

[NoAutomaticTrigger]
public static void ServiceBusOut(
string input,
Expand Down Expand Up @@ -163,11 +191,10 @@ public void Dispose()
private async Task<int> CleanUpEntity()
{
var messageReceiver = new MessageReceiver(_connectionString, _queueName, ReceiveMode.ReceiveAndDelete);
Message message;
int count = 0;
do
{
message = await messageReceiver.ReceiveAsync(TimeSpan.FromSeconds(3)).ConfigureAwait(false);
var message = await messageReceiver.ReceiveAsync(TimeSpan.FromSeconds(3)).ConfigureAwait(false);
if (message != null)
{
count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ public static void ValidateRequest(
Assert.Equal(LogLevel.Information.ToString(), request.Properties[LogConstants.LogLevelKey]);
Assert.NotNull(request.Name);
Assert.NotNull(request.Id);
Assert.Equal(operationId, request.Context.Operation.Id);

if (operationId != null)
{
Assert.Equal(operationId, request.Context.Operation.Id);
}

if (parentId != null)
{
Assert.Equal(parentId, request.Context.Operation.ParentId);
}
Assert.Equal(operationName, request.Context.Operation.Name);
Assert.Equal(parentId, request.Context.Operation.ParentId);
Assert.True(request.Properties.ContainsKey(LogConstants.InvocationIdKey));
Assert.True(request.Properties.ContainsKey(LogConstants.TriggerReasonKey));
}
Expand Down

0 comments on commit 4dce071

Please sign in to comment.