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

feat: support classic ingest keys #371

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Text.RegularExpressions;

namespace Honeycomb.OpenTelemetry
{
Expand All @@ -18,6 +19,8 @@ public class HoneycombOptions
private const string OtelExporterOtlpProtocolGrpc = "grpc";
private const string OtelExporterHttpTracesPath = "/v1/traces";
private const string OtelExporterHttpMetricsPath = "/v1/metrics";
private const string ClassicKeyRegex = "^[a-f0-9]*$";
private const string IngestClassicKeyRegex = "^hc[a-z]ic_[a-z0-9]*$";
private bool isHttp = false;

/// <summary>
Expand Down Expand Up @@ -48,13 +51,26 @@ public class HoneycombOptions
public string ApiKey { get; set; }

/// <summary>
/// Returns whether the provided API key is a legacy key.
/// Returns whether the provided API key is a classic key.
/// </summary>
/// <remarks>
/// Legacy keys have 32 characters.
/// Classic configuration keys have 32 characters and classic ingest keys have 64 characters.
/// </remarks>
internal static bool IsClassicKey(string apikey) => apikey?.Length == 32;

internal static bool IsClassicKey(string apikey) {
if (String.IsNullOrEmpty(apikey))
{
return false;
}
else if (apikey.Length == 32)
{
return Regex.Match(apikey, ClassicKeyRegex).Success;
}
else if (apikey.Length == 64)
{
return Regex.Match(apikey, IngestClassicKeyRegex).Success;
}
return false;
}
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Write links to honeycomb traces as they come in
/// </summary>
Expand Down
22 changes: 11 additions & 11 deletions test/Honeycomb.OpenTelemetry.Tests/HoneycombOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,17 @@ public void DoesNotAppendMetricsPathToGenericEndpointIfPathSpecified(string prot
Assert.DoesNotContain("/v1/metrics", options.GetMetricsEndpoint());
}

[Fact]
public void Legacy_key_length()
{
var options = new HoneycombOptions { ApiKey = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a" };
Assert.True(HoneycombOptions.IsClassicKey(options.ApiKey));
}
[Fact]
public void Not_legacy_key_length()
[Theory]
[InlineData("", false)]
[InlineData("12345678901234567890123456789012", true)]
[InlineData("hcaic_1234567890123456789012345678901234567890123456789012345678", true)]
[InlineData("kgvSpPwegJshQkuowXReLD", false)]
[InlineData("hcaic_12345678901234567890123456", false)]
[InlineData("hcxik_01hqk4k20cjeh63wca8vva5stw70nft6m5n8wr8f5mjx3762s8269j50wc", false)]
public void IsClassicKey(string key, bool expected)
{
var options = new HoneycombOptions { ApiKey = "specialenvkey" };
Assert.False(HoneycombOptions.IsClassicKey(options.ApiKey));
var options = new HoneycombOptions { ApiKey = key };
Assert.Equal(HoneycombOptions.IsClassicKey(key), expected);
}
}
}
}
Loading