If your application needs to receive, handle, and store remote write requests, you can use remote.NewHandler.
To use this, you must implement a storage backend that satisfies the required interface (e.g., a Store method that accepts a context, message type, and HTTP request).
import (
"net/http"
"log"
"github.com/prometheus/client_golang/exp/api/remote"
)
// ...
type db {} // Your storage implementation
func NewStorage() *db {}
// Implement the storage interface
func (d *db) Store(ctx context.Context, msgType remote.WriteMessageType, req *http.Request) (*remote.WriteResponse, error) {}
// ...
mux := http.NewServeMux()
// Create the handler with your storage and optional logger
remoteWriteHandler := remote.NewHandler(storage, remote.WithHandlerLogger(logger.With("component", "remote_write_handler")))
// Register the handler at the standard remote write endpoint
mux.Handle("/api/v1/write", remoteWriteHandler)
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}