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 executormetricsExecutor struct {
	handler       handler.Executor
	logger        log.Logger
	metricsClient metrics.Client
}

func NewExecutorMetricsExecutor(handler handler.Executor, logger log.Logger, metricsClient metrics.Client) handler.Executor {
	return &executormetricsExecutor{
		handler:       handler,
		logger:        logger,
		metricsClient: metricsClient,
	}
}

func (h *executormetricsExecutor) Heartbeat(ctx context.Context, ep1 *types.ExecutorHeartbeatRequest) (ep2 *types.ExecutorHeartbeatResponse, err error) {
	defer func() { log.CapturePanic(recover(), h.logger, &err) }()

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

	ep2, err = h.handler.Heartbeat(ctx, ep1)

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

	return ep2, err
}
