Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geneva Exporter - Throw on TableNameMappings null value #322

Merged
merged 13 commits into from
May 7, 2022
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 26 additions & 1 deletion src/OpenTelemetry.Exporter.Geneva/GenevaExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,36 @@ public class GenevaExporterOptions
[Schema.V40.PartA.Ver] = "4.0",
};

private IReadOnlyDictionary<string, string> _tableNameMappings;

public string ConnectionString { get; set; }

public IEnumerable<string> CustomFields { get; set; }

public IReadOnlyDictionary<string, string> TableNameMappings { get; set; }
public IReadOnlyDictionary<string, string> TableNameMappings
{
get => this._tableNameMappings;
set
{
Guard.ThrowIfNull(value);

foreach (var entry in value)
{
if (entry.Value is null)
mic-max marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentNullException(entry.Key, $"{nameof(this.TableNameMappings)} must not contain null values.");
}
}

var copy = new Dictionary<string, string>(value.Count);
foreach (var entry in value)
mic-max marked this conversation as resolved.
Show resolved Hide resolved
{
copy[entry.Key] = entry.Value; // shallow copy
}

this._tableNameMappings = copy;
}
}

public IReadOnlyDictionary<string, object> PrepopulatedFields
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public void SpecialChractersInTableNameMappings()
TableNameMappings = new Dictionary<string, string> { ["*"] = "\u0418" },
});
});

// Throw on null value - include key in exception message
var ex = Assert.Throws<ArgumentNullException>(() =>
{
new GenevaExporterOptions
{
TableNameMappings = new Dictionary<string, string> { ["TestCategory"] = null },
};
});
Assert.Contains("TableNameMappings must not contain null values.", ex.Message);
Assert.Equal("TestCategory", ex.ParamName);

// Throw when TableNameMappings is null
Assert.Throws<ArgumentNullException>(() =>
{
new GenevaExporterOptions
{
TableNameMappings = null,
};
});
}

[Theory]
Expand Down