|
| 1 | +//go:build ignore |
| 2 | + |
| 3 | +// runtime-e2e/v91_org_id_telemetry/main.go |
| 4 | +// |
| 5 | +// Real-wire test of the SDK's v9.1 org_id telemetry field (#2277). The |
| 6 | +// SDK does not expose its internal http.Client, so we run a tiny |
| 7 | +// in-process HTTP server that pretends to be the checkpoint receiver, |
| 8 | +// inspects the raw JSON body for the org_id field, and exits with the |
| 9 | +// verdict. Bytes flow real → real through net/http on both sides. |
| 10 | +// |
| 11 | +// Run via: |
| 12 | +// |
| 13 | +// # ORG_ID set — operator-supplied or cs_<uuid>: |
| 14 | +// ORG_ID=acme-corp go run runtime-e2e/v91_org_id_telemetry/main.go |
| 15 | +// |
| 16 | +// # ORG_ID unset — sentinel: |
| 17 | +// unset ORG_ID && go run runtime-e2e/v91_org_id_telemetry/main.go |
| 18 | +// |
| 19 | +// This proof goes alongside the unit-test wire assertions in |
| 20 | +// telemetry_test.go. Both paths run against the same SDK build; this |
| 21 | +// one additionally exercises the real net.Listen / http.Serve stack |
| 22 | +// rather than httptest.Server (which is functionally equivalent but |
| 23 | +// less faithful to a production-shaped wire path). |
| 24 | + |
| 25 | +package main |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "encoding/json" |
| 30 | + "fmt" |
| 31 | + "io" |
| 32 | + "net" |
| 33 | + "net/http" |
| 34 | + "os" |
| 35 | + "path/filepath" |
| 36 | + "sync/atomic" |
| 37 | + "time" |
| 38 | + |
| 39 | + axonflow "github.com/getaxonflow/axonflow-sdk-go/v8" |
| 40 | +) |
| 41 | + |
| 42 | +func main() { |
| 43 | + expectedOrgID := os.Getenv("ORG_ID") |
| 44 | + if expectedOrgID == "" { |
| 45 | + expectedOrgID = "local-dev-org" |
| 46 | + } |
| 47 | + |
| 48 | + // The SDK gates pings to once per 7 days via a stamp file at |
| 49 | + // os.UserCacheDir + axonflow/go-telemetry-last-sent. Nuke that |
| 50 | + // stamp file at the same path the SDK resolves — env var injection |
| 51 | + // here is too late (sharedHeartbeat is a package var initialized |
| 52 | + // before main() runs). This makes the proof self-contained on |
| 53 | + // any host without manual cleanup between runs. |
| 54 | + if cacheDir, cdErr := os.UserCacheDir(); cdErr == nil { |
| 55 | + stampPath := filepath.Join(cacheDir, "axonflow", "go-telemetry-last-sent") |
| 56 | + _ = os.Remove(stampPath) // ignore: stamp may not exist on first run |
| 57 | + } |
| 58 | + |
| 59 | + var capturedBody atomic.Value |
| 60 | + capturedBody.Store([]byte{}) |
| 61 | + |
| 62 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 63 | + body, _ := io.ReadAll(r.Body) |
| 64 | + capturedBody.Store(body) |
| 65 | + w.Header().Set("Content-Type", "application/json") |
| 66 | + _ = json.NewEncoder(w).Encode(map[string]string{"latest_version": "8.1.0"}) |
| 67 | + }) |
| 68 | + |
| 69 | + listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 70 | + if err != nil { |
| 71 | + fmt.Fprintf(os.Stderr, "listen: %v\n", err) |
| 72 | + os.Exit(2) |
| 73 | + } |
| 74 | + defer listener.Close() |
| 75 | + |
| 76 | + srv := &http.Server{Handler: handler} |
| 77 | + go func() { _ = srv.Serve(listener) }() |
| 78 | + defer srv.Shutdown(context.Background()) |
| 79 | + |
| 80 | + checkpointURL := "http://" + listener.Addr().String() + "/v1/ping" |
| 81 | + _ = os.Setenv("AXONFLOW_CHECKPOINT_URL", checkpointURL) |
| 82 | + _ = os.Setenv("AXONFLOW_TELEMETRY", "") |
| 83 | + |
| 84 | + client := axonflow.NewClient(axonflow.AxonFlowConfig{ |
| 85 | + Endpoint: "https://example.invalid", |
| 86 | + ClientID: "rt-e2e", |
| 87 | + ClientSecret: "rt-e2e", |
| 88 | + Mode: "production", |
| 89 | + }) |
| 90 | + _ = client |
| 91 | + |
| 92 | + // SDK fires telemetry on startup goroutine — give it a beat to land. |
| 93 | + time.Sleep(2 * time.Second) |
| 94 | + |
| 95 | + got := capturedBody.Load().([]byte) |
| 96 | + if len(got) == 0 { |
| 97 | + fmt.Fprintln(os.Stderr, "FAIL: no telemetry body captured (did SDK try to ping?)") |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | + |
| 101 | + var payload map[string]any |
| 102 | + if err := json.Unmarshal(got, &payload); err != nil { |
| 103 | + fmt.Fprintf(os.Stderr, "FAIL: telemetry body not valid JSON: %v\n%s\n", err, string(got)) |
| 104 | + os.Exit(1) |
| 105 | + } |
| 106 | + |
| 107 | + orgID, ok := payload["org_id"].(string) |
| 108 | + if !ok { |
| 109 | + fmt.Fprintf(os.Stderr, "FAIL: telemetry body missing org_id field; body: %s\n", string(got)) |
| 110 | + os.Exit(1) |
| 111 | + } |
| 112 | + if orgID != expectedOrgID { |
| 113 | + fmt.Fprintf(os.Stderr, "FAIL: telemetry org_id = %q, want %q\n", orgID, expectedOrgID) |
| 114 | + os.Exit(1) |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Printf("PASS: telemetry wire payload carries org_id=%q (expected=%q)\n", orgID, expectedOrgID) |
| 118 | + fmt.Printf("Wire body: %s\n", string(got)) |
| 119 | +} |
0 commit comments