1+ /**
2+ * Copyright 2018, OpenCensus Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import * as assert from 'assert' ;
18+
19+ import { AggregationType , Measure , MeasureType , MeasureUnit , Stats , Tags , View } from '../src' ;
20+ import { MetricDescriptorType } from '../src/metrics/export/types' ;
21+ import { MetricProducerForStats } from '../src/stats/metric-producer' ;
22+
23+ describe ( 'Metric producer for stats' , ( ) => {
24+ interface AggregationTestCase {
25+ aggregationType : AggregationType ;
26+ description : string ;
27+ metricDescriptorType : MetricDescriptorType ;
28+ }
29+ const aggregationTestCases : AggregationTestCase [ ] = [
30+ {
31+ aggregationType : AggregationType . SUM ,
32+ description : 'Sum' ,
33+ metricDescriptorType : MetricDescriptorType . CUMULATIVE_DOUBLE
34+ } ,
35+ {
36+ aggregationType : AggregationType . COUNT ,
37+ description : 'Count' ,
38+ metricDescriptorType : MetricDescriptorType . CUMULATIVE_INT64
39+ } ,
40+ {
41+ aggregationType : AggregationType . LAST_VALUE ,
42+ description : 'Last Value' ,
43+ metricDescriptorType : MetricDescriptorType . GAUGE_DOUBLE
44+ } ,
45+ {
46+ aggregationType : AggregationType . DISTRIBUTION ,
47+ description : 'Distribution' ,
48+ metricDescriptorType : MetricDescriptorType . CUMULATIVE_DISTRIBUTION
49+ }
50+ ] ;
51+ const measure : Measure = {
52+ name : 'Test Measure' ,
53+ type : MeasureType . DOUBLE ,
54+ unit : MeasureUnit . UNIT
55+ } ;
56+
57+ let stats : Stats ;
58+ let metricProducerForStats : MetricProducerForStats ;
59+
60+ beforeEach ( ( ) => {
61+ stats = new Stats ( ) ;
62+ metricProducerForStats = new MetricProducerForStats ( stats ) ;
63+ } ) ;
64+
65+ describe ( 'Metric producer' , ( ) => {
66+ const tags : Tags = { testKey1 : 'testValue' , testKey2 : 'testValue' } ;
67+ const measurementValues = [ 1.1 , 2.3 , 3.2 , 4.3 , 5.2 ] ;
68+ const buckets = [ 2 , 4 , 6 ] ;
69+
70+ describe ( 'getMetrics()' , ( ) => {
71+ // Detailed coverage in test-viev.ts
72+ for ( const aggregation of aggregationTestCases ) {
73+ it ( `should return list of metrics for ${
74+ aggregation . aggregationType } aggregation`,
75+ ( ) => {
76+ const view : View = stats . createView (
77+ 'test/view/name' , measure , aggregation . aggregationType ,
78+ Object . keys ( tags ) , 'test description' , buckets ) ;
79+ for ( const value of measurementValues ) {
80+ const measurement = { measure, tags, value} ;
81+ view . recordMeasurement ( measurement ) ;
82+ }
83+
84+ const metrics = metricProducerForStats . getMetrics ( ) ;
85+
86+ assert . strictEqual ( metrics . length , 1 ) ;
87+ const [ { descriptor, timeseries} ] = metrics ;
88+
89+ assert . deepStrictEqual ( descriptor , {
90+ name : 'test/view/name' ,
91+ description : 'test description' ,
92+ 'labelKeys' : [ { 'key' : 'testKey1' } , { 'key' : 'testKey2' } ] ,
93+ unit : MeasureUnit . UNIT ,
94+ type : aggregation . metricDescriptorType ,
95+ } ) ;
96+ assert . strictEqual ( timeseries . length , 1 ) ;
97+ } ) ;
98+ }
99+ } ) ;
100+ } ) ;
101+ } ) ;
0 commit comments