-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding FunctionActivitySource to Worker; refactoring AppInsights supp…
…ort (#1307)
- Loading branch information
Showing
23 changed files
with
399 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/DotNetWorker.ApplicationInsights/FunctionActivitySource.cs
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/DotNetWorker.ApplicationInsights/FunctionActivitySourceMiddleware.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 7 additions & 55 deletions
62
src/DotNetWorker.ApplicationInsights/FunctionsTelemetryInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,42 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.Extensibility.Implementation; | ||
|
||
namespace Microsoft.Azure.Functions.Worker.ApplicationInsights | ||
{ | ||
internal class FunctionsTelemetryInitializer : ITelemetryInitializer | ||
{ | ||
private const string NameKey = "Name"; | ||
private readonly string? _sdkVersion; | ||
|
||
private readonly string _sdkVersion; | ||
private readonly string _roleInstanceName; | ||
|
||
internal FunctionsTelemetryInitializer(string sdkVersion, string roleInstanceName) | ||
internal FunctionsTelemetryInitializer(string? sdkVersion) | ||
{ | ||
_sdkVersion = sdkVersion; | ||
_roleInstanceName = roleInstanceName; | ||
} | ||
|
||
public FunctionsTelemetryInitializer() : | ||
this(GetSdkVersion(), GetRoleInstanceName()) | ||
{ | ||
} | ||
|
||
private static string GetSdkVersion() | ||
this(GetSdkVersion()) | ||
{ | ||
return "azurefunctions-netiso: " + typeof(FunctionsTelemetryInitializer).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()!.Version; | ||
} | ||
|
||
private static string GetRoleInstanceName() | ||
private static string? GetSdkVersion() | ||
{ | ||
const string ComputerNameKey = "COMPUTERNAME"; | ||
const string WebSiteInstanceIdKey = "WEBSITE_INSTANCE_ID"; | ||
const string ContainerNameKey = "CONTAINER_NAME"; | ||
|
||
string? instanceName = Environment.GetEnvironmentVariable(WebSiteInstanceIdKey); | ||
if (string.IsNullOrEmpty(instanceName)) | ||
{ | ||
instanceName = Environment.GetEnvironmentVariable(ComputerNameKey); | ||
if (string.IsNullOrEmpty(instanceName)) | ||
{ | ||
instanceName = Environment.GetEnvironmentVariable(ContainerNameKey); | ||
} | ||
} | ||
string? version = typeof(FunctionsTelemetryInitializer).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; | ||
|
||
return instanceName ?? Environment.MachineName; | ||
return version == null ? null : $"azurefunctions-netiso: {version}"; | ||
} | ||
|
||
public void Initialize(ITelemetry telemetry) | ||
{ | ||
if (telemetry == null) | ||
if (telemetry is null || _sdkVersion is null) | ||
{ | ||
return; | ||
} | ||
|
||
telemetry.Context.Cloud.RoleInstance = _roleInstanceName; | ||
telemetry.Context.GetInternalContext().SdkVersion = _sdkVersion; | ||
|
||
telemetry.Context.Location.Ip ??= "0.0.0.0"; | ||
|
||
if (Activity.Current is not null) | ||
{ | ||
foreach (var tag in Activity.Current.Tags) | ||
{ | ||
switch (tag.Key) | ||
{ | ||
case NameKey: | ||
telemetry.Context.Operation.Name = tag.Value; | ||
continue; | ||
default: | ||
break; | ||
} | ||
|
||
if (telemetry is ISupportProperties properties && !tag.Key.StartsWith("ai_")) | ||
{ | ||
properties.Properties[tag.Key] = tag.Value; | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Microsoft.Azure.Functions.Worker.ApplicationInsights | ||
{ | ||
internal class TraceConstants | ||
{ | ||
public const string FunctionsActivitySource = "Microsoft.Azure.Functions.Worker"; | ||
|
||
public const string AttributeExceptionEventName = "exception"; | ||
public const string AttributeExceptionType = "exception.type"; | ||
public const string AttributeExceptionMessage = "exception.message"; | ||
public const string AttributeExceptionStacktrace = "exception.stacktrace"; | ||
} | ||
} |
Oops, something went wrong.