1414 * limitations under the License.
1515 */
1616
17- import { AggregationType , DistributionData , logger , Logger , Measurement , MeasureType , StatsEventListener , Tags , View } from '@opencensus/core' ;
17+ import { AggregationType , DistributionData , logger , Logger , Measure , Measurement , MeasureType , MeasureUnit , StatsEventListener , Tags , View } from '@opencensus/core' ;
1818import { auth , JWT } from 'google-auth-library' ;
1919import { google } from 'googleapis' ;
2020import * as path from 'path' ;
@@ -189,7 +189,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
189189 if ( aggregationData . type === AggregationType . DISTRIBUTION ) {
190190 value = { distributionValue : this . createDistribution ( aggregationData ) } ;
191191 } else if ( view . measure . type === MeasureType . INT64 ) {
192- value = { int64Value : aggregationData . value . toString ( ) } ;
192+ value = { int64Value : aggregationData . value } ;
193193 } else {
194194 value = { doubleValue : aggregationData . value } ;
195195 }
@@ -198,7 +198,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
198198 metric : { type : this . getMetricType ( view . name ) , labels : tags } ,
199199 resource : { type : 'global' , labels : resourceLabels } ,
200200 metricKind : this . createMetricKind ( view . aggregation ) ,
201- valueType : this . createValueType ( view ) ,
201+ valueType : this . createValueType ( view . aggregation , view . measure ) ,
202202 points : [ { interval : { startTime, endTime} , value} ]
203203 } ;
204204 }
@@ -209,7 +209,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
209209 */
210210 private createDistribution ( distribution : DistributionData ) : Distribution {
211211 return {
212- count : distribution . count . toString ( ) ,
212+ count : distribution . count ,
213213 mean : distribution . mean ,
214214 sumOfSquaredDeviation : distribution . sumSquaredDeviations ,
215215 bucketOptions : { explicitBuckets : { bounds : [ 0 , ...distribution . buckets ] } } ,
@@ -246,23 +246,62 @@ export class StackdriverStatsExporter implements StatsEventListener {
246246 description : view . description || view . measure . description ,
247247 displayName : view . measure . name ,
248248 metricKind : this . createMetricKind ( view . aggregation ) ,
249- valueType : this . createValueType ( view ) ,
250- unit : view . measure . unit ,
249+ valueType : this . createValueType ( view . aggregation , view . measure ) ,
250+ unit : this . createUnit ( view . aggregation , view . measure ) ,
251251 labels : this . createLabelDescriptor ( view . getColumns ( ) )
252252 } as MetricDescriptor ;
253253 }
254+ /**
255+ * Creates a Stackdriver unit from given aggregationType and measure.
256+ *
257+ * @param {AggregationType } aggregationType The aggregation type.
258+ * @param {Measure } measure The view measure.
259+ * @returns {string } The unit.
260+ */
261+ private createUnit ( aggregationType : AggregationType , measure : Measure ) :
262+ string {
263+ if ( aggregationType === AggregationType . COUNT ) {
264+ // If the aggregation type is count, which counts the number of recorded
265+ // measurements, the unit must be "1", because this view does not apply to
266+ // the recorded values.
267+ return MeasureUnit . UNIT ;
268+ }
269+ return measure . unit ;
270+ }
254271
255272 /**
256- * Creates a Stackdriver ValueType from a given view.
257- * @param view The view to extract data from
273+ * Creates a Stackdriver ValueType from a given aggregationType and measure.
274+ *
275+ * @param {AggregationType } aggregationType The aggregation type.
276+ * @param {Measure } measure The view measure.
277+ * @returns {ValueType } The value type.
258278 */
259- private createValueType ( view : View ) : ValueType {
260- if ( view . measure . type === MeasureType . INT64 ) {
279+ private createValueType ( aggregationType : AggregationType , measure : Measure ) :
280+ ValueType {
281+ if ( aggregationType === AggregationType . COUNT ) {
261282 return ValueType . INT64 ;
262- } else if ( view . aggregation === AggregationType . DISTRIBUTION ) {
283+ } else if ( aggregationType === AggregationType . SUM ) {
284+ switch ( measure . type ) {
285+ case MeasureType . INT64 : // Sum INT64
286+ return ValueType . INT64 ;
287+ case MeasureType . DOUBLE : // Sum Double
288+ return ValueType . DOUBLE ;
289+ default :
290+ throw new Error ( `Unknown measure type: ${ measure . type } ` ) ;
291+ }
292+ } else if ( aggregationType === AggregationType . DISTRIBUTION ) {
263293 return ValueType . DISTRIBUTION ;
294+ } else if ( aggregationType === AggregationType . LAST_VALUE ) {
295+ switch ( measure . type ) {
296+ case MeasureType . INT64 : // LastValue INT64
297+ return ValueType . INT64 ;
298+ case MeasureType . DOUBLE : // LastValue Double
299+ return ValueType . DOUBLE ;
300+ default :
301+ throw new Error ( `Unknown measure type ${ measure . type } ` ) ;
302+ }
264303 }
265- return ValueType . DOUBLE ;
304+ throw Error ( `unsupported aggregation type: ${ aggregationType } ` ) ;
266305 }
267306
268307 /**
0 commit comments