Skip to content

Commit

Permalink
push to posthog and compress function calls to hex (#2437)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal authored Jan 15, 2025
1 parent f199a19 commit f188da4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
68 changes: 67 additions & 1 deletion routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package routes

import (
"bytes"
"compress/gzip"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -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
Expand Down Expand Up @@ -196,23 +200,85 @@ 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")
trimmed := f.File
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
Expand Down

0 comments on commit f188da4

Please sign in to comment.