Skip to content

Commit f0f13a1

Browse files
committed
Add Host Instance ID to AppInsights logs
1 parent bfedd10 commit f0f13a1

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.ApplicationInsights.Channel;
6+
using Microsoft.ApplicationInsights.Extensibility;
7+
using Microsoft.Extensions.Options;
8+
9+
namespace Microsoft.Azure.WebJobs.Script.Config
10+
{
11+
internal class ScriptTelemetryInitializer : ITelemetryInitializer
12+
{
13+
private readonly ScriptJobHostOptions _hostOptions;
14+
15+
public ScriptTelemetryInitializer(IOptions<ScriptJobHostOptions> hostOptions)
16+
{
17+
if (hostOptions == null)
18+
{
19+
throw new ArgumentNullException(nameof(hostOptions));
20+
}
21+
22+
if (hostOptions.Value == null)
23+
{
24+
throw new ArgumentNullException(nameof(hostOptions.Value));
25+
}
26+
27+
_hostOptions = hostOptions.Value;
28+
}
29+
30+
public void Initialize(ITelemetry telemetry)
31+
{
32+
telemetry?.Context?.Properties?.Add(ScriptConstants.LogPropertyHostInstanceIdKey, _hostOptions.InstanceId);
33+
}
34+
}
35+
}

src/WebJobs.Script/ScriptHostBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ internal static void ConfigureApplicationInsights(HostBuilderContext context, IL
243243

244244
builder.Services.AddSingleton<ISdkVersionProvider, FunctionsSdkVersionProvider>();
245245

246+
builder.Services.AddSingleton<ITelemetryInitializer, ScriptTelemetryInitializer>();
247+
246248
if (SystemEnvironment.Instance.IsPlaceholderModeEnabled())
247249
{
248250
for (int i = 0; i < builder.Services.Count; i++)

test/WebJobs.Script.Tests.Integration/ApplicationInsights/ApplicationInsightsEndToEndTestsBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,20 +355,20 @@ public void Validate_BeginScope()
355355
// Every telemetry will have {originalFormat}, Category, Level, but we validate those elsewhere.
356356
// We're only interested in the custom properties.
357357
Assert.Equal("1", traces[0].Message);
358-
Assert.Equal(5, traces[0].Properties.Count);
358+
Assert.Equal(6, traces[0].Properties.Count);
359359
Assert.Equal("customValue1", traces[0].Properties["prop__customKey1"]);
360360

361361
Assert.Equal("2", traces[1].Message);
362-
Assert.Equal(6, traces[1].Properties.Count);
362+
Assert.Equal(7, traces[1].Properties.Count);
363363
Assert.Equal("customValue1", traces[1].Properties["prop__customKey1"]);
364364
Assert.Equal("customValue2", traces[1].Properties["prop__customKey2"]);
365365

366366
Assert.Equal("3", traces[2].Message);
367-
Assert.Equal(5, traces[2].Properties.Count);
367+
Assert.Equal(6, traces[2].Properties.Count);
368368
Assert.Equal("customValue1", traces[2].Properties["prop__customKey1"]);
369369

370370
Assert.Equal("4", traces[3].Message);
371-
Assert.Equal(4, traces[3].Properties.Count);
371+
Assert.Equal(5, traces[3].Properties.Count);
372372
}
373373
}
374374

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.ApplicationInsights.DataContracts;
6+
using Microsoft.Azure.WebJobs.Script.Config;
7+
using Microsoft.Extensions.Options;
8+
using Xunit;
9+
10+
namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
11+
{
12+
public class ScriptTelemetryInitializerTests
13+
{
14+
[Fact]
15+
public void Add_Host_Instance_Id_Test()
16+
{
17+
var telemetry = new RequestTelemetry
18+
{
19+
Url = new Uri("https://localhost/api/function?name=World"),
20+
ResponseCode = "200",
21+
Name = "Function Request"
22+
};
23+
24+
var jobHostOptions = new ScriptJobHostOptions();
25+
var hostInstanceID = jobHostOptions.InstanceId;
26+
27+
var initializer = new ScriptTelemetryInitializer(new OptionsWrapper<ScriptJobHostOptions>(jobHostOptions));
28+
initializer.Initialize(telemetry);
29+
30+
Assert.True(telemetry.Context.Properties.TryGetValue(ScriptConstants.LogPropertyHostInstanceIdKey, out string telemetryHostId));
31+
Assert.Equal(hostInstanceID, telemetryHostId);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)