github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/metrics/logging/reporter_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/tickoalcantara12/micro/v3/metrics/logging/reporter_test.go 14 15 package logging 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/tickoalcantara12/micro/v3/service/metrics" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestLoggingReporter(t *testing.T) { 26 27 // Make a Reporter: 28 reporter := New(metrics.Path("/prometheus"), metrics.DefaultTags(map[string]string{"service": "prometheus-test"})) 29 assert.NotNil(t, reporter) 30 assert.Equal(t, "prometheus-test", reporter.options.DefaultTags["service"]) 31 assert.Equal(t, ":9000", reporter.options.Address) 32 assert.Equal(t, "/prometheus", reporter.options.Path) 33 34 // Check that our implementation is valid: 35 assert.Implements(t, new(metrics.Reporter), reporter) 36 37 // Test tag conversion: 38 tags := metrics.Tags{ 39 "tag1": "false", 40 "tag2": "true", 41 } 42 convertedTags := convertTags(tags) 43 assert.Equal(t, "false", convertedTags["tag1"]) 44 assert.Equal(t, "true", convertedTags["tag2"]) 45 46 // Test submitting metrics through the interface methods: 47 assert.NoError(t, reporter.Count("test.counter.1", 6, tags)) 48 assert.NoError(t, reporter.Count("test.counter.2", 19, tags)) 49 assert.NoError(t, reporter.Count("test.counter.1", 5, tags)) 50 assert.NoError(t, reporter.Gauge("test.gauge.1", 99, tags)) 51 assert.NoError(t, reporter.Gauge("test.gauge.2", 55, tags)) 52 assert.NoError(t, reporter.Gauge("test.gauge.1", 98, tags)) 53 assert.NoError(t, reporter.Timing("test.timing.1", time.Second, tags)) 54 assert.NoError(t, reporter.Timing("test.timing.2", time.Minute, tags)) 55 }