Skip to content

Commit 9964786

Browse files
authored
Add service type tag to metrics (#2545)
1 parent f92cc93 commit 9964786

23 files changed

+155
-43
lines changed

common/archiver/s3store/historyArchiver_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ func (s *historyArchiverSuite) TearDownSuite() {
103103
func (s *historyArchiverSuite) SetupTest() {
104104
scope := tally.NewTestScope("test", nil)
105105
s.Assertions = require.New(s.T())
106+
metricsClient, err := metrics.NewClient(&metrics.ClientConfig{}, scope, metrics.History)
107+
s.Require().NoError(err, "metrics.NewClient")
106108
s.container = &archiver.HistoryBootstrapContainer{
107109
Logger: log.NewNoopLogger(),
108-
MetricsClient: metrics.NewClient(&metrics.ClientConfig{}, scope, metrics.HistoryArchiverScope),
110+
MetricsClient: metricsClient,
109111
}
110112

111113
s.controller = gomock.NewController(s.T())

common/archiver/s3store/visibilityArchiver_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ func (s *visibilityArchiverSuite) SetupSuite() {
137137

138138
s.testArchivalURI, err = archiver.NewURI(testBucketURI)
139139
s.Require().NoError(err)
140-
140+
metricsClient, err := metrics.NewClient(&metrics.ClientConfig{}, scope, metrics.History)
141+
s.Require().NoError(err, "metrics.NewClient")
141142
s.container = &archiver.VisibilityBootstrapContainer{
142143
Logger: log.NewNoopLogger(),
143-
MetricsClient: metrics.NewClient(&metrics.ClientConfig{}, scope, metrics.VisibilityArchiverScope),
144+
MetricsClient: metricsClient,
144145
}
145146
}
146147

common/metrics/common.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,32 @@ func GetMetricsServiceIdx(serviceName string, logger log.Logger) ServiceIdx {
6767
return Matching
6868
case primitives.WorkerService:
6969
return Worker
70+
case primitives.ServerService:
71+
return Server
72+
case primitives.UnitTestService:
73+
return UnitTestService
7074
default:
7175
logger.Fatal("Unknown service name for metrics!", tag.Service(serviceName))
7276
panic(fmt.Sprintf("Unknown service name for metrics: %s", serviceName))
7377
}
7478
}
79+
80+
// GetMetricsServiceIdx returns service id corresponding to serviceName
81+
func MetricsServiceIdxToServiceName(serviceIdx ServiceIdx) (string, error) {
82+
switch serviceIdx {
83+
case Server:
84+
return primitives.ServerService, nil
85+
case Frontend:
86+
return primitives.FrontendService, nil
87+
case History:
88+
return primitives.HistoryService, nil
89+
case Matching:
90+
return primitives.MatchingService, nil
91+
case Worker:
92+
return primitives.WorkerService, nil
93+
case UnitTestService:
94+
return primitives.UnitTestService, nil
95+
default:
96+
return "", fmt.Errorf(fmt.Sprintf("Unknown service idx for metrics: %d", serviceIdx))
97+
}
98+
}

common/metrics/metric_client_tests.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929

3030
"github.com/stretchr/testify/assert"
3131
"github.com/stretchr/testify/suite"
32+
33+
"go.temporal.io/server/common/primitives"
3234
)
3335

3436
type (
@@ -73,7 +75,7 @@ func (s *MetricTestSuiteBase) TestClientReportCounter() {
7375
TestScope1,
7476
TestCounterMetric1, 66)
7577
testDef := MetricDefs[UnitTestService][TestCounterMetric1]
76-
assert.NoError(s.T(), s.metricTestUtility.ContainsCounter(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, 66))
78+
assert.NoError(s.T(), s.metricTestUtility.ContainsCounter(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, 66))
7779
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
7880
}
7981

@@ -82,7 +84,7 @@ func (s *MetricTestSuiteBase) TestClientReportGauge() {
8284
TestScope1,
8385
TestGaugeMetric1, 66)
8486
testDef := MetricDefs[UnitTestService][TestGaugeMetric1]
85-
assert.NoError(s.T(), s.metricTestUtility.ContainsGauge(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, 66))
87+
assert.NoError(s.T(), s.metricTestUtility.ContainsGauge(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, 66))
8688
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
8789
}
8890

@@ -92,7 +94,7 @@ func (s *MetricTestSuiteBase) TestClientReportTimer() {
9294
TestScope1,
9395
TestTimerMetric1, targetDuration)
9496
testDef := MetricDefs[UnitTestService][TestTimerMetric1]
95-
assert.NoError(s.T(), s.metricTestUtility.ContainsTimer(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, targetDuration))
97+
assert.NoError(s.T(), s.metricTestUtility.ContainsTimer(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, targetDuration))
9698
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
9799
}
98100

@@ -101,28 +103,28 @@ func (s *MetricTestSuiteBase) TestClientReportHistogram() {
101103
TestScope1,
102104
TestDimensionlessHistogramMetric1, 66)
103105
testDef := MetricDefs[UnitTestService][TestDimensionlessHistogramMetric1]
104-
assert.NoError(s.T(), s.metricTestUtility.ContainsHistogram(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, 66))
106+
assert.NoError(s.T(), s.metricTestUtility.ContainsHistogram(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, 66))
105107
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
106108
}
107109

108110
func (s *MetricTestSuiteBase) TestScopeReportCounter() {
109111
s.testClient.Scope(TestScope1).AddCounter(TestCounterMetric1, 66)
110112
testDef := MetricDefs[UnitTestService][TestCounterMetric1]
111-
assert.NoError(s.T(), s.metricTestUtility.ContainsCounter(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, 66))
113+
assert.NoError(s.T(), s.metricTestUtility.ContainsCounter(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, 66))
112114
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
113115
}
114116

115117
func (s *MetricTestSuiteBase) TestScopeReportGauge() {
116118
s.testClient.Scope(TestScope1).UpdateGauge(TestGaugeMetric1, 66)
117119
testDef := MetricDefs[UnitTestService][TestGaugeMetric1]
118-
assert.NoError(s.T(), s.metricTestUtility.ContainsGauge(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, 66))
120+
assert.NoError(s.T(), s.metricTestUtility.ContainsGauge(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, 66))
119121
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
120122
}
121123

122124
func (s *MetricTestSuiteBase) TestScopeReportTimer() {
123125
targetDuration := time.Second * 100
124126
s.testClient.Scope(TestScope1).RecordTimer(TestTimerMetric1, targetDuration)
125127
testDef := MetricDefs[UnitTestService][TestTimerMetric1]
126-
assert.NoError(s.T(), s.metricTestUtility.ContainsTimer(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation}, targetDuration))
128+
assert.NoError(s.T(), s.metricTestUtility.ContainsTimer(testDef.metricName, map[string]string{namespace: namespaceAllValue, OperationTagName: ScopeDefs[UnitTestService][TestScope1].operation, serviceName: primitives.UnitTestService}, targetDuration))
127129
assert.Equal(s.T(), 1, s.metricTestUtility.CollectionSize())
128130
}

common/metrics/opentelemetry_client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package metrics
2626

2727
import (
28+
"fmt"
2829
"time"
2930

3031
"go.temporal.io/server/common/log"
@@ -55,6 +56,17 @@ func NewOpentelemeteryClient(clientConfig *ClientConfig, serviceIdx ServiceIdx,
5556

5657
globalRootScope := newOpentelemetryScope(serviceIdx, reporter.GetMeterMust(), nil, clientConfig.Tags, getMetricDefs(serviceIdx), false, gaugeCache, false)
5758

59+
serviceTypeTagValue, err := MetricsServiceIdxToServiceName(serviceIdx)
60+
if err != nil {
61+
return nil, fmt.Errorf("failed to initialize metrics client: %w", err)
62+
}
63+
64+
rootTags := make(map[string]string, len(clientConfig.Tags)+1)
65+
for k, v := range clientConfig.Tags {
66+
rootTags[k] = v
67+
}
68+
rootTags[serviceName] = serviceTypeTagValue
69+
5870
totalScopes := len(ScopeDefs[Common]) + len(ScopeDefs[serviceIdx])
5971
metricsClient := &opentelemetryClient{
6072
rootScope: globalRootScope,

common/metrics/opentelemetry_reporter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (r *opentelemetryReporterImpl) GetMeterMust() metric.MeterMust {
9393
}
9494

9595
func (r *opentelemetryReporterImpl) NewClient(logger log.Logger, serviceIdx ServiceIdx) (Client, error) {
96-
9796
return NewOpentelemeteryClient(r.clientConfig, serviceIdx, r, logger, r.gaugeCache)
9897
}
9998

common/metrics/tags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
workflowType = "workflowType"
4747
activityType = "activityType"
4848
commandType = "commandType"
49+
serviceName = "service_name"
4950

5051
namespaceAllValue = "all"
5152
unknownValue = "_unknown_"
@@ -227,3 +228,7 @@ func HttpStatusTag(value int) Tag {
227228
func ResourceExhaustedCauseTag(cause enumspb.ResourceExhaustedCause) Tag {
228229
return &tagImpl{key: resourceExhaustedTag, value: cause.String()}
229230
}
231+
232+
func ServiceTypeTag(value string) Tag {
233+
return &tagImpl{key: serviceName, value: value}
234+
}

common/metrics/tally_client.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
package metrics
2626

2727
import (
28+
"fmt"
2829
"time"
2930

3031
"github.com/uber-go/tally/v4"
3132
)
3233

34+
var _ Client = (*TallyClient)(nil)
35+
3336
// TallyClient is used for reporting metrics by various Temporal services
3437
type TallyClient struct {
3538
// This is the scope provided by user to the client. It contains no client-specific tags.
@@ -46,7 +49,7 @@ type TallyClient struct {
4649
// Client implementation
4750
// reporter holds the common tags for the service
4851
// serviceIdx indicates the service type in (InputhostIndex, ... StorageIndex)
49-
func NewClient(clientConfig *ClientConfig, scope tally.Scope, serviceIdx ServiceIdx) Client {
52+
func NewClient(clientConfig *ClientConfig, scope tally.Scope, serviceIdx ServiceIdx) (Client, error) {
5053
tagsFilterConfig := NewTagFilteringScopeConfig(clientConfig.ExcludeTags)
5154

5255
perUnitBuckets := make(map[MetricUnit]tally.Buckets)
@@ -58,6 +61,13 @@ func NewClient(clientConfig *ClientConfig, scope tally.Scope, serviceIdx Service
5861
return NewTagFilteringScope(tagsFilterConfig, impl)
5962
}
6063

64+
serviceTypeTagValue, err := MetricsServiceIdxToServiceName(serviceIdx)
65+
if err != nil {
66+
return nil, fmt.Errorf("failed to initialize metrics client: %w", err)
67+
}
68+
69+
rootScope := scope.Tagged(map[string]string{serviceName: serviceTypeTagValue})
70+
6171
totalScopes := len(ScopeDefs[Common]) + len(ScopeDefs[serviceIdx])
6272
metricsClient := &TallyClient{
6373
globalRootScope: scope,
@@ -75,7 +85,7 @@ func NewClient(clientConfig *ClientConfig, scope tally.Scope, serviceIdx Service
7585
namespace: namespaceAllValue,
7686
}
7787
mergeMapToRight(def.tags, scopeTags)
78-
metricsClient.childScopes[idx] = scope.Tagged(scopeTags)
88+
metricsClient.childScopes[idx] = rootScope.Tagged(scopeTags)
7989
}
8090

8191
for idx, def := range ScopeDefs[serviceIdx] {
@@ -84,10 +94,10 @@ func NewClient(clientConfig *ClientConfig, scope tally.Scope, serviceIdx Service
8494
namespace: namespaceAllValue,
8595
}
8696
mergeMapToRight(def.tags, scopeTags)
87-
metricsClient.childScopes[idx] = scope.Tagged(scopeTags)
97+
metricsClient.childScopes[idx] = rootScope.Tagged(scopeTags)
8898
}
8999

90-
return metricsClient
100+
return metricsClient, nil
91101
}
92102

93103
// IncCounter increments one for a counter and emits

common/metrics/tally_metric_test_utility.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func NewTallyMetricTestUtility() *TallyMetricTestUtility {
4646
}
4747

4848
func (t *TallyMetricTestUtility) GetClient(config *ClientConfig, idx ServiceIdx) Client {
49-
return NewClient(config, t.scope, idx)
49+
result, err := NewClient(config, t.scope, idx)
50+
if err != nil {
51+
panic(err)
52+
}
53+
return result
5054
}
5155

5256
func (t *TallyMetricTestUtility) ContainsCounter(name MetricName, labels map[string]string, value int64) error {

common/metrics/tally_reporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewTallyReporter(
5151
}
5252

5353
func (tr *TallyReporter) NewClient(logger log.Logger, serviceIdx ServiceIdx) (Client, error) {
54-
return NewClient(tr.clientConfig, tr.scope, serviceIdx), nil
54+
return NewClient(tr.clientConfig, tr.scope, serviceIdx)
5555
}
5656

5757
func (tr *TallyReporter) GetScope() tally.Scope {

0 commit comments

Comments
 (0)