Skip to content

Commit b2d10ef

Browse files
authored
Update module to 1.0.0, final preview changes (#17)
* Update module to 1.0.0, final preview changes - Bump module version to 1.0.0 - Update OpenTelemetry dependencies to 1.8.1 - Add ResourceDetector to logs as well as traces - Increase specificity and reduce volume of warnings emitted by module
1 parent e261569 commit b2d10ef

13 files changed

+78
-16
lines changed

.vscode/launch.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"console": "integratedTerminal",
2828
"env": {
2929
"OTEL_EXPORTER_OTLP_ENDPOINT": "<VALUE>",
30-
"OTEL_EXPORTER_OTLP_HEADERS": "<VALUE>",
31-
"APPLICATIONINSIGHTS_CONNECTION_STRING": "<VALUE>"
30+
"OTEL_EXPORTER_OTLP_HEADERS": "<VALUE>"
3231
}
3332
}
3433
]

src/AzureFunctions.PowerShell.OpenTelemetry.SDK.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
# Version number of this module.
3-
ModuleVersion = '0.0.2'
3+
ModuleVersion = '1.0.0'
44

55
# Supported PSEditions
66
CompatiblePSEditions = @('Core')

src/AzureFunctions.PowerShell.OpenTelemetry.SDK/Constants/SDKConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace AzureFunctions.PowerShell.OpenTelemetry.SDK
1010
{
1111
public class SDKConstants
1212
{
13+
internal const string FunctionsOpenTelemetryModuleName = "AzureFunctions.PowerShell.OpenTelemetry.SDK";
1314
internal const string FunctionsOpenTelemetryEnvironmentVariableName = "OTEL_FUNCTIONS_WORKER_ENABLED";
1415
internal const string EnvironmentVariableMissingErrorCategory = "OpenTelemetryEnvironmentVariableNotSet";
1516

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Security.Policy;
7+
8+
namespace AzureFunctions.PowerShell.OpenTelemetry.SDK
9+
{
10+
public class FunctionsEnvironmentWarningChecker
11+
{
12+
private static bool _environmentVariableWarned = false;
13+
14+
private static string _warningMessage = String.Format("{0}: Environment variable {1} not set, this module may not have the intended behavior. " +
15+
"Logs emitted from your function app's default pipeline will not be sent to OpenTelemetry, " +
16+
"and traces and spans will not be correlated with telemetry emitted from the functions host.",
17+
SDKConstants.FunctionsOpenTelemetryModuleName, SDKConstants.FunctionsOpenTelemetryEnvironmentVariableName);
18+
19+
private static bool IsFunctionsEnvironmentVariableEnabled()
20+
{
21+
string? value = System.Environment.GetEnvironmentVariable(SDKConstants.FunctionsOpenTelemetryEnvironmentVariableName);
22+
23+
if (!string.IsNullOrEmpty(value) && bool.TryParse(value, out bool isEnabled) && isEnabled)
24+
{
25+
return true;
26+
}
27+
28+
return false;
29+
}
30+
31+
internal static bool ShouldWarnEnvironmentVariableMissing(out string? warningMessage)
32+
{
33+
warningMessage = null;
34+
35+
if (!_environmentVariableWarned && !IsFunctionsEnvironmentVariableEnabled())
36+
{
37+
warningMessage = _warningMessage;
38+
39+
_environmentVariableWarned = true;
40+
return true;
41+
}
42+
43+
// Even if we didn't warn, we still set this to true to prevent expensive env checks for future calls
44+
_environmentVariableWarned = true;
45+
return false;
46+
}
47+
}
48+
}

src/AzureFunctions.PowerShell.OpenTelemetry.SDK/Logging/NewFunctionsOpenTelemetryLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class NewFunctionsOpenTelemetryLogger : PSCmdlet
2020
{
2121
protected override void ProcessRecord()
2222
{
23-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(SDKConstants.FunctionsOpenTelemetryEnvironmentVariableName)))
23+
if (FunctionsEnvironmentWarningChecker.ShouldWarnEnvironmentVariableMissing(out var warningMessage))
2424
{
25-
WriteWarning("OpenTelemetry environment variable not set, logs from your function app's default logging pipeline will not be sent via this module to OpenTelemetry");
25+
WriteWarning(warningMessage);
2626
}
2727

2828
var response = FunctionsLoggerBuilder.GetLogger().BuildResponse();

src/AzureFunctions.PowerShell.OpenTelemetry.SDK/Logging/WriteFunctionsOpenTelemetryLog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class WriteFunctionsOpenTelemetryLog : PSCmdlet
2424

2525
protected override void ProcessRecord()
2626
{
27-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(SDKConstants.FunctionsOpenTelemetryEnvironmentVariableName)))
27+
if (FunctionsEnvironmentWarningChecker.ShouldWarnEnvironmentVariableMissing(out var warningMessage))
2828
{
29-
WriteWarning("OpenTelemetry environment variable not set, logs emitted from this worker instance will not be correlated with the invocation");
29+
WriteWarning(warningMessage);
3030
}
3131

3232
FunctionsLoggerBuilder.GetLogger().Log(LogItem, Level?.ToString());

src/AzureFunctions.PowerShell.OpenTelemetry.SDK/Traces/NewFunctionsOpenTelemetryTracerBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public class NewFunctionsOpenTelemetryTracerBuilder : PSCmdlet
2828
// This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called
2929
protected override void ProcessRecord()
3030
{
31-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(SDKConstants.FunctionsOpenTelemetryEnvironmentVariableName)))
31+
if (FunctionsEnvironmentWarningChecker.ShouldWarnEnvironmentVariableMissing(out var warningMessage))
3232
{
33-
WriteWarning("OpenTelemetry environment variable not set, user generated traces will not be linked to parent trace from functions host");
33+
WriteWarning(warningMessage);
3434
}
3535

3636
var response = FunctionsTracerBuilder.BuildTracer(AdditionalSources);

src/AzureFunctions.PowerShell.OpenTelemetry.SDK/Traces/StartFunctionsOpenTelemetrySpan.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class StartFunctionsOpenTelemetrySpan : PSCmdlet
2525
// This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called
2626
protected override void ProcessRecord()
2727
{
28-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(SDKConstants.FunctionsOpenTelemetryEnvironmentVariableName)))
28+
if (FunctionsEnvironmentWarningChecker.ShouldWarnEnvironmentVariableMissing(out var warningMessage))
2929
{
30-
WriteWarning("OpenTelemetry environment variable not set, the span will not be correlated with the invocation span coming from functions host");
30+
WriteWarning(warningMessage);
3131
}
3232

3333
var response = FunctionsActivityBuilder.StartActivity(ActivityName);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace OpenTelemetryEngine.Constants
7+
{
8+
internal class WorkerConstants
9+
{
10+
internal const string SystemLogPrefix = "LanguageWorkerConsoleLog";
11+
}
12+
}

src/OpenTelemetryEngine/Logging/FunctionsLoggerBuilder.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
using Microsoft.Extensions.Logging;
77
using OpenTelemetry.Logs;
8+
using OpenTelemetry.Resources;
89
using OpenTelemetryEngine.Types;
10+
using OpenTelemetryEngine.Resources;
911

1012

1113
namespace OpenTelemetryEngine.Logging
@@ -22,7 +24,8 @@ public static void InitializeLogger()
2224
{
2325
builder.AddOpenTelemetry(options =>
2426
{
25-
options.AddOtlpExporter();
27+
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddDetector(new FunctionsResourceDetector()))
28+
.AddOtlpExporter();
2629
});
2730
});
2831

0 commit comments

Comments
 (0)