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/config"
	"github.com/uber/cadence/common/log"
	"github.com/uber/cadence/common/metrics"
	"github.com/uber/cadence/common/persistence"
)

// meteredHistoryManager implements persistence.HistoryManager interface instrumented with rate limiter.
type meteredHistoryManager struct {
	base
	wrapped persistence.HistoryManager
}

// NewHistoryManager creates a new instance of HistoryManager with ratelimiter.
func NewHistoryManager(
	wrapped persistence.HistoryManager,
	metricClient metrics.Client,
	logger log.Logger,
	cfg *config.Persistence,
) persistence.HistoryManager {
	return &meteredHistoryManager{
		wrapped: wrapped,
		base: base{
			metricClient:                  metricClient,
			logger:                        logger,
			enableLatencyHistogramMetrics: cfg.EnablePersistenceLatencyHistogramMetrics,
		},
	}
}

func (c *meteredHistoryManager) AppendHistoryNodes(ctx context.Context, request *persistence.AppendHistoryNodesRequest) (ap1 *persistence.AppendHistoryNodesResponse, err error) {
	op := func() error {
		ap1, err = c.wrapped.AppendHistoryNodes(ctx, request)
		c.emptyMetric("HistoryManager.AppendHistoryNodes", request, ap1, err)
		return err
	}

	err = c.call(metrics.PersistenceAppendHistoryNodesScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) Close() {
	c.wrapped.Close()
	return
}

func (c *meteredHistoryManager) DeleteHistoryBranch(ctx context.Context, request *persistence.DeleteHistoryBranchRequest) (err error) {
	op := func() error {
		err = c.wrapped.DeleteHistoryBranch(ctx, request)
		c.emptyMetric("HistoryManager.DeleteHistoryBranch", request, err, err)
		return err
	}

	err = c.call(metrics.PersistenceDeleteHistoryBranchScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) ForkHistoryBranch(ctx context.Context, request *persistence.ForkHistoryBranchRequest) (fp1 *persistence.ForkHistoryBranchResponse, err error) {
	op := func() error {
		fp1, err = c.wrapped.ForkHistoryBranch(ctx, request)
		c.emptyMetric("HistoryManager.ForkHistoryBranch", request, fp1, err)
		return err
	}

	err = c.call(metrics.PersistenceForkHistoryBranchScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) GetAllHistoryTreeBranches(ctx context.Context, request *persistence.GetAllHistoryTreeBranchesRequest) (gp1 *persistence.GetAllHistoryTreeBranchesResponse, err error) {
	op := func() error {
		gp1, err = c.wrapped.GetAllHistoryTreeBranches(ctx, request)
		c.emptyMetric("HistoryManager.GetAllHistoryTreeBranches", request, gp1, err)
		return err
	}

	err = c.call(metrics.PersistenceGetAllHistoryTreeBranchesScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) GetHistoryTree(ctx context.Context, request *persistence.GetHistoryTreeRequest) (gp1 *persistence.GetHistoryTreeResponse, err error) {
	op := func() error {
		gp1, err = c.wrapped.GetHistoryTree(ctx, request)
		c.emptyMetric("HistoryManager.GetHistoryTree", request, gp1, err)
		return err
	}

	err = c.call(metrics.PersistenceGetHistoryTreeScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) GetName() (s1 string) {
	return c.wrapped.GetName()
}

func (c *meteredHistoryManager) ReadHistoryBranch(ctx context.Context, request *persistence.ReadHistoryBranchRequest) (rp1 *persistence.ReadHistoryBranchResponse, err error) {
	op := func() error {
		rp1, err = c.wrapped.ReadHistoryBranch(ctx, request)
		c.emptyMetric("HistoryManager.ReadHistoryBranch", request, rp1, err)
		return err
	}

	err = c.call(metrics.PersistenceReadHistoryBranchScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) ReadHistoryBranchByBatch(ctx context.Context, request *persistence.ReadHistoryBranchRequest) (rp1 *persistence.ReadHistoryBranchByBatchResponse, err error) {
	op := func() error {
		rp1, err = c.wrapped.ReadHistoryBranchByBatch(ctx, request)
		c.emptyMetric("HistoryManager.ReadHistoryBranchByBatch", request, rp1, err)
		return err
	}

	err = c.call(metrics.PersistenceReadHistoryBranchByBatchScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredHistoryManager) ReadRawHistoryBranch(ctx context.Context, request *persistence.ReadHistoryBranchRequest) (rp1 *persistence.ReadRawHistoryBranchResponse, err error) {
	op := func() error {
		rp1, err = c.wrapped.ReadRawHistoryBranch(ctx, request)
		c.emptyMetric("HistoryManager.ReadRawHistoryBranch", request, rp1, err)
		return err
	}

	err = c.call(metrics.PersistenceReadRawHistoryBranchScope, op, getCustomMetricTags(request)...)
	return
}
