package metered

// Code generated by gowrap. DO NOT EDIT.
// template: ../../templates/metered.tmpl
// gowrap: http://github.com/hexdigest/gowrap

import (
	"context"

	"github.com/uber/cadence/common/log"
	"github.com/uber/cadence/common/log/tag"
	"github.com/uber/cadence/common/metrics"
	"github.com/uber/cadence/common/types"
	"github.com/uber/cadence/service/sharddistributor/handler"
)

type metricsHandler struct {
	handler       handler.Handler
	logger        log.Logger
	metricsClient metrics.Client
}

func NewMetricsHandler(handler handler.Handler, logger log.Logger, metricsClient metrics.Client) handler.Handler {
	return &metricsHandler{
		handler:       handler,
		logger:        logger,
		metricsClient: metricsClient,
	}
}

func (h *metricsHandler) GetShardOwner(ctx context.Context, gp1 *types.GetShardOwnerRequest) (gp2 *types.GetShardOwnerResponse, err error) {
	defer func() { log.CapturePanic(recover(), h.logger, &err) }()

	scope := h.metricsClient.Scope(metrics.ShardDistributorGetShardOwnerScope)
	scope = scope.Tagged(metrics.NamespaceTag(gp1.GetNamespace()))
	scope.IncCounter(metrics.ShardDistributorRequests)
	sw := scope.StartTimer(metrics.ShardDistributorLatency)
	defer sw.Stop()
	logger := h.logger.WithTags(tag.ShardNamespace(gp1.GetNamespace()))

	gp2, err = h.handler.GetShardOwner(ctx, gp1)

	if err != nil {
		handleErr(err, scope, logger)
	}

	return gp2, err
}

func (h *metricsHandler) Health(ctx context.Context) (hp1 *types.HealthStatus, err error) {
	return h.handler.Health(ctx)
}

func (h *metricsHandler) Start() {
	h.handler.Start()
	return
}

func (h *metricsHandler) Stop() {
	h.handler.Stop()
	return
}
