Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: support Classic Ingest Keys #193

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions honeycomb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package honeycomb

import (
"os"
"regexp"
"runtime"
"strconv"

Expand All @@ -35,6 +36,9 @@ const (
otlpProtoVersionValue = "1.0.0"
)

var classicKeyRegex = regexp.MustCompile(`^[a-f0-9]*$`)
var classicIngestKeyRegex = regexp.MustCompile(`^hc[a-z]ic_[a-z0-9]*$`)

func init() {
otelconfig.SetVendorOptions = getVendorOptionSetters
otelconfig.ValidateConfig = validateConfig
Expand Down Expand Up @@ -188,14 +192,13 @@ func validateConfig(c *otelconfig.Config) error {
dataset := c.Headers[honeycombDatasetHeader]

if c.Logger != nil {
switch len(apikey) {
case 0:
if len(apikey) == 0 {
c.Logger.Debugf(noApiKeyDetectedMessage)
case 32: // classic
} else if isClassicKey(apikey) {
if dataset == "" {
c.Logger.Debugf("%s\n%s", classicKeyMissingDatasetMessage, apikey)
}
default:
} else {
if dataset != "" {
c.Logger.Debugf(dontSetADatasetMessageMessage)
}
Expand All @@ -204,3 +207,12 @@ func validateConfig(c *otelconfig.Config) error {

return nil
}

func isClassicKey(key string) bool {
if len(key) == 32 {
return classicKeyRegex.MatchString(key)
} else if len(key) == 64 {
return classicIngestKeyRegex.MatchString(key)
}
return false
}
53 changes: 43 additions & 10 deletions honeycomb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/honeycombio/otel-config-go/otelconfig"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
)
Expand Down Expand Up @@ -92,6 +93,11 @@ func TestSetVendorOptions(t *testing.T) {
}

func TestValidateConfig(t *testing.T) {
classicKey := "12345678901234567890123456789012"
classicIngestKey := "hcxic_1234567890123456789012345678901234567890123456789012345678"
ingestKey := "hcxik_1234567890123456789012345678901234567890123456789012345678"
modernKey := "123456789012345678901"

testCases := []struct {
desc string
apikey string
Expand All @@ -101,21 +107,42 @@ func TestValidateConfig(t *testing.T) {
}{
{
desc: "modern API key and no dataset",
apikey: "123456789012345678901",
apikey: modernKey,
dataset: "",
expectedLoggerFormat: "",
expectedLoggerValues: nil,
},
{
desc: "classic API key and a dataset",
apikey: "12345678901234567890123456789012",
apikey: classicKey,
dataset: "is-set-horrah",
expectedLoggerFormat: "",
expectedLoggerValues: nil,
},
{
desc: "classic Ingest Key and a dataset",
apikey: classicIngestKey,
dataset: "my-dataset",
expectedLoggerFormat: "",
expectedLoggerValues: nil,
},
{
desc: "Ingest Key and no dataset",
apikey: ingestKey,
dataset: "",
expectedLoggerFormat: "",
expectedLoggerValues: nil,
},
{
desc: "modern API key and a dataset",
apikey: "123456789012345678901",
apikey: modernKey,
dataset: "no thank you",
expectedLoggerFormat: dontSetADatasetMessageMessage,
expectedLoggerValues: nil,
},
{
desc: "ingest key and a dataset",
apikey: ingestKey,
dataset: "no thank you",
expectedLoggerFormat: dontSetADatasetMessageMessage,
expectedLoggerValues: nil,
Expand All @@ -129,12 +156,22 @@ func TestValidateConfig(t *testing.T) {
},
{
desc: "classic API key and no dataset",
apikey: "12345678901234567890123456789012",
apikey: classicKey,
dataset: "",
expectedLoggerFormat: "%s\n%s",
expectedLoggerValues: []interface{}{
classicKeyMissingDatasetMessage,
classicKey,
},
},
{
desc: "classic Ingest Key and no dataset",
apikey: classicIngestKey,
dataset: "",
expectedLoggerFormat: "%s\n%s",
expectedLoggerValues: []interface{}{
classicKeyMissingDatasetMessage,
"12345678901234567890123456789012",
classicIngestKey,
},
},
}
Expand All @@ -143,15 +180,11 @@ func TestValidateConfig(t *testing.T) {
aConfig := freshConfig()
aConfig.Headers[honeycombApiKeyHeader] = tC.apikey
aConfig.Headers[honeycombDatasetHeader] = tC.dataset

logger := &captureLogger{}

aConfig.Logger = logger

err := validateConfig(aConfig)

// Check the output
assert.Equal(t, err, nil)
require.NoError(t, err)
assert.Equal(t, tC.expectedLoggerFormat, logger.Format)
assert.Equal(t, tC.expectedLoggerValues, logger.Values)
})
Expand Down
Loading