Skip to content

Commit

Permalink
feat: support classic ingest keys (#371)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

We've released Ingest Keys, but need to update the key detection logic
to allow Ingest Keys to be used to send data to Classic environments.

## Short description of the changes
- updates the Classic API Key detection logic to understand the shape of
a Classic Ingest Key
- updates tests with a more comprehensive list of cases
- bonus: updated the circleCI image for smoke tests to
`ubuntu-2204:2024.01.1` since the one we were using [is
deprecated](https://discuss.circleci.com/t/linux-image-deprecations-and-eol-for-2024/50177)

## How to verify that this has the expected result

It should be possible to use [an ingest
key](https://docs.honeycomb.io/working-with-your-data/settings/api-keys/#ingest-keys-1)
pointed to a classic environment after this change. Please note that the
ability to create ingest keys in classic environments isn't yet publicly
available (since we need to update this library and others to understand
classic ingest keys).

---------

Co-authored-by: Mike Goldsmith <goldsmith.mike@gmail.com>
Co-authored-by: Phillip Carter <pcarter@fastmail.com>
  • Loading branch information
3 people authored Mar 5, 2024
1 parent 9a876a2 commit 415e430
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

smoke_test:
machine:
image: ubuntu-2004:202107-02
image: ubuntu-2204:2024.01.1
steps:
- checkout
- attach_workspace:
Expand Down
24 changes: 21 additions & 3 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,12 +51,27 @@ 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;
}

/// <summary>
/// Write links to honeycomb traces as they come in
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);
}
}
}
}

0 comments on commit 415e430

Please sign in to comment.