Skip to content

Commit

Permalink
Revert "push to posthog and compress function calls to hex (stakwork#…
Browse files Browse the repository at this point in the history
…2382)" (stakwork#2411)

This reverts commit 5959ee8.
  • Loading branch information
kevkevinpal authored Jan 13, 2025
1 parent 5959ee8 commit 444c17b
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 62 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ 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: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2028,8 +2028,6 @@ 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
60 changes: 1 addition & 59 deletions routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"bytes"
"context"
"encoding/json"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
"time"
"compress/gzip"

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
Expand All @@ -28,8 +26,6 @@ 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 @@ -200,77 +196,23 @@ 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)

elements_chain := ""
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:]
}
//fmt.Printf("%s:%d %s\n", trimmed, f.Line, f.Name)
append_element := fmt.Sprintf("%s:%d %s,\n", trimmed, f.Line, f.Name)
elements_chain = fmt.Sprintf("%s%s", append_element, elements_chain)
logger.Log.Machine("%s:%d %s\n", trimmed, f.Line, f.Name)

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()
hexCompressed, _ := compressToHex(elements_chain)
_ = 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 444c17b

Please sign in to comment.