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-flavored ingest keys #237

Merged
merged 5 commits into from
Feb 27, 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
17 changes: 14 additions & 3 deletions libhoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"path"
"reflect"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -73,6 +74,9 @@ var sd, _ = statsd.New(statsd.Mute(true), statsd.Prefix("libhoney"))
// expected format is product-name/version, eg "myapp/1.0"
var UserAgentAddition string

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

// Config specifies settings for initializing the library.
type Config struct {

Expand Down Expand Up @@ -147,7 +151,7 @@ type Config struct {
}

func (c *Config) getDataset() string {
if c.isClassic() {
if c.IsClassic() {
if strings.TrimSpace(c.Dataset) == "" {
return defaultClassicDataset
}
Expand All @@ -164,8 +168,15 @@ func (c *Config) getDataset() string {
return trimmedDataset
}

func (c *Config) isClassic() bool {
return c.APIKey == "" || len(c.APIKey) == 32
func (c *Config) IsClassic() bool {
if len(c.APIKey) == 0 {
return true
} else if len(c.APIKey) == 32 {
return classicKeyRegex.MatchString(c.APIKey)
} else if len(c.APIKey) == 64 {
return classicIngestKeyRegex.MatchString(c.APIKey)
}
return false
}

// Init is called on app initialization and passed a Config struct, which
Expand Down
30 changes: 25 additions & 5 deletions libhoney_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,12 @@ func TestConfigVariationsForClassicNonClassic(t *testing.T) {
{
apikey: "",
dataset: "",
expectedDataset: "libhoney-go dataset",
expectedDataset: defaultClassicDataset,
},
{
apikey: "c1a551c000d68f9ed1e96432ac1a3380",
dataset: "",
expectedDataset: "libhoney-go dataset",
expectedDataset: defaultClassicDataset,
},
{
apikey: "c1a551c000d68f9ed1e96432ac1a3380",
Expand All @@ -1170,13 +1170,33 @@ func TestConfigVariationsForClassicNonClassic(t *testing.T) {
{
apikey: "d68f9ed1e96432ac1a3380",
dataset: "",
expectedDataset: "unknown_dataset",
expectedDataset: defaultDataset,
},
{
apikey: "d68f9ed1e96432ac1a3380",
dataset: " my-service ",
expectedDataset: "my-service",
},
{
apikey: "hcxik_1234567890123456789012345678901234567890123456789012345678",
dataset: "",
expectedDataset: defaultDataset,
},
{
apikey: "hcxik_1234567890123456789012345678901234567890123456789012345678",
dataset: "my-service",
expectedDataset: "my-service",
},
{
apikey: "hcxic_1234567890123456789012345678901234567890123456789012345678",
dataset: "",
expectedDataset: defaultClassicDataset,
},
{
apikey: "hcxic_1234567890123456789012345678901234567890123456789012345678",
dataset: "my-service",
expectedDataset: "my-service",
},
}

for _, tc := range tests {
Expand All @@ -1194,7 +1214,7 @@ func TestVerifyAPIKey(t *testing.T) {
APIKey string
expectedEnvironment string
}{
{Name: "classic", APIKey: "lcYrFflRUR6rHbIifwqhfGRUR6rHbIic", expectedEnvironment: ""},
{Name: "classic", APIKey: "f2b9746602fd36049b222d3e8c6c48c9", expectedEnvironment: ""},
{Name: "non-classic", APIKey: "lcYrFflRUR6rHbIifwqhfG", expectedEnvironment: "test_env"},
}

Expand All @@ -1209,7 +1229,7 @@ func TestVerifyAPIKey(t *testing.T) {
assert.Equal(t, "/1/auth", r.URL.Path)
assert.Equal(t, []string{tc.APIKey}, r.Header["X-Honeycomb-Team"])

if config.isClassic() {
if config.IsClassic() {
w.Write([]byte(`{"team":{"slug":"test_team"}}`))
} else {
w.Write([]byte(`{"team":{"slug":"test_team"},"environment":{"slug":"test_env"}}`))
Expand Down
Loading