2021-03-28 19:31:03 +03:00
|
|
|
package handler
|
2021-03-23 17:26:29 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// guard to fail early
|
|
|
|
_ MeterServer = &handler{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
meter meter.Meter
|
|
|
|
opts []meter.Option
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(meter meter.Meter, opts ...meter.Option) *handler {
|
|
|
|
return &handler{meter: meter, opts: opts}
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:34:34 +03:00
|
|
|
func (h *handler) Metrics(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
2021-03-23 17:26:29 +03:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if err := h.meter.Write(buf, h.opts...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Data = buf.Bytes()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|