mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-24 02:39:42 +02:00
* feat(server): funnel/community/commercial business metrics + PostHog pairing (MUL-2949) PR3 of the Grafana board metrics split (parent MUL-2328). Adds 23 new Prometheus counter/histogram families to the PR2 BusinessMetrics collector covering the activation/community/commercial funnels, and binds every PostHog event emission to a matching metric increment so the two sides cannot drift. Funnel: signup, workspace_created, team_invite_sent/accepted, onboarding_*, cloud_waitlist_joined. Content: issue_created, chat_message_sent, agent_created, squad_created, autopilot_created, issue_executed. Runtime: runtime_registered/ready/failed/offline + ready_seconds histogram, daemon_ws_message_received_total. Autopilot: autopilot_run_started/terminal/skipped. Webhook/GitHub: webhook_delivery_total, github_event_received_total, github_pr_review_total, github_pr_merge_seconds histogram. CloudRuntime: cloudruntime_request_total + duration histogram, wired through a small RequestRecorder interface so the cloudruntime package stays decoupled from metrics. Commercial: feedback_submitted, contact_sales_submitted. The pairing helper metrics.RecordEvent(client, m, ev) emits the PostHog event AND increments the matching counter via IncForEvent dispatch, reading labels from the analytics event Properties. Every existing h.Analytics.Capture(analytics.X(...)) call site has been migrated to the helper across handler/, service/, and cmd/server/runtime_sweeper.go. Lint enforcement (server/internal/metrics/business_pairing_test.go): - TestEveryAnalyticsEventHasPrometheusCounter: every Event* constant in analytics/events.go either dispatches via IncForEvent or is in the taskMetricEvents allow-list (PR2 typed RecordTask* methods). - TestNoNakedAnalyticsCaptureInHandlersOrServices: AST-walks handler/ service/cmd-server for direct Analytics.Capture(...) calls — only service/task.go's captureTaskEvent helper is allow-listed. - TestEveryAnalyticsRecordEventTakesAnalyticsHelper: validates the third arg of every metrics.RecordEvent call is built from analytics.*. Cardinality protection: all new label values pass through fixed allow-lists in labels_pr3.go; unknown values collapse to 'other'/'unknown'/'error'. Refs: - Spec MUL-2328 / MUL-2949. - Builds on PR2 (MUL-2948) — collectors registered through the same BusinessMetrics struct, no separate Registry. - Uses PR1's taskfailure.Reason (MUL-2946) for runtime_failed's failure_reason label via NormalizeFailureReason. Out of scope: Sampler-class metrics (PR4 / MUL-2947), pr_review_total emission point (no review event handler exists yet — counter is defined, TODO to wire up when /api/webhooks/github grows pull_request_review handling). Co-authored-by: multica-agent <github@multica.ai> * fix(server): tighten PR3 review items — signup_source bucket, fill platform/kind/form_source enums, onboarding_started server emission, lint scope (MUL-2949) Addresses 张大彪's review on #3698: 1. signup_source: NormalizeSignupSource added to labels_pr3.go with a fixed allow-list bucket (direct/google/twitter/linkedin/.../other). Parses JSON cookie payload for utm_source/source/referrer fields, strips URL schemes, maps well-known hostnames to channel buckets. PostHog event still ships the raw cookie value for analytics; only the Prometheus label is bucketed. 2. Filled the unknown/other label gaps: - analytics.IssueCreated and analytics.ChatMessageSent now take a platform parameter sourced from middleware.ClientMetadataFromContext (X-Client-Platform header) at the handler. Autopilot-originated issues stamp PlatformServer. - analytics.FeedbackSubmitted now takes a kind parameter; CreateFeedback reads req.Kind (default "general") so the picker selection lights up the metric's kind label instead of long-term "other". - analytics.ContactSalesSubmitted now takes a formSource (page / onboarding / agents_page); CreateContactSales reads req.Source. The metric reads ev.Properties["form_source"] so the analytics CoreProperties.Source ("marketing_contact_sales") stays backward-compat for PostHog dashboards. 3. analytics.OnboardingStarted helper added; server-side emission lives in PatchOnboarding, fired exactly once per user on the first PATCH that carries a non-empty questionnaire payload (firstTouch logic compares prior bytes against {} / null). Frontend onboarding_started keeps firing on page open; the server emission is what guarantees the Prometheus counter exists so Grafana can be cross-checked against the PostHog funnel without depending on the SDK roundtrip. 4. business_pairing_test.go tightened: - TestNoNakedAnalyticsCaptureInHandlersOrServices now allow-lists at function granularity (just captureTaskEvent in service/task.go), not whole-file. Any future naked Capture in the same file fails CI. - TestEveryAnalyticsRecordEventTakesAnalyticsHelper now does def-use tracking inside the enclosing FuncDecl: when RecordEvent's third arg is an *ast.Ident, the test walks the function body for the assignment that defined it and confirms the RHS is an analytics.<Helper>(...) call. Bare local idents that didn't originate from analytics are now caught. 5. gofmt -w applied across the touched files; gofmt -l clean. Tests: go test ./internal/metrics/... ./internal/analytics/... pass. Pre-existing TestClaimTask_/TestWebhook_MergedPR/TestDeleteIssueByIdentifier failures on origin/main are DB-environment-dependent and not regressions from this change. Co-authored-by: multica-agent <github@multica.ai> * fix(server): normalise onboarding_started platform label + regression test (MUL-2949) Addresses 张大彪's last review nit: - IncForEvent's EventOnboardingStarted case now wraps the platform property with NormalizePlatform, matching every other platform-bearing metric. A misbehaving frontend can no longer leak a raw X-Client-Platform header value into the multica_onboarding_started_total{platform=...} series. - New labels_pr3_test.go covers every PR3 normalizer with both a happy-path value and an unknown value, asserting the unknown collapses to the documented fallback bucket. Includes a focused regression for onboarding_started: emits one event with an attacker-shaped platform string and asserts the metric only exposes web + unknown label values (no raw header bleed). - testutil.go gains a small GatherForTest helper so the regression test can pull the typed MetricFamily map without re-implementing the registry-walk dance. Co-authored-by: multica-agent <github@multica.ai> * fix(server): NormalizeTaskSource on workspace_created + document lint limitations (MUL-2949) Final review touch-ups before merge: - IncForEvent's EventWorkspaceCreated case wraps source through NormalizeTaskSource, matching the other source-bearing dispatches (issue_created, agent_created, issue_executed). Closes the last raw property leak in the dispatcher table. - business_pairing_test.go inline docstrings now spell out the two known limitations of the lint gate that 张大彪 / Eve flagged: analyticsBackedIdents matches by ident NAME (not SSA def-use, so a nested-scope shadow could pass) and isMetricsRecordEvent hard-codes the import alias set. PR description carries a Follow-ups section with the same two items so the work is visible after merge. Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: 魏和尚 <agent+wei@multica.ai> Co-authored-by: multica-agent <github@multica.ai>
324 lines
9.8 KiB
Go
324 lines
9.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/mail"
|
|
"net/netip"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/multica-ai/multica/server/internal/analytics"
|
|
"github.com/multica-ai/multica/server/internal/logger"
|
|
obsmetrics "github.com/multica-ai/multica/server/internal/metrics"
|
|
db "github.com/multica-ai/multica/server/pkg/db/generated"
|
|
)
|
|
|
|
// Public, unauthenticated endpoint for the landing-page "Contact Sales"
|
|
// form. Spam is mitigated by:
|
|
// - business-email validation (free providers blocked at the handler).
|
|
// - a per-email hourly cap so a successful submission can't be replayed
|
|
// into a flood from the same address.
|
|
// - per-IP rate limiting applied at the router (RATE_LIMIT_CONTACT_SALES).
|
|
//
|
|
// Free-text fields are length-capped before they reach the DB, and the
|
|
// request body itself is bounded so an attacker can't POST megabytes of
|
|
// junk into the JSONB-free TEXT columns.
|
|
const (
|
|
contactSalesMaxFirstName = 80
|
|
contactSalesMaxLastName = 80
|
|
contactSalesMaxEmail = 254
|
|
contactSalesMaxCompanyName = 200
|
|
contactSalesMaxGoals = 2000
|
|
contactSalesHourlyEmailCap = 3
|
|
contactSalesBodyLimit = 16 * 1024
|
|
)
|
|
|
|
// contactSalesAllowedCompanySize is the closed enum the frontend dropdown
|
|
// emits. Keep this in sync with `apps/web/features/landing/i18n/types.ts`.
|
|
var contactSalesAllowedCompanySize = []string{
|
|
"1-10",
|
|
"11-50",
|
|
"51-200",
|
|
"201-500",
|
|
"501-1000",
|
|
"1000+",
|
|
}
|
|
|
|
// contactSalesAllowedUseCase mirrors the "How do you plan to use or
|
|
// collaborate with Multica?" dropdown.
|
|
var contactSalesAllowedUseCase = []string{
|
|
"evaluate",
|
|
"adopt_team",
|
|
"self_host",
|
|
"integrate",
|
|
"partner",
|
|
"other",
|
|
}
|
|
|
|
// freeEmailDomains are the personal-mail domains we reject up front. The
|
|
// frontend shows the same warning copy, but server-side enforcement is the
|
|
// authority — a hand-rolled curl request mustn't be able to bypass it.
|
|
var freeEmailDomains = map[string]struct{}{
|
|
"gmail.com": {},
|
|
"googlemail.com": {},
|
|
"outlook.com": {},
|
|
"hotmail.com": {},
|
|
"live.com": {},
|
|
"msn.com": {},
|
|
"yahoo.com": {},
|
|
"yahoo.co.uk": {},
|
|
"yahoo.co.jp": {},
|
|
"ymail.com": {},
|
|
"icloud.com": {},
|
|
"me.com": {},
|
|
"mac.com": {},
|
|
"aol.com": {},
|
|
"protonmail.com": {},
|
|
"proton.me": {},
|
|
"pm.me": {},
|
|
"gmx.com": {},
|
|
"gmx.de": {},
|
|
"mail.com": {},
|
|
"zoho.com": {},
|
|
"yandex.com": {},
|
|
"yandex.ru": {},
|
|
"qq.com": {},
|
|
"163.com": {},
|
|
"126.com": {},
|
|
"sina.com": {},
|
|
"foxmail.com": {},
|
|
}
|
|
|
|
type CreateContactSalesRequest struct {
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
BusinessEmail string `json:"business_email"`
|
|
CompanyName string `json:"company_name"`
|
|
CompanySize string `json:"company_size"`
|
|
CountryRegion string `json:"country_region"`
|
|
UseCase string `json:"use_case"`
|
|
Goals string `json:"goals"`
|
|
// Source identifies where the form was opened from. Frontend
|
|
// enumerates {page, onboarding, agents_page}; the metric label
|
|
// `multica_contact_sales_submitted_total{source=...}` reads it
|
|
// via the metrics.NormalizeContactSalesSource allow-list, anything
|
|
// else collapses to "other". Empty falls back to "page" so legacy
|
|
// clients that don't send the field don't blackhole the metric.
|
|
Source string `json:"source"`
|
|
ConsentOutreach bool `json:"consent_outreach"`
|
|
ConsentUpdates bool `json:"consent_updates"`
|
|
}
|
|
|
|
type ContactSalesResponse struct {
|
|
ID string `json:"id"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
// CreateContactSales is the public POST /api/contact-sales endpoint.
|
|
func (h *Handler) CreateContactSales(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, contactSalesBodyLimit)
|
|
var req CreateContactSalesRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
firstName, ok := requireTrimmedField(w, req.FirstName, "first_name", contactSalesMaxFirstName)
|
|
if !ok {
|
|
return
|
|
}
|
|
lastName, ok := requireTrimmedField(w, req.LastName, "last_name", contactSalesMaxLastName)
|
|
if !ok {
|
|
return
|
|
}
|
|
companyName, ok := requireTrimmedField(w, req.CompanyName, "company_name", contactSalesMaxCompanyName)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
email, ok := canonicalBusinessEmail(req.BusinessEmail)
|
|
if !ok {
|
|
writeError(w, http.StatusBadRequest, "business_email is invalid")
|
|
return
|
|
}
|
|
if len(email) > contactSalesMaxEmail {
|
|
writeError(w, http.StatusBadRequest, "business_email is too long")
|
|
return
|
|
}
|
|
if !isBusinessEmailDomain(email) {
|
|
writeError(w, http.StatusBadRequest, "please use a business email address")
|
|
return
|
|
}
|
|
|
|
companySize := strings.TrimSpace(req.CompanySize)
|
|
if !slices.Contains(contactSalesAllowedCompanySize, companySize) {
|
|
writeError(w, http.StatusBadRequest, "company_size is invalid")
|
|
return
|
|
}
|
|
|
|
countryRegion := strings.TrimSpace(req.CountryRegion)
|
|
if countryRegion == "" {
|
|
writeError(w, http.StatusBadRequest, "country_region is required")
|
|
return
|
|
}
|
|
// Bound the dropdown value at a generous length — we accept any string
|
|
// the frontend submits (country list is large) but cap it so an
|
|
// attacker can't stuff the column.
|
|
if len(countryRegion) > 80 {
|
|
writeError(w, http.StatusBadRequest, "country_region is too long")
|
|
return
|
|
}
|
|
|
|
useCase := strings.TrimSpace(req.UseCase)
|
|
if !slices.Contains(contactSalesAllowedUseCase, useCase) {
|
|
writeError(w, http.StatusBadRequest, "use_case is invalid")
|
|
return
|
|
}
|
|
|
|
goals := strings.TrimSpace(req.Goals)
|
|
if len(goals) > contactSalesMaxGoals {
|
|
writeError(w, http.StatusBadRequest, "goals is too long")
|
|
return
|
|
}
|
|
|
|
// Per-email hourly cap: keeps a single business address from being
|
|
// used as a re-submit channel after one valid pass. DB-backed so it
|
|
// works across replicas and survives a restart.
|
|
count, err := h.Queries.CountRecentContactSalesByEmail(r.Context(), email)
|
|
if err != nil {
|
|
slog.Warn("count recent contact sales failed", append(logger.RequestAttrs(r), "error", err)...)
|
|
writeError(w, http.StatusInternalServerError, "failed to submit inquiry")
|
|
return
|
|
}
|
|
if count >= contactSalesHourlyEmailCap {
|
|
writeError(w, http.StatusTooManyRequests, "too many recent inquiries from this email")
|
|
return
|
|
}
|
|
|
|
inquiry, err := h.Queries.CreateContactSalesInquiry(r.Context(), db.CreateContactSalesInquiryParams{
|
|
FirstName: firstName,
|
|
LastName: lastName,
|
|
BusinessEmail: email,
|
|
CompanyName: companyName,
|
|
CompanySize: companySize,
|
|
CountryRegion: countryRegion,
|
|
UseCase: useCase,
|
|
Goals: goals,
|
|
ConsentOutreach: req.ConsentOutreach,
|
|
ConsentUpdates: req.ConsentUpdates,
|
|
UserAgent: truncateString(r.UserAgent(), 512),
|
|
SubmitterIp: submitterIP(r),
|
|
})
|
|
if err != nil {
|
|
slog.Warn("create contact sales failed", append(logger.RequestAttrs(r), "error", err)...)
|
|
writeError(w, http.StatusInternalServerError, "failed to submit inquiry")
|
|
return
|
|
}
|
|
|
|
inquiryID := uuidToString(inquiry.ID)
|
|
slog.Info("contact sales submitted",
|
|
append(logger.RequestAttrs(r),
|
|
"inquiry_id", inquiryID,
|
|
"company_size", companySize,
|
|
"country_region", countryRegion,
|
|
"use_case", useCase,
|
|
)...)
|
|
|
|
formSource := strings.TrimSpace(req.Source)
|
|
if formSource == "" {
|
|
formSource = "page"
|
|
}
|
|
|
|
obsmetrics.RecordEvent(h.Analytics, h.Metrics, analytics.ContactSalesSubmitted(
|
|
inquiryID,
|
|
companySize,
|
|
countryRegion,
|
|
useCase,
|
|
formSource,
|
|
goals != "",
|
|
))
|
|
|
|
writeJSON(w, http.StatusCreated, ContactSalesResponse{
|
|
ID: inquiryID,
|
|
CreatedAt: timestampToString(inquiry.CreatedAt),
|
|
})
|
|
}
|
|
|
|
func requireTrimmedField(w http.ResponseWriter, raw, field string, max int) (string, bool) {
|
|
v := strings.TrimSpace(raw)
|
|
if v == "" {
|
|
writeError(w, http.StatusBadRequest, field+" is required")
|
|
return "", false
|
|
}
|
|
if len(v) > max {
|
|
writeError(w, http.StatusBadRequest, field+" is too long")
|
|
return "", false
|
|
}
|
|
return v, true
|
|
}
|
|
|
|
func truncateString(s string, max int) string {
|
|
if len(s) <= max {
|
|
return s
|
|
}
|
|
return s[:max]
|
|
}
|
|
|
|
// canonicalBusinessEmail parses raw user input with net/mail and returns the
|
|
// canonical "local@domain" form (lower-cased, no display name, no surrounding
|
|
// whitespace). It is the only safe input to isBusinessEmailDomain — checking
|
|
// the raw string allows `Ada <ada@gmail.com>` to slip past the free-email
|
|
// block list because the parsed RFC 5322 address would have domain `gmail.com`
|
|
// while the raw "@" suffix would be `gmail.com>`.
|
|
func canonicalBusinessEmail(raw string) (string, bool) {
|
|
trimmed := strings.TrimSpace(raw)
|
|
if trimmed == "" {
|
|
return "", false
|
|
}
|
|
addr, err := mail.ParseAddress(trimmed)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
email := strings.ToLower(strings.TrimSpace(addr.Address))
|
|
// mail.ParseAddress also accepts forms like `<ada@example.com>` and
|
|
// addresses with comments; addr.Address strips both. Require the
|
|
// canonical local@domain shape with non-empty pieces on either side.
|
|
at := strings.LastIndex(email, "@")
|
|
if at <= 0 || at == len(email)-1 {
|
|
return "", false
|
|
}
|
|
return email, true
|
|
}
|
|
|
|
func isBusinessEmailDomain(email string) bool {
|
|
at := strings.LastIndex(email, "@")
|
|
if at < 0 || at == len(email)-1 {
|
|
return false
|
|
}
|
|
domain := strings.ToLower(email[at+1:])
|
|
if _, blocked := freeEmailDomains[domain]; blocked {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// submitterIP captures the raw connection IP so we can correlate abuse
|
|
// after the fact. We deliberately ignore X-Forwarded-For here — the
|
|
// router's rate-limit middleware already vets trusted-proxy headers; for
|
|
// the audit record we want the actual TCP peer, which is the conservative
|
|
// signal under deployments that don't enforce XFF stripping.
|
|
func submitterIP(r *http.Request) *netip.Addr {
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
host = r.RemoteAddr
|
|
}
|
|
addr, err := netip.ParseAddr(host)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return &addr
|
|
}
|