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(posthog): Add Telemetry client Posthog #781

Merged
merged 1 commit into from
May 20, 2024

Conversation

javirln
Copy link
Member

@javirln javirln commented May 15, 2024

This PR creates a new implementation of a Tracker client based on Posthog.

Users can deactivate telemetry by setting the env variable DO_NOT_TRACK=1, it follows the convention at: https://consoledonottrack.com/

Information being tracked:

  • System information (OS, ARCH)
  • Command ran
  • CI Runner information if detected
  • CLI version
  • Hashed Control Plane URL
  • Anonymous device ID
  • User and Organization if logged in

Refs: #780

Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/internal/telemetry/posthog/posthog.go Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
Copy link
Member Author

@javirln javirln left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the code to be able to check the exit code as it was not trivial since cobra hooks are not executed when a command fails.

In any case I think it's pretty neat, we have everything we wanted.

app/cli/internal/telemetry/posthog/posthog.go Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
@javirln javirln changed the title WIP feat(posthog): Add Telemetry client Posthog feat(posthog): Add Telemetry client Posthog May 16, 2024
@javirln javirln marked this pull request as ready for review May 16, 2024 06:36
@javirln javirln requested review from migmartri and jiparis May 16, 2024 06:37
@javirln javirln marked this pull request as draft May 16, 2024 06:49
@javirln javirln marked this pull request as ready for review May 16, 2024 07:57
@javirln javirln marked this pull request as draft May 16, 2024 09:05
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
@javirln
Copy link
Member Author

javirln commented May 16, 2024

Secrets on the repository updated already to the proper project.

@javirln javirln marked this pull request as ready for review May 16, 2024 16:53
.goreleaser.yml Outdated Show resolved Hide resolved
Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some minor comments :)

app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root_test.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/internal/telemetry/telemetry.go Outdated Show resolved Hide resolved
app/cli/internal/telemetry/telemetry.go Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
@javirln javirln marked this pull request as draft May 17, 2024 07:08
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not show debug messages by default, especially the one about telemetry enabled

@javirln
Copy link
Member Author

javirln commented May 17, 2024

Do not show debug messages by default, especially the one about telemetry enabled

I've moved the message to the root command. I was not able to set the proper flag from outside not making the logger to be consistent with the logging level. I even tried the global setting from zerlog without luck.

@javirln javirln marked this pull request as ready for review May 17, 2024 10:51
@javirln javirln marked this pull request as draft May 17, 2024 10:56
Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks,

I tried to figure out the flag issue and boy it's quite confusing, ..., I kinda managed to fix it but I do not like my solution. In am ok going with your solution for now but we should add a task to fix it.

Re telemetry performance, I'll take a quick look and see if I find anything, once done I think I'll be ready to approve this and move forward. Thanks!

app/cli/main.go Outdated
os.Exit(exitCode)
}
// Measure the execution time
executionTime := time.Since(timeStart)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need recording the duration? If we do not, I'd not add it yet. For these kind of nice to have features I tend to follow YAGNI pretty aggressively.

app/cli/main.go Outdated Show resolved Hide resolved
app/cli/main.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root_test.go Show resolved Hide resolved
@javirln
Copy link
Member Author

javirln commented May 20, 2024

Refactored all code with:

  • Moved all logic from main to root.
  • Use a separated go routine to send the telemetry on the persistentPreRunE. Use a sync.WaitGroup to wait for it in case it's slow.
  • Refactored pase of the jwt token in a generic way, otherwise we would have to cast the token to each type of claim coupling backend and CLI due to types being on the backend.
  • Added more tests with empty fields and random tokens.

@javirln javirln marked this pull request as ready for review May 20, 2024 08:01
app/cli/main.go Outdated
if err := rootCmd.Execute(); err != nil {
var msg string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can probably revert all the changes in this file. For example, having the msg var doesn't seem to make sense anymore.

app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
app/cli/cmd/root.go Outdated Show resolved Hide resolved
}

// DetermineUserID Determines the user ID using available information or generates a random one.
func DetermineUserID(tags Tags) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DetermineUserID can be downcase if you move the tests to the same package

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added it into a separated packaged because if added to the same package it creates an import cycle between the mocks and the implementation:

FAIL	github.com/chainloop-dev/chainloop/app/cli/internal/telemetry [setup failed]
package github.com/chainloop-dev/chainloop/app/cli/internal/telemetry
	imports github.com/chainloop-dev/chainloop/app/cli/internal/telemetry/mocks
	imports github.com/chainloop-dev/chainloop/app/cli/internal/telemetry: import cycle not allowed in test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this is the problem of using the mocks generated by mockery :(

A workaround is to create another file, telemerty_unit_test.go or similar and have these unit tests.

In general when I end up writing code in a specific way just so it works in the tests, I realize that I might be doing smth wrong and re-evaluate. But up to you.

Thansk!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A workaround is to create another file, telemerty_unit_test.go or similar and have these unit tests.

But the problem would still be the same, the method will still be public right?

I mean for me, I wouldn't even create a test for this specific function since they are covered by the tests on the Track method that includes it.

@@ -239,46 +268,125 @@ func loadControlplaneAuthToken(cmd *cobra.Command) (string, error) {
// 3. API token
// Each one of them have an associated audience claim that we use to identify the type of token. If the token is not
// present, nor we cannot match it with one of the expected audience, return nil.
func parseToken(token string) (*ParsedToken, error) {
func parseToken(token string) (*parsedToken, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but most probably we can have a common interface. Something like

type Token interface {
  func Parse(token string) (*parsedToken, error)
}

And then moving the "extracting" logic to their own implementations.

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
@javirln javirln merged commit 3968471 into chainloop-dev:main May 20, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants