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(GEN-95|HELIX-1134): add datadog and mixpanel support #6

Merged
merged 3 commits into from
Sep 30, 2024

Conversation

danidomi
Copy link
Contributor

@danidomi danidomi commented Sep 18, 2024

adds support for datadog (Profiles and Spans)
adds mixpanel support (BAU metrics)

this PR also merges the indexer client of metrics in here!

https://github.com/InjectiveLabs/injective-indexer/blob/dev/metrics/client.go

Summary by CodeRabbit

  • New Features

    • Introduced MixPanel integration for enhanced analytics.
    • Added profiling capabilities with DataDog for better performance monitoring.
    • Enhanced error reporting and event tracking with new functions in the metrics framework.
    • New metrics reporting functions for counting, timing, and gauging metrics.
    • Added configuration options for improved metrics handling.
  • Bug Fixes

    • Improved error handling in function calls.
  • Chores

    • Updated dependencies for better observability and error tracking capabilities.

feat(HELIX-1134): add mixpanel support
Copy link

coderabbitai bot commented Sep 18, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes introduced in this pull request enhance the metrics handling functionality by adding MixPanel integration and expanding profiling capabilities. New configuration options have been added to the StatterConfig structure for greater customization. The metrics.go file introduces functions for error reporting and event tracking with Mixpanel. Additionally, the reports.go file adds new functions for counting, timing, and gauging metrics, along with utility functions for tagging. The go.mod file has been updated to include new dependencies to support these enhancements.

Changes

Files Change Summary
client.go Added MixPanel integration and profiling capabilities with new configuration options in StatterConfig. Introduced StartMixPanel and setupProfiler functions.
go.mod Updated to include new dependencies: suplog, mixpanel-go, and several indirect dependencies for improved logging and analytics.
metrics.go Introduced functions for error reporting and event tracking with Mixpanel, including ReportFuncDeferredError, Track, and NewEvent. Added utility function MergeTags.
reports.go Added functions for counting, timing, and gauging metrics, including Counter, CounterPositive, and Incr. Introduced utility functions BoolTag and IntTag.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Metrics
    participant MixPanel
    participant Profiler

    Client->>Metrics: Initialize Metrics Client
    Metrics->>MixPanel: Start MixPanel Client
    Metrics->>Profiler: Setup Profiler
    Client->>Metrics: Report Event
    Metrics->>MixPanel: Track Event
    Metrics->>Profiler: Record Profile Data
Loading

Poem

🐇 In the meadow where metrics bloom,
New features sprout, dispelling gloom.
MixPanel dances, profiling sings,
Count and gauge, oh what joy it brings!
With tags that sparkle, our data's bright,
A rabbit's delight, in metrics' light! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c71af53 and 33c227e.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (4)
  • client.go (6 hunks)
  • go.mod (6 hunks)
  • metrics.go (5 hunks)
  • reports.go (2 hunks)
Additional context used
golangci-lint
reports.go

63-63: Error return value of s.Timing is not checked

(errcheck)


69-69: Error return value of s.Gauge is not checked

(errcheck)

Additional comments not posted (25)
reports.go (5)

45-49: LGTM!

The Counter function is implemented correctly and uses the CustomReport function to handle the reporting logic.


51-55: LGTM!

The CounterPositive function is implemented correctly and uses generics to accept any integer type for the value argument. It conditionally calls the Counter function only if the value is greater than zero, ensuring that only positive counts are recorded.


57-59: LGTM!

The Incr function is implemented correctly and simplifies the process of incrementing a metric by one by calling the Counter function with a value of 1.


73-78: LGTM!

The BoolTag function is implemented correctly and provides a convenient way to convert boolean values to string representations suitable for tagging.


80-82: LGTM!

The IntTag function is implemented correctly and uses generics to accept any integer type. It converts the integer value to a string representation using strconv.Itoa.

go.mod (5)

7-7: LGTM!

The addition of the github.com/InjectiveLabs/suplog dependency at version v1.3.3 looks good. It will likely enhance the logging capabilities of the project.


10-10: LGTM!

The addition of the github.com/mixpanel/mixpanel-go dependency at version v1.2.1 looks good. It will enable Mixpanel analytics integration in the project.


38-38: LGTM!

The addition of the github.com/aws/aws-sdk-go indirect dependency at version v1.44.327 looks good. It's likely pulled in transitively by another direct dependency and should not cause any issues.


42-43: LGTM!

The addition of the github.com/bugsnag/bugsnag-go and github.com/bugsnag/panicwrap indirect dependencies at versions v1.5.3 and v1.3.4 respectively looks good. They are likely pulled in transitively by another direct dependency and should not cause any issues.


112-112: LGTM!

The addition of the github.com/sirupsen/logrus indirect dependency at version v1.9.3 looks good. It's likely pulled in transitively by another direct dependency and should not cause any issues.

metrics.go (5)

27-34: LGTM!

The ReportFuncDeferredError function provides a useful utility for deferred error reporting. It captures the caller function name and returns a closure that checks for errors and reports them using ReportClosureFuncError. This can help in identifying the source of errors and ensure that they are not missed.


74-84: LGTM!

The ReportFuncCallAndTimingCtxWithErr function is a useful addition that combines timing and error reporting in a context-aware manner. It starts a timer using reportTiming and returns a closure that stops the timer and reports any errors using ReportClosureFuncError. This can help in measuring the execution time of functions and identifying performance bottlenecks and error-prone functions.


230-239: LGTM!

The Track function provides a simple and convenient way to send events to Mixpanel. It checks if the mixPanelClient is initialized and sends the events using mixPanelClient.Track with the provided context. If an error occurs during sending, it returns the error. If the client is not initialized or the sending is successful, it returns nil. This function is context-aware and handles the case when the Mixpanel client is not initialized gracefully.


241-247: LGTM!

The NewEvent function provides a simple way to create new Mixpanel events. It checks if the mixPanelClient is initialized and creates a new event using mixPanelClient.NewEvent with the provided event name, distinct ID, and properties. If the client is not initialized, it returns nil. This function handles the case when the Mixpanel client is not initialized gracefully.


268-279: LGTM!

The MergeTags function provides a useful utility for merging multiple tag maps into a single map. It creates a new tag map, copies the key-value pairs from the original map, and then iterates over the source tag maps, copying their key-value pairs into the destination map. The merging process overwrites existing keys with the values from the source maps, following a last-write-wins strategy. This function allows for easy combination of tags from different sources.

client.go (10)

4-15: LGTM!

The new imports for Mixpanel and Datadog profiler are valid and necessary for the added functionality.


32-34: LGTM!

The new variables for the Datadog tracer provider, tracer, and Mixpanel client are properly initialized and necessary for the added functionality.


43-51: LGTM!

The new fields added to the StatterConfig struct for version, mocking threshold, profiling, and Mixpanel are properly defined and provide necessary configuration options for the added functionality.


103-103: LGTM!

Using the cfg parameter directly in the newMockStatter function call is more appropriate and improves the function's independence and testability.


143-158: LGTM!

The new code for initializing the Datadog tracer provider and tracer, setting up the profiler, and starting Mixpanel is properly guarded by the respective configuration flags and includes appropriate error handling.


163-167: LGTM!

The new StartMixPanel function properly initializes the Mixpanel client using the provided projectToken and ensures thread safety with a mutex lock.


169-187: LGTM!

The new setupProfiler function properly configures the Datadog profiler with the provided configuration options, sets the mutex and block profile rates, and includes appropriate error handling and wrapping.


205-205: LGTM!

Using log.WithError in the errHandler function is a more structured and consistent way of logging errors, improving the readability of error messages.


208-214: LGTM!

The updates to the newMockStatter function to take the cfg parameter directly and use the MockingThreshold field are properly implemented and improve the function's independence and configurability.


218-290: LGTM!

The updates to the mockStatter struct and its methods to use the threshold field for conditional logging are consistently applied and properly implemented, improving the configurability of the mock statter.

Comment on lines +61 to +65
func Timer(metric string, value time.Duration, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Timing(metric, value, tagSpec, 1)
}, tags...)
}
Copy link

Choose a reason for hiding this comment

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

Handle the error return value of s.Timing.

The Timer function is implemented correctly, but it ignores the error return value of s.Timing. This could lead to silent failures if the timing metric fails to be recorded.

Consider handling the error return value to ensure that any failures are logged or propagated appropriately. For example:

func Timer(metric string, value time.Duration, tags ...Tags) {
    CustomReport(func(s Statter, tagSpec []string) {
        if err := s.Timing(metric, value, tagSpec, 1); err != nil {
            // Handle the error, e.g., log it or propagate it
            log.Printf("Failed to record timing metric: %v", err)
        }
    }, tags...)
}
Tools
golangci-lint

63-63: Error return value of s.Timing is not checked

(errcheck)

Comment on lines +67 to +71
func Gauge(metric string, value float64, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Gauge(metric, value, tagSpec, 1)
}, tags...)
}
Copy link

Choose a reason for hiding this comment

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

Handle the error return value of s.Gauge.

The Gauge function is implemented correctly, but it ignores the error return value of s.Gauge. This could lead to silent failures if the gauge metric fails to be recorded.

Consider handling the error return value to ensure that any failures are logged or propagated appropriately. For example:

func Gauge(metric string, value float64, tags ...Tags) {
    CustomReport(func(s Statter, tagSpec []string) {
        if err := s.Gauge(metric, value, tagSpec, 1); err != nil {
            // Handle the error, e.g., log it or propagate it
            log.Printf("Failed to record gauge metric: %v", err)
        }
    }, tags...)
}
Tools
golangci-lint

69-69: Error return value of s.Gauge is not checked

(errcheck)

client.go Outdated Show resolved Hide resolved
reports.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
reports.go (1)

74-78: Simplify BoolTag using strconv.FormatBool.

You can simplify the BoolTag function by using the standard library function strconv.FormatBool, which returns the string representation of a boolean value.

Apply this diff to streamline the function:

 func BoolTag(b bool) string {
-	if b {
-		return "true"
-	}
-	return "false"
+	return strconv.FormatBool(b)
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 33c227e and 604f92d.

📒 Files selected for processing (1)
  • reports.go (2 hunks)

Comment on lines +45 to +49
func Counter[T constraints.Integer](metric string, value T, tags ...Tags) {
CustomReport(func(s Statter, tagSpec []string) {
s.Count(metric, int64(value), tagSpec, 1)
}, tags...)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle the error return value of s.Count in Counter.

The s.Count method returns an error that is currently being ignored. This could lead to silent failures if the count metric fails to be recorded. It's important to handle or log the error to ensure reliability.

Apply this diff to handle the error:

func Counter[T constraints.Integer](metric string, value T, tags ...Tags) {
	CustomReport(func(s Statter, tagSpec []string) {
		if err := s.Count(metric, int64(value), tagSpec, 1); err != nil {
			// Handle the error, e.g., log it or propagate it
			log.Printf("Failed to record count metric: %v", err)
		}
	}, tags...)
}

Comment on lines +80 to +82
func IntTag[T constraints.Integer](i T) string {
return strconv.Itoa(int(i))
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid casting to int in IntTag to prevent data loss.

Casting i to int with int(i) may lead to data loss or overflow when i is of a larger integer type (e.g., int64, uint64) that exceeds the range of int. This can cause incorrect tag values for metrics.

Consider using fmt.Sprint to safely convert any integer type to its string representation without casting:

-import (
-	"strconv"
-)
+import (
+	"fmt"
+)

 func IntTag[T constraints.Integer](i T) string {
-	return strconv.Itoa(int(i))
+	return fmt.Sprint(i)
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func IntTag[T constraints.Integer](i T) string {
return strconv.Itoa(int(i))
}
import (
"fmt"
)
func IntTag[T constraints.Integer](i T) string {
return fmt.Sprint(i)
}

Comment on lines +51 to +55
func CounterPositive[T constraints.Integer](metric string, value T, tags ...Tags) {
if value > 0 {
Counter(metric, value, tags...)
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ensure correct comparison in CounterPositive for unsigned integers.

The condition value > 0 may not behave as expected for unsigned integer types due to their nature of never being negative. While it works correctly, consider explicitly handling unsigned integers to avoid confusion.

Specify that T is a signed integer to make the intention clear:

-func CounterPositive[T constraints.Integer](metric string, value T, tags ...Tags) {
+func CounterPositive[T constraints.Signed](metric string, value T, tags ...Tags) {
	if value > 0 {
		Counter(metric, value, tags...)
	}
}

Alternatively, clarify the intent in comments or ensure that the function is used appropriately with signed integers.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func CounterPositive[T constraints.Integer](metric string, value T, tags ...Tags) {
if value > 0 {
Counter(metric, value, tags...)
}
}
func CounterPositive[T constraints.Signed](metric string, value T, tags ...Tags) {
if value > 0 {
Counter(metric, value, tags...)
}
}

@danidomi danidomi merged commit 815d3fe into master Sep 30, 2024
2 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.

3 participants