forked from ton-connect/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Ton analytics send events #1
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
Open
tztopusr
wants to merge
47
commits into
tztopusr:master
Choose a base branch
from
ton-connect:ton-analytics-send-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
2e20643
sync time between bridges (#170)
ed9efd0
send events
dabf1c2
review message sent-received
cdf8b2b
expired messages
c213ff1
fix unit tests
3150a28
fixes after review
9e3442d
refactor CreateBridgeMessageValidationFailedEvent
3a8340a
get rid of bridge-client-message-decode-error
3d07152
get rid of CreateBridgeClientConnectStartedEvent, CreateBridgeConnect…
21d859f
restore some stuff
ead3504
event collector and event sender
e05ab32
fix topic TODOs
bfa57e9
minor typo
3e713fb
remove duplicating code
52a8ce0
generate new types
03945a0
fix all TODO send missing analytics event
b03b89e
analytics.AnalyticCollector -> analytics.EventCollector
336b431
SendBatch + minor refactoring
8c55571
Refactor analytics integration across handlers and storage
811809f
minor fixes
12bce12
fix todos
1b92821
add debug logs
846e336
revert me
8746d62
rename values in config
c317c48
fix
4cf7472
add TON_ANALYTICS vars
d716599
mock analytics server
80237a9
fix lint
26cfbd9
add ring_buffer_test.go
714beb9
docker/analytics-mock-server.go -> cmd/analytics-mock/main.go
3aa0f63
fixes after review
f22dc7e
simplify ring buffer
e28e2fb
update docs
ed0b0b4
simplify ring collector
774e923
move sender to separate file, use atomic
a9a5747
even simpler
fca79eb
even simpler
54364b6
fixes after review
243d97c
refactor flush part
ac4fe7f
pass trace_id everywhere
bb659ad
fix panic
a1f1b6c
use eventCh
3ed4481
final
2a515a8
feat: Add flush notification channel for metrics collector
TrueCarry 6dac359
fix: Change random id to 2^53 - 1 to allow safe JS integer conversions
TrueCarry df0c97e
Merge branch 'ton-analytics-send-events' of github.com:ton-connect/br…
TrueCarry 566e3b6
tests: Increase timeouts for the bridge tests
TrueCarry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "sync" | ||
| ) | ||
|
|
||
| type AnalyticsMock struct { | ||
| mu sync.RWMutex | ||
| receivedEvents []map[string]interface{} | ||
| totalEvents int | ||
| } | ||
|
|
||
| func NewAnalyticsMock() *AnalyticsMock { | ||
| return &AnalyticsMock{ | ||
| receivedEvents: make([]map[string]interface{}, 0), | ||
| } | ||
| } | ||
|
|
||
| func (m *AnalyticsMock) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/health" { | ||
| w.WriteHeader(http.StatusOK) | ||
| if _, err := w.Write([]byte("OK")); err != nil { | ||
| log.Printf("Failed to write health response: %v", err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if r.Method != http.MethodPost { | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| return | ||
| } | ||
|
|
||
| if r.URL.Path != "/events" { | ||
| w.WriteHeader(http.StatusNotFound) | ||
| return | ||
| } | ||
|
|
||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| log.Printf("Failed to read body: %v", err) | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| var events []map[string]interface{} | ||
| if err := json.Unmarshal(body, &events); err != nil { | ||
| log.Printf("Failed to unmarshal events: %v", err) | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| m.mu.Lock() | ||
| m.receivedEvents = append(m.receivedEvents, events...) | ||
| m.totalEvents += len(events) | ||
| total := m.totalEvents | ||
| m.mu.Unlock() | ||
|
|
||
| log.Printf("✅ Received batch of %d events (total: %d)", len(events), total) | ||
| for _, event := range events { | ||
| if eventName, ok := event["event_name"].(string); ok { | ||
| log.Printf(" - %s", eventName) | ||
| } | ||
| } | ||
|
|
||
| // Return 202 Accepted like the real analytics server | ||
| w.WriteHeader(http.StatusAccepted) | ||
| } | ||
|
|
||
| func (m *AnalyticsMock) GetStats(w http.ResponseWriter, r *http.Request) { | ||
| m.mu.RLock() | ||
| defer m.mu.RUnlock() | ||
|
|
||
| eventTypes := make(map[string]int) | ||
| for _, event := range m.receivedEvents { | ||
| if eventName, ok := event["event_name"].(string); ok { | ||
| eventTypes[eventName]++ | ||
| } | ||
| } | ||
|
|
||
| stats := map[string]interface{}{ | ||
| "total_events": m.totalEvents, | ||
| "event_types": eventTypes, | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(stats); err != nil { | ||
| log.Printf("Failed to encode stats: %v", err) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| mock := NewAnalyticsMock() | ||
|
|
||
| http.HandleFunc("/events", mock.ServeHTTP) | ||
| http.HandleFunc("/health", mock.ServeHTTP) | ||
| http.HandleFunc("/stats", mock.GetStats) | ||
|
|
||
| port := ":9090" | ||
| log.Printf("🚀 Analytics Mock Server starting on %s", port) | ||
| log.Printf("📊 Endpoints:") | ||
| log.Printf(" POST /events - Receive analytics events") | ||
| log.Printf(" GET /health - Health check") | ||
| log.Printf(" GET /stats - Get statistics") | ||
| log.Printf("") | ||
| if err := http.ListenAndServe(port, nil); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| FROM golang:1.24-alpine AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy the standalone mock server | ||
| COPY cmd/analytics-mock/main.go ./main.go | ||
|
|
||
| # Build the server | ||
| RUN go build -o analytics-mock main.go | ||
|
|
||
| FROM alpine:latest | ||
| RUN apk --no-cache add ca-certificates curl | ||
| WORKDIR /root/ | ||
| COPY --from=builder /app/analytics-mock . | ||
| EXPOSE 9090 | ||
| CMD ["./analytics-mock"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Copy-paste error in bridge3 analytics URL configuration
The
bridge3service hasTON_ANALYTICS_BRIDGE_URLset to"http://bridge-instance2:8080"which is copied from thebridge2service configuration. This causes bridge3 to report itself as bridge-instance2 in analytics events, making it impossible to distinguish which bridge instance generated the events. The URL should reference bridge3's own endpoint.