| 1 |
# client_golang experimental module |
| 2 |
|
| 3 |
Contains experimental utilities and APIs for Prometheus. |
| 4 |
The module may be contain breaking changes or be removed in the future. |
| 5 |
|
| 6 |
Packages within this module are listed below. |
| 7 |
|
| 8 |
## Remote |
| 9 |
|
| 10 |
Implements bindings from Prometheus remote APIs (remote write v1 and v2 for now). |
| 11 |
|
| 12 |
Contains flexible method for building API clients, that can send remote write protocol messages. |
| 13 |
|
| 14 |
```go |
| 15 |
import ( |
| 16 |
"github.com/prometheus/client_golang/exp/api/remote" |
| 17 |
) |
| 18 |
... |
| 19 |
|
| 20 |
remoteAPI, err := remote.NewAPI( |
| 21 |
"https://your-remote-endpoint", |
| 22 |
remote.WithAPIHTTPClient(httpClient), |
| 23 |
remote.WithAPILogger(logger.With("component", "remote_write_api")), |
| 24 |
) |
| 25 |
... |
| 26 |
|
| 27 |
stats, err := remoteAPI.Write(ctx, remote.WriteV2MessageType, protoWriteReq) |
| 28 |
``` |
| 29 |
|
| 30 |
Also contains handler methods for applications that would like to handle and store remote write requests. |
| 31 |
|
| 32 |
```go |
| 33 |
import ( |
| 34 |
"net/http" |
| 35 |
"log" |
| 36 |
|
| 37 |
"github.com/prometheus/client_golang/exp/api/remote" |
| 38 |
) |
| 39 |
... |
| 40 |
|
| 41 |
type db {} |
| 42 |
|
| 43 |
func NewStorage() *db {} |
| 44 |
|
| 45 |
func (d *db) Store(ctx context.Context, msgType remote.WriteMessageType, req *http.Request) (*remote.WriteResponse, error) {} |
| 46 |
... |
| 47 |
|
| 48 |
mux := http.NewServeMux() |
| 49 |
|
| 50 |
remoteWriteHandler := remote.NewHandler(storage, remote.WithHandlerLogger(logger.With("component", "remote_write_handler"))) |
| 51 |
mux.Handle("/api/v1/write", remoteWriteHandler) |
| 52 |
|
| 53 |
server := &http.Server{ |
| 54 |
Addr: ":8080", |
| 55 |
Handler: mux, |
| 56 |
} |
| 57 |
if err := server.ListenAndServe(); err != nil { |
| 58 |
log.Fatal(err) |
| 59 |
} |
| 60 |
``` |
| 61 |
|
| 62 |
For more details, see [go doc](https://pkg.go.dev/github.com/prometheus/client_golang/exp/api/remote). |