Skip to content

Commit

Permalink
feat: set env-var AIRBYTE_INSTALLATION_ID (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
colesnodgrass authored Jun 4, 2024
1 parent a24ff1e commit e83019c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 7 deletions.
6 changes: 6 additions & 0 deletions internal/cmd/local/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/airbytehq/abctl/internal/telemetry"
"github.com/docker/docker/api/types"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -133,6 +134,7 @@ type mockTelemetryClient struct {
success func(ctx context.Context, eventType telemetry.EventType) error
failure func(ctx context.Context, eventType telemetry.EventType, err error) error
attr func(key, val string)
user func() uuid.UUID
}

func (m *mockTelemetryClient) Start(ctx context.Context, eventType telemetry.EventType) error {
Expand All @@ -150,3 +152,7 @@ func (m *mockTelemetryClient) Failure(ctx context.Context, eventType telemetry.E
func (m *mockTelemetryClient) Attr(key, val string) {
m.attr(key, val)
}

func (m *mockTelemetryClient) User() uuid.UUID {
return m.user()
}
8 changes: 8 additions & 0 deletions internal/cmd/local/local/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/airbytehq/abctl/internal/cmd/local/localerr"
"github.com/airbytehq/abctl/internal/telemetry"
"github.com/cli/browser"
"github.com/google/uuid"
helmclient "github.com/mittwald/go-helm-client"
"github.com/mittwald/go-helm-client/values"
"github.com/pterm/pterm"
Expand Down Expand Up @@ -223,6 +224,12 @@ func New(provider k8s.Provider, opts ...Option) (*Command, error) {
func (c *Command) Install(ctx context.Context, user, pass string) error {
go c.watchEvents(ctx)

var telUser string
// only override the empty telUser if the tel.User returns a non-nil (uuid.Nil) value.
if c.tel.User() != uuid.Nil {
telUser = c.tel.User().String()
}

if err := c.handleChart(ctx, chartRequest{
name: "airbyte",
repoName: airbyteRepoName,
Expand All @@ -231,6 +238,7 @@ func (c *Command) Install(ctx context.Context, user, pass string) error {
chartRelease: airbyteChartRelease,
chartVersion: c.helmChartVersion,
namespace: airbyteNamespace,
values: []string{fmt.Sprintf("global.env_vars.AIRBYTE_INSTALLATION_ID=%s", telUser)},
}); err != nil {
return fmt.Errorf("could not install airbyte chart: %w", err)
}
Expand Down
16 changes: 13 additions & 3 deletions internal/cmd/local/local/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/airbytehq/abctl/internal/cmd/local/k8s"
"github.com/airbytehq/abctl/internal/telemetry"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
helmclient "github.com/mittwald/go-helm-client"
"github.com/mittwald/go-helm-client/values"
"helm.sh/helm/v3/pkg/action"
Expand All @@ -32,6 +33,10 @@ func TestCommand_Install(t *testing.T) {
{name: airbyteRepoName, url: airbyteRepoURL},
{name: nginxRepoName, url: nginxRepoURL},
}

// userID is for telemetry tracking purposes
userID := uuid.New()

expChartCnt := 0
expChart := []struct {
chart helmclient.ChartSpec
Expand All @@ -45,6 +50,7 @@ func TestCommand_Install(t *testing.T) {
CreateNamespace: true,
Wait: true,
Timeout: 10 * time.Minute,
ValuesOptions: values.Options{Values: []string{"global.env_vars.AIRBYTE_INSTALLATION_ID=" + userID.String()}},
},
release: release.Release{
Chart: &chart.Chart{Metadata: &chart.Metadata{Version: "1.2.3.4"}},
Expand Down Expand Up @@ -125,9 +131,8 @@ func TestCommand_Install(t *testing.T) {

attrs := map[string]string{}
tel := mockTelemetryClient{
attr: func(key, val string) {
attrs[key] = val
},
attr: func(key, val string) { attrs[key] = val },
user: func() uuid.UUID { return userID },
}

httpClient := mockHTTP{do: func(req *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -256,6 +261,7 @@ type mockTelemetryClient struct {
success func(context.Context, telemetry.EventType) error
failure func(context.Context, telemetry.EventType, error) error
attr func(key, val string)
user func() uuid.UUID
}

func (m *mockTelemetryClient) Start(ctx context.Context, eventType telemetry.EventType) error {
Expand All @@ -274,6 +280,10 @@ func (m *mockTelemetryClient) Attr(key, val string) {
m.attr(key, val)
}

func (m *mockTelemetryClient) User() uuid.UUID {
return m.user()
}

var _ HTTPClient = (*mockHTTP)(nil)

type mockHTTP struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/telemetry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/pterm/pterm"
"net/http"
"os"
Expand Down Expand Up @@ -36,6 +37,8 @@ type Client interface {
Failure(context.Context, EventType, error) error
// Attr should be called to add additional attributes to this activity.
Attr(key, val string)
// User returns the user identifier being used by this client
User() uuid.UUID
}

type getConfig struct {
Expand Down
9 changes: 7 additions & 2 deletions internal/telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewUUID() UUID {

// String returns a string representation of this UUID.
func (u UUID) String() string {
return uuid.UUID(u).String()
return u.toUUID().String()
}

func (u *UUID) UnmarshalYAML(node *yaml.Node) error {
Expand All @@ -46,14 +46,19 @@ func (u *UUID) UnmarshalYAML(node *yaml.Node) error {
}

func (u UUID) MarshalYAML() (any, error) {
return uuid.UUID(u).String(), nil
return u.toUUID().String(), nil
}

// IsZero implements the yaml interface, used to treat a uuid.Nil as empty for yaml purposes
func (u UUID) IsZero() bool {
return u.String() == uuid.Nil.String()
}

// toUUID converts the telemetry.UUID type back to the underlying uuid.UUID type
func (u UUID) toUUID() uuid.UUID {
return uuid.UUID(u)
}

// ULID is a wrapper around ulid.ULID so that we can implement the yaml interfaces.
// Deprecated: use UUID instead
type ULID ulid.ULID
Expand Down
9 changes: 8 additions & 1 deletion internal/telemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ anonymous_user_uuid: %s

func TestUUID(t *testing.T) {
t.Run("string", func(t *testing.T) {
uuid := uuid.New()
uuid := NewUUID()
if d := cmp.Diff(36, len(uuid.String())); d != "" {
t.Error("uuid length mismatch", d)
}
Expand Down Expand Up @@ -269,4 +269,11 @@ func TestUUID(t *testing.T) {
t.Error("uuid should zero", d)
}
})

t.Run("toUUID", func(t *testing.T) {
uuid := NewUUID()
if d := cmp.Diff(36, len(uuid.toUUID().String())); d != "" {
t.Error("uuid length mismatch", d)
}
})
}
9 changes: 8 additions & 1 deletion internal/telemetry/noop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package telemetry

import "context"
import (
"context"
"github.com/google/uuid"
)

var _ Client = (*NoopClient)(nil)

Expand All @@ -21,3 +24,7 @@ func (n NoopClient) Failure(context.Context, EventType, error) error {
}

func (n NoopClient) Attr(_, _ string) {}

func (n NoopClient) User() uuid.UUID {
return uuid.Nil
}
6 changes: 6 additions & 0 deletions internal/telemetry/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func WithSessionID(sessionID uuid.UUID) Option {
}
}

var _ Client = (*SegmentClient)(nil)

// SegmentClient client, all methods communicate with segment.
type SegmentClient struct {
doer Doer
Expand Down Expand Up @@ -78,6 +80,10 @@ func (s *SegmentClient) Attr(key, val string) {
s.attrs[key] = val
}

func (s *SegmentClient) User() uuid.UUID {
return s.cfg.UserUUID.toUUID()
}

const (
trackingKey = "kpYsVGLgxEqD5OuSZAQ9zWmdgBlyiaej"
url = "https://api.segment.io/v1/track"
Expand Down
6 changes: 6 additions & 0 deletions internal/telemetry/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/pterm/pterm"
"os"
"strings"
Expand Down Expand Up @@ -203,6 +204,7 @@ type MockClient struct {
success func(ctx context.Context, eventType EventType) error
failure func(ctx context.Context, eventType EventType, err error) error
attr func(key, val string)
user func() uuid.UUID
}

func (m MockClient) Start(ctx context.Context, eventType EventType) error {
Expand All @@ -220,3 +222,7 @@ func (m MockClient) Failure(ctx context.Context, eventType EventType, err error)
func (m MockClient) Attr(key, val string) {
m.attr(key, val)
}

func (m MockClient) User() uuid.UUID {
return m.user()
}

0 comments on commit e83019c

Please sign in to comment.