1414 * limitations under the License.
1515 */
1616
17- import { Observable } from '@opentelemetry/api' ;
17+ import { Observable , diag } from '@opentelemetry/api' ;
1818import * as assert from 'assert' ;
19+ import * as sinon from 'sinon' ;
1920import {
2021 CounterInstrument ,
2122 HistogramInstrument ,
@@ -27,78 +28,86 @@ import {
2728import { Meter } from '../src/Meter' ;
2829import { MeterProviderSharedState } from '../src/state/MeterProviderSharedState' ;
2930import { MeterSharedState } from '../src/state/MeterSharedState' ;
30- import { defaultInstrumentationScope , defaultResource } from './util' ;
31+ import {
32+ defaultInstrumentationScope ,
33+ defaultResource ,
34+ invalidNames ,
35+ validNames ,
36+ } from './util' ;
3137
3238describe ( 'Meter' , ( ) => {
39+ afterEach ( ( ) => {
40+ sinon . restore ( ) ;
41+ } ) ;
42+
3343 describe ( 'createCounter' , ( ) => {
34- it ( 'should create counter', ( ) => {
44+ testWithNames ( ' counter', name => {
3545 const meterSharedState = new MeterSharedState (
3646 new MeterProviderSharedState ( defaultResource ) ,
3747 defaultInstrumentationScope
3848 ) ;
3949 const meter = new Meter ( meterSharedState ) ;
40- const counter = meter . createCounter ( 'foobar' ) ;
50+ const counter = meter . createCounter ( name ) ;
4151 assert ( counter instanceof CounterInstrument ) ;
4252 } ) ;
4353 } ) ;
4454
4555 describe ( 'createUpDownCounter' , ( ) => {
46- it ( 'should create up down counter ', ( ) => {
56+ testWithNames ( 'UpDownCounter ', name => {
4757 const meterSharedState = new MeterSharedState (
4858 new MeterProviderSharedState ( defaultResource ) ,
4959 defaultInstrumentationScope
5060 ) ;
5161 const meter = new Meter ( meterSharedState ) ;
52- const upDownCounter = meter . createUpDownCounter ( 'foobar' ) ;
62+ const upDownCounter = meter . createUpDownCounter ( name ) ;
5363 assert ( upDownCounter instanceof UpDownCounterInstrument ) ;
5464 } ) ;
5565 } ) ;
5666
5767 describe ( 'createHistogram' , ( ) => {
58- it ( 'should create histogram ', ( ) => {
68+ testWithNames ( 'Histogram ', name => {
5969 const meterSharedState = new MeterSharedState (
6070 new MeterProviderSharedState ( defaultResource ) ,
6171 defaultInstrumentationScope
6272 ) ;
6373 const meter = new Meter ( meterSharedState ) ;
64- const histogram = meter . createHistogram ( 'foobar' ) ;
74+ const histogram = meter . createHistogram ( name ) ;
6575 assert ( histogram instanceof HistogramInstrument ) ;
6676 } ) ;
6777 } ) ;
6878
6979 describe ( 'createObservableGauge' , ( ) => {
70- it ( 'should create observable gauge ', ( ) => {
80+ testWithNames ( 'ObservableGauge ', name => {
7181 const meterSharedState = new MeterSharedState (
7282 new MeterProviderSharedState ( defaultResource ) ,
7383 defaultInstrumentationScope
7484 ) ;
7585 const meter = new Meter ( meterSharedState ) ;
76- const observableGauge = meter . createObservableGauge ( 'foobar' ) ;
86+ const observableGauge = meter . createObservableGauge ( name ) ;
7787 assert ( observableGauge instanceof ObservableGaugeInstrument ) ;
7888 } ) ;
7989 } ) ;
8090
8191 describe ( 'createObservableCounter' , ( ) => {
82- it ( 'should create observable counter ', ( ) => {
92+ testWithNames ( 'ObservableCounter ', name => {
8393 const meterSharedState = new MeterSharedState (
8494 new MeterProviderSharedState ( defaultResource ) ,
8595 defaultInstrumentationScope
8696 ) ;
8797 const meter = new Meter ( meterSharedState ) ;
88- const observableCounter = meter . createObservableCounter ( 'foobar' ) ;
98+ const observableCounter = meter . createObservableCounter ( name ) ;
8999 assert ( observableCounter instanceof ObservableCounterInstrument ) ;
90100 } ) ;
91101 } ) ;
92102
93103 describe ( 'createObservableUpDownCounter' , ( ) => {
94- it ( 'should create observable up-down-counter ', ( ) => {
104+ testWithNames ( 'ObservableUpDownCounter ', name => {
95105 const meterSharedState = new MeterSharedState (
96106 new MeterProviderSharedState ( defaultResource ) ,
97107 defaultInstrumentationScope
98108 ) ;
99109 const meter = new Meter ( meterSharedState ) ;
100- const observableUpDownCounter =
101- meter . createObservableUpDownCounter ( 'foobar' ) ;
110+ const observableUpDownCounter = meter . createObservableUpDownCounter ( name ) ;
102111 assert (
103112 observableUpDownCounter instanceof ObservableUpDownCounterInstrument
104113 ) ;
@@ -167,3 +176,22 @@ describe('Meter', () => {
167176 } ) ;
168177 } ) ;
169178} ) ;
179+
180+ function testWithNames ( type : string , tester : ( name : string ) => void ) {
181+ for ( const invalidName of invalidNames ) {
182+ it ( `should warn with invalid name ${ invalidName } for ${ type } ` , ( ) => {
183+ const warnStub = sinon . spy ( diag , 'warn' ) ;
184+ tester ( invalidName ) ;
185+ assert . strictEqual ( warnStub . callCount , 1 ) ;
186+ assert . ok ( warnStub . calledWithMatch ( 'Invalid metric name' ) ) ;
187+ } ) ;
188+ }
189+
190+ for ( const validName of validNames ) {
191+ it ( `should not warn with valid name ${ validName } for ${ type } ` , ( ) => {
192+ const warnStub = sinon . spy ( diag , 'warn' ) ;
193+ tester ( validName ) ;
194+ assert . strictEqual ( warnStub . callCount , 0 ) ;
195+ } ) ;
196+ }
197+ }
0 commit comments