From f188da43782a43b0b711a5c8423e59c841a07102 Mon Sep 17 00:00:00 2001 From: kevkevin Date: Wed, 15 Jan 2025 10:38:54 -0600 Subject: [PATCH] push to posthog and compress function calls to hex (#2437) --- go.mod | 1 + go.sum | 2 ++ routes/index.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3a073f373..ceebd5578 100644 --- a/go.mod +++ b/go.mod @@ -59,6 +59,7 @@ require ( github.com/lightningnetwork/lnd/tlv v1.1.1 // indirect github.com/lightningnetwork/lnd/tor v1.1.2 // indirect github.com/onsi/gomega v1.26.0 // indirect + github.com/posthog/posthog-go v1.2.24 // indirect github.com/robfig/cron v1.2.0 github.com/urfave/negroni v1.0.0 // indirect github.com/xhd2015/xgo/runtime v1.0.52 // indirect diff --git a/go.sum b/go.sum index d4ae95b46..9cafc0764 100644 --- a/go.sum +++ b/go.sum @@ -2028,6 +2028,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/posthog/posthog-go v1.2.24 h1:A+iG4saBJemo++VDlcWovbYf8KFFNUfrCoJtsc40RPA= +github.com/posthog/posthog-go v1.2.24/go.mod h1:uYC2l1Yktc8E+9FAHJ9QZG4vQf/NHJPD800Hsm7DzoM= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= diff --git a/routes/index.go b/routes/index.go index 122d69b42..0c652791b 100644 --- a/routes/index.go +++ b/routes/index.go @@ -2,7 +2,9 @@ package routes import ( "bytes" + "compress/gzip" "context" + "encoding/hex" "encoding/json" "fmt" "io" @@ -26,6 +28,8 @@ import ( "github.com/stakwork/sphinx-tribes/logger" customMiddleware "github.com/stakwork/sphinx-tribes/middlewares" "github.com/stakwork/sphinx-tribes/utils" + + "github.com/posthog/posthog-go" ) // NewRouter creates a chi router @@ -196,11 +200,37 @@ func sendEdgeListToJarvis(edgeList utils.EdgeList) error { return fmt.Errorf("jarvis returned non-success status: %d, body: %s", resp.StatusCode, string(body)) } +func compressToHex(input string) (string, error) { + // Create a buffer to hold the compressed data + var compressedBuffer bytes.Buffer + + // Create a gzip writer + gzipWriter := gzip.NewWriter(&compressedBuffer) + + // Write the input string to the gzip writer + _, err := gzipWriter.Write([]byte(input)) + if err != nil { + return "", fmt.Errorf("failed to write to gzip writer: %w", err) + } + + // Close the gzip writer to flush the data + err = gzipWriter.Close() + if err != nil { + return "", fmt.Errorf("failed to close gzip writer: %w", err) + } + + // Encode the compressed data to hex + hexEncoded := hex.EncodeToString(compressedBuffer.Bytes()) + return hexEncoded, nil +} + // Middleware to handle InternalServerError func internalServerErrorHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rr := negroni.NewResponseWriter(w) + var elements_chain strings.Builder + isExceedingLimit := false trap.AddInterceptor(&trap.Interceptor{ Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { index := strings.Index(f.File, "sphinx-tribes") @@ -208,11 +238,47 @@ func internalServerErrorHandler(next http.Handler) http.Handler { if index != -1 { trimmed = f.File[index:] } - logger.Log.Machine("%s:%d %s\n", trimmed, f.Line, f.Name) + //fmt.Printf("%s:%d %s\n", trimmed, f.Line, f.Name) + newContent := fmt.Sprintf("%s:%d %s,\n", trimmed, f.Line, f.Name) + if elements_chain.Len()+len(newContent) <= 512000 { + elements_chain.WriteString(newContent) + } else if isExceedingLimit == false { + // Optionally, you could log or handle this case differently if needed + fmt.Printf("elements_chain length exceeded 500KB, skipping further additions.\n") + isExceedingLimit = true + } return nil, nil }, }) + + defer func() { + posthog_key := os.Getenv("POSTHOG_KEY") + posthog_url := os.Getenv("POSTHOG_URL") + session_id := r.Header.Get("x-session-id") + if posthog_key != "" && posthog_url != "" && session_id != "" { + logger.Log.Info("Sending to Posthog") + client, _ := posthog.NewWithConfig( + posthog_key, + posthog.Config{ + Endpoint: posthog_url, + }, + ) + defer client.Close() + elements_chain_string := elements_chain.String() + hexCompressed, _ := compressToHex(elements_chain_string) + _ = client.Enqueue(posthog.Capture{ + DistinctId: session_id, // Unique ID for the user + Event: "backend_api_call", // The event name + Properties: map[string]interface{}{ + "$session_id": session_id, + "$event_type": "backend_api_call", + "$elements_chain": hexCompressed, + }, + }) + } + }() + defer func() { if err := recover(); err != nil { // Get stack trace