Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Exporter/Stats/Stackdriver: fix MetricDescriptor and TimeSeries (#199)
Browse files Browse the repository at this point in the history
* Exporter/Stats/Stackdriver: fix MetricDescriptor and TimeSeries

* nit fix

* refactor tests

* Fix style
  • Loading branch information
mayurkale22 committed Nov 27, 2018
1 parent 3205530 commit 765bae3
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {AggregationType, DistributionData, logger, Logger, Measurement, MeasureType, StatsEventListener, Tags, View} from '@opencensus/core';
import {AggregationType, DistributionData, logger, Logger, Measure, Measurement, MeasureType, MeasureUnit, StatsEventListener, Tags, View} from '@opencensus/core';
import {auth, JWT} from 'google-auth-library';
import {google} from 'googleapis';
import * as path from 'path';
Expand Down Expand Up @@ -189,7 +189,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
if (aggregationData.type === AggregationType.DISTRIBUTION) {
value = {distributionValue: this.createDistribution(aggregationData)};
} else if (view.measure.type === MeasureType.INT64) {
value = {int64Value: aggregationData.value.toString()};
value = {int64Value: aggregationData.value};
} else {
value = {doubleValue: aggregationData.value};
}
Expand All @@ -198,7 +198,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
metric: {type: this.getMetricType(view.name), labels: tags},
resource: {type: 'global', labels: resourceLabels},
metricKind: this.createMetricKind(view.aggregation),
valueType: this.createValueType(view),
valueType: this.createValueType(view.aggregation, view.measure),
points: [{interval: {startTime, endTime}, value}]
};
}
Expand All @@ -209,7 +209,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
*/
private createDistribution(distribution: DistributionData): Distribution {
return {
count: distribution.count.toString(),
count: distribution.count,
mean: distribution.mean,
sumOfSquaredDeviation: distribution.sumSquaredDeviations,
bucketOptions: {explicitBuckets: {bounds: [0, ...distribution.buckets]}},
Expand Down Expand Up @@ -246,23 +246,62 @@ export class StackdriverStatsExporter implements StatsEventListener {
description: view.description || view.measure.description,
displayName: view.measure.name,
metricKind: this.createMetricKind(view.aggregation),
valueType: this.createValueType(view),
unit: view.measure.unit,
valueType: this.createValueType(view.aggregation, view.measure),
unit: this.createUnit(view.aggregation, view.measure),
labels: this.createLabelDescriptor(view.getColumns())
} as MetricDescriptor;
}
/**
* Creates a Stackdriver unit from given aggregationType and measure.
*
* @param {AggregationType} aggregationType The aggregation type.
* @param {Measure} measure The view measure.
* @returns {string} The unit.
*/
private createUnit(aggregationType: AggregationType, measure: Measure):
string {
if (aggregationType === AggregationType.COUNT) {
// If the aggregation type is count, which counts the number of recorded
// measurements, the unit must be "1", because this view does not apply to
// the recorded values.
return MeasureUnit.UNIT;
}
return measure.unit;
}

/**
* Creates a Stackdriver ValueType from a given view.
* @param view The view to extract data from
* Creates a Stackdriver ValueType from a given aggregationType and measure.
*
* @param {AggregationType} aggregationType The aggregation type.
* @param {Measure} measure The view measure.
* @returns {ValueType} The value type.
*/
private createValueType(view: View): ValueType {
if (view.measure.type === MeasureType.INT64) {
private createValueType(aggregationType: AggregationType, measure: Measure):
ValueType {
if (aggregationType === AggregationType.COUNT) {
return ValueType.INT64;
} else if (view.aggregation === AggregationType.DISTRIBUTION) {
} else if (aggregationType === AggregationType.SUM) {
switch (measure.type) {
case MeasureType.INT64: // Sum INT64
return ValueType.INT64;
case MeasureType.DOUBLE: // Sum Double
return ValueType.DOUBLE;
default:
throw new Error(`Unknown measure type: ${measure.type}`);
}
} else if (aggregationType === AggregationType.DISTRIBUTION) {
return ValueType.DISTRIBUTION;
} else if (aggregationType === AggregationType.LAST_VALUE) {
switch (measure.type) {
case MeasureType.INT64: // LastValue INT64
return ValueType.INT64;
case MeasureType.DOUBLE: // LastValue Double
return ValueType.DOUBLE;
default:
throw new Error(`Unknown measure type ${measure.type}`);
}
}
return ValueType.DOUBLE;
throw Error(`unsupported aggregation type: ${aggregationType}`);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-exporter-stackdriver/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface MetricDescriptor {
}

export interface Distribution {
count: string;
count: number;
mean: number;
sumOfSquaredDeviation: number;
bucketOptions: {explicitBuckets: {bounds: Bucket[];}};
Expand All @@ -106,7 +106,7 @@ export interface Point {
interval: {endTime: string, startTime: string};
value: {
boolValue?: boolean;
int64Value?: string;
int64Value?: number;
doubleValue?: number;
stringValue?: string;
distributionValue?: Distribution;
Expand Down
Loading

0 comments on commit 765bae3

Please sign in to comment.