1 // Copyright (c) 2017 Uber Technologies, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 "github.com/stretchr/testify/assert"
27 "github.com/uber/jaeger-lib/metrics"
28 "github.com/uber/jaeger-lib/metrics/testutils"
31 // E.g. tags("key", "value", "key", "value")
32 func tags(kv
...string) map[string]string {
33 m
:= make(map[string]string)
34 for i
:= 0; i
< len(kv
)-1; i
+= 2 {
40 func endpointTags(endpoint
string, kv
...string) map[string]string {
41 return tags(append([]string{"endpoint", endpoint
}, kv
...)...)
44 func TestMetricsByEndpoint(t
*testing
.T
) {
45 met
:= metrics
.NewLocalFactory(0)
46 mbe
:= newMetricsByEndpoint(met
, DefaultNameNormalizer
, 2)
49 m2
:= mbe
.get("abc1") // from cache
50 m2a
:= mbe
.getWithWriteLock("abc1") // from cache in double-checked lock
51 assert
.Equal(t
, m1
, m2
)
52 assert
.Equal(t
, m1
, m2a
)
55 m4
:= mbe
.get("overflow")
56 m5
:= mbe
.get("overflow2")
58 for _
, m
:= range []*Metrics
{m1
, m2
, m2a
, m3
, m4
, m5
} {
59 m
.RequestCountSuccess
.Inc(1)
62 testutils
.AssertCounterMetrics(t
, met
,
63 testutils
.ExpectedMetric
{Name
: "requests", Tags
: endpointTags("abc1", "error", "false"), Value
: 3},
64 testutils
.ExpectedMetric
{Name
: "requests", Tags
: endpointTags("abc3", "error", "false"), Value
: 1},
65 testutils
.ExpectedMetric
{Name
: "requests", Tags
: endpointTags("other", "error", "false"), Value
: 2},