From bb01f8167b6a49d595f8bc055f5fc3f8552c1e6f Mon Sep 17 00:00:00 2001 From: Michael Maxwell Date: Fri, 6 May 2022 19:24:18 -0700 Subject: [PATCH] Geneva Exporter - Throw on TableNameMappings `null` value (#322) --- .../CHANGELOG.md | 3 +++ .../GenevaExporterOptions.cs | 25 ++++++++++++++++++- .../OpenTelemetry.Exporter.Geneva.csproj | 2 +- .../GenevaLogExporterTests.cs | 20 +++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md b/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md index 02cb9fb9ad..83a089daac 100644 --- a/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Throw exception when `TableNameMappings` contains a `null` value. +[322](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/322) + ## 1.2.6 [2022-Apr-21] * Set GenevaMetricExporter temporality preference back to Delta. diff --git a/src/OpenTelemetry.Exporter.Geneva/GenevaExporterOptions.cs b/src/OpenTelemetry.Exporter.Geneva/GenevaExporterOptions.cs index 4ef5ad6621..63eba08012 100644 --- a/src/OpenTelemetry.Exporter.Geneva/GenevaExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Geneva/GenevaExporterOptions.cs @@ -27,11 +27,34 @@ public class GenevaExporterOptions [Schema.V40.PartA.Ver] = "4.0", }; + private IReadOnlyDictionary _tableNameMappings; + public string ConnectionString { get; set; } public IEnumerable CustomFields { get; set; } - public IReadOnlyDictionary TableNameMappings { get; set; } + public IReadOnlyDictionary TableNameMappings + { + get => this._tableNameMappings; + set + { + Guard.ThrowIfNull(value); + + var copy = new Dictionary(value.Count); + + foreach (var entry in value) + { + if (entry.Value is null) + { + throw new ArgumentNullException(entry.Key, $"{nameof(this.TableNameMappings)} must not contain null values."); + } + + copy[entry.Key] = entry.Value; + } + + this._tableNameMappings = copy; + } + } public IReadOnlyDictionary PrepopulatedFields { diff --git a/src/OpenTelemetry.Exporter.Geneva/OpenTelemetry.Exporter.Geneva.csproj b/src/OpenTelemetry.Exporter.Geneva/OpenTelemetry.Exporter.Geneva.csproj index 11c0837589..097f916f46 100644 --- a/src/OpenTelemetry.Exporter.Geneva/OpenTelemetry.Exporter.Geneva.csproj +++ b/src/OpenTelemetry.Exporter.Geneva/OpenTelemetry.Exporter.Geneva.csproj @@ -1,4 +1,4 @@ - + true diff --git a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs index 58cda5efd4..276320ec9c 100644 --- a/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Geneva.Tests/GenevaLogExporterTests.cs @@ -62,6 +62,26 @@ public void SpecialChractersInTableNameMappings() TableNameMappings = new Dictionary { ["*"] = "\u0418" }, }); }); + + // Throw on null value - include key in exception message + var ex = Assert.Throws(() => + { + new GenevaExporterOptions + { + TableNameMappings = new Dictionary { ["TestCategory"] = null }, + }; + }); + Assert.Contains("TableNameMappings must not contain null values.", ex.Message); + Assert.Equal("TestCategory", ex.ParamName); + + // Throw when TableNameMappings is null + Assert.Throws(() => + { + new GenevaExporterOptions + { + TableNameMappings = null, + }; + }); } [Theory]