|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type Server struct { |
| 14 | + mu sync.Mutex |
| 15 | + file *os.File |
| 16 | + w *bufio.Writer |
| 17 | +} |
| 18 | + |
| 19 | +// NewServer creates a server. If outPath is empty, events are printed to stdout. |
| 20 | +func NewServer(outPath string) (*Server, error) { |
| 21 | + s := &Server{} |
| 22 | + if outPath == "" { |
| 23 | + log.Printf("collector: EVENT_LOG_PATH not set; events will be printed to stdout") |
| 24 | + return s, nil |
| 25 | + } |
| 26 | + |
| 27 | + f, err := os.OpenFile(outPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + s.file = f |
| 33 | + s.w = bufio.NewWriterSize(f, 1<<20) |
| 34 | + log.Printf("collector: writing events to %s", outPath) |
| 35 | + return s, nil |
| 36 | +} |
| 37 | + |
| 38 | +// Close flushes and closes the underlying file if configured. |
| 39 | +func (s *Server) Close() { |
| 40 | + s.mu.Lock() |
| 41 | + defer s.mu.Unlock() |
| 42 | + |
| 43 | + if s.w != nil { |
| 44 | + _ = s.w.Flush() |
| 45 | + } |
| 46 | + if s.file != nil { |
| 47 | + _ = s.file.Close() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (s *Server) Mux() *http.ServeMux { |
| 52 | + mux := http.NewServeMux() |
| 53 | + mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { |
| 54 | + w.WriteHeader(http.StatusOK) |
| 55 | + _, _ = w.Write([]byte("ok")) |
| 56 | + }) |
| 57 | + mux.HandleFunc("/events", s.HandleEvents) |
| 58 | + return mux |
| 59 | +} |
| 60 | + |
| 61 | +func (s *Server) HandleEvents(w http.ResponseWriter, r *http.Request) { |
| 62 | + if r.Method != http.MethodPost { |
| 63 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + var ev MeteringEvent |
| 68 | + dec := json.NewDecoder(r.Body) |
| 69 | + dec.DisallowUnknownFields() |
| 70 | + if err := dec.Decode(&ev); err != nil { |
| 71 | + http.Error(w, "invalid json: "+err.Error(), http.StatusBadRequest) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + if ev.RequestID == "" { |
| 76 | + http.Error(w, "missing request_id", http.StatusBadRequest) |
| 77 | + return |
| 78 | + } |
| 79 | + if ev.At.IsZero() { |
| 80 | + ev.At = time.Now().UTC() |
| 81 | + } |
| 82 | + |
| 83 | + b, err := json.Marshal(ev) |
| 84 | + if err != nil { |
| 85 | + http.Error(w, "failed to marshal", http.StatusInternalServerError) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + s.mu.Lock() |
| 90 | + defer s.mu.Unlock() |
| 91 | + |
| 92 | + if s.w != nil { |
| 93 | + _, _ = s.w.Write(b) |
| 94 | + _, _ = s.w.WriteString("\n") |
| 95 | + _ = s.w.Flush() |
| 96 | + } else { |
| 97 | + log.Printf("EVENT %s", string(b)) |
| 98 | + } |
| 99 | + |
| 100 | + w.WriteHeader(http.StatusAccepted) |
| 101 | + _, _ = w.Write([]byte("accepted")) |
| 102 | +} |
0 commit comments