From ad558a6bdfdc95232bbc3ace83a013d66b2c5135 Mon Sep 17 00:00:00 2001 From: Kevin Gosse Date: Thu, 12 Dec 2024 14:34:42 +0100 Subject: [PATCH] Catch error when failing to read env vars on IIS --- .../Checks/Windows/IIS/ApplicationPool.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tracer/src/Datadog.Trace.Tools.dd_dotnet/Checks/Windows/IIS/ApplicationPool.cs b/tracer/src/Datadog.Trace.Tools.dd_dotnet/Checks/Windows/IIS/ApplicationPool.cs index d654c7a0e0c7..f9a36bd6accc 100644 --- a/tracer/src/Datadog.Trace.Tools.dd_dotnet/Checks/Windows/IIS/ApplicationPool.cs +++ b/tracer/src/Datadog.Trace.Tools.dd_dotnet/Checks/Windows/IIS/ApplicationPool.cs @@ -3,8 +3,11 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#pragma warning disable SA1108 // BlockStatementsMustNotContainEmbeddedComments + using System; using System.Collections.Generic; +using System.ComponentModel; namespace Datadog.Trace.Tools.dd_dotnet.Checks.Windows.IIS { @@ -55,14 +58,21 @@ public IReadOnlyDictionary GetEnvironmentVariables() { var result = new Dictionary(); - using var environmentVariables = _applicationPool.GetElementByName("environmentVariables"); - using var collection = environmentVariables.Collection(); - var count = collection.Count(); + try + { + using var environmentVariables = _applicationPool.GetElementByName("environmentVariables"); + using var collection = environmentVariables.Collection(); + var count = collection.Count(); - for (int i = 0; i < count; i++) + for (int i = 0; i < count; i++) + { + using var item = collection.GetItem(i); + result.Add(item.GetStringProperty("name"), item.GetStringProperty("value")); + } + } + catch (Win32Exception ex) when ((uint)ex.NativeErrorCode == 0x80070585) // Invalid index { - using var item = collection.GetItem(i); - result.Add(item.GetStringProperty("name"), item.GetStringProperty("value")); + Utils.WriteWarning("Could not read IIS environment variables. This is expected if using a version of IIS prior to 10."); } return result;