Files
tasks/client/history/metric_client.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

113 lines
3.7 KiB
Go

package history
import (
"context"
"time"
"go.temporal.io/api/serviceerror"
"github.com/hanzoai/tasks/api/historyservice/v1"
"github.com/hanzoai/tasks/common/headers"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/metrics"
"google.golang.org/grpc"
)
var _ historyservice.HistoryServiceClient = (*metricClient)(nil)
type metricClient struct {
client historyservice.HistoryServiceClient
metricsHandler metrics.Handler
logger log.Logger
throttledLogger log.Logger
}
// NewMetricClient creates a new instance of historyservice.HistoryServiceClient that emits metrics
func NewMetricClient(
client historyservice.HistoryServiceClient,
metricsHandler metrics.Handler,
logger log.Logger,
throttledLogger log.Logger,
) historyservice.HistoryServiceClient {
return &metricClient{
client: client,
metricsHandler: metricsHandler,
logger: logger,
throttledLogger: throttledLogger,
}
}
func (c *metricClient) StreamWorkflowReplicationMessages(
ctx context.Context,
opts ...grpc.CallOption,
) (_ historyservice.HistoryService_StreamWorkflowReplicationMessagesClient, retError error) {
metricsHandler, startTime := c.startMetricsRecording(ctx, metrics.HistoryClientStreamWorkflowReplicationMessagesScope)
defer func() {
c.finishMetricsRecording(metricsHandler, startTime, retError)
}()
return c.client.StreamWorkflowReplicationMessages(ctx, opts...)
}
func (c *metricClient) StartNexusOperation(
ctx context.Context,
request *historyservice.StartNexusOperationRequest,
opts ...grpc.CallOption,
) (_ *historyservice.StartNexusOperationResponse, retError error) {
op := "HistoryClientStartNexusOperation_" + request.GetRequest().GetService() + "_" + request.GetRequest().GetOperation()
metricsHandler, startTime := c.startMetricsRecording(ctx, op)
defer func() {
c.finishMetricsRecording(metricsHandler, startTime, retError)
}()
return c.client.StartNexusOperation(ctx, request, opts...)
}
func (c *metricClient) CancelNexusOperation(
ctx context.Context,
request *historyservice.CancelNexusOperationRequest,
opts ...grpc.CallOption,
) (_ *historyservice.CancelNexusOperationResponse, retError error) {
op := "HistoryClientCancelNexusOperation_" + request.GetRequest().GetService() + "_" + request.GetRequest().GetOperation()
metricsHandler, startTime := c.startMetricsRecording(ctx, op)
defer func() {
c.finishMetricsRecording(metricsHandler, startTime, retError)
}()
return c.client.CancelNexusOperation(ctx, request, opts...)
}
func (c *metricClient) startMetricsRecording(
ctx context.Context,
operation string,
) (metrics.Handler, time.Time) {
caller := headers.GetCallerInfo(ctx).CallerName
metricsHandler := c.metricsHandler.WithTags(metrics.OperationTag(operation), metrics.NamespaceTag(caller), metrics.ServiceRoleTag(metrics.HistoryRoleTagValue))
metrics.ClientRequests.With(metricsHandler).Record(1)
return metricsHandler, time.Now().UTC()
}
func (c *metricClient) finishMetricsRecording(
metricsHandler metrics.Handler,
startTime time.Time,
err error,
) {
if err != nil {
switch err.(type) {
case *serviceerror.Canceled,
*serviceerror.DeadlineExceeded,
*serviceerror.NotFound,
*serviceerror.QueryFailed,
*serviceerror.NamespaceNotFound,
*serviceerror.WorkflowNotReady,
*serviceerror.WorkflowExecutionAlreadyStarted:
// noop - not interest and too many logs
default:
c.throttledLogger.Info("history client encountered error", tag.Error(err), tag.ServiceErrorType(err))
}
metrics.ClientFailures.With(metricsHandler).Record(1, metrics.ServiceErrorTypeTag(err))
}
metrics.ClientLatency.With(metricsHandler).Record(time.Since(startTime))
}