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

Add helper util to convert OC Metrics data models to SD monitoring data models #266

Merged

Conversation

mayurkale22
Copy link
Member

This PR is part of #257, Basically, StackdriverStatsExporterUtils methods will be used by StackdriverStatsExporter to translate data from OpenCensus data models to StackDriver monitoring data models. I will send separate PR to include this util in StackdriverStatsExporter.

@@ -103,7 +103,7 @@ export interface Distribution {
}

export interface Point {
interval: {endTime: string, startTime: string};
interval: {endTime: string, startTime?: string};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startTime should be optional (Must be present for cumulative metrics only). More info: https://cloud-dot-devsite.googleplex.com/monitoring/api/ref_v3/rest/v3/TimeInterval

@@ -115,8 +115,8 @@ export interface Point {

export interface TimeSeries {
metric: {type: string; labels: {[key: string]: string};};
resource: {type: 'global', labels: {[key: string]: string}};
resource: {type: string, labels: {[key: string]: string}};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we have planned to add more type like k8s_container, gce_instance and aws_ec2_instance.

Copy link
Contributor

@songy23 songy23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall

timeSeriesList.push({
metric: this.createMetric(
metricDescriptor, timeSeries.labelValues, metricPrefix),
resource: monitoredResource,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to double check: it's fine to use the same monitoredResource here (i.e no need to convert)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently no, but once we have monitored resource util in place (#173) we need to do the conversion same as Java's implementation.

* monitoring data models.
*/
export class StackdriverStatsExporterUtils {
static readonly OPENCENSUS_TASK: string = 'opencensus_task';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these constants and methods static within a class? While that is needed in Java, IMO it's just extra wrapping for TypeScript since the module itself already acts as a namespace. What would you think about just exporting the functions and constants directly?

valueType: this.createValueType(metricDescriptor.type),
unit: metricDescriptor.unit,
labels: this.createLabelDescriptor(metricDescriptor.labelKeys)
} as MetricDescriptor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the as MetricDescriptor? The TS compiler should know the object must match the type based on the method return type.

const metricKind = this.createMetricKind(metricDescriptor.type);
const valueType = this.createValueType(metricDescriptor.type);

for (const timeSeries of metric.timeseries) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: could we use map here instead of a for loop + push?

*/
static createLabelDescriptor(labelKeys: LabelKey[]): LabelDescriptor[] {
const labelDescriptorList = labelKeys.map(labelKey => {
return {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, you can avoid the return if you put the object in paratheses, so labelKeys.map(labelKey => ({ key: ...}))

key: labelKey.key,
valueType: 'STRING', // Now we only support String type.
description: labelKey.description
} as LabelDescriptor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a type annotation on labelDescriptorList, i.e. const labelDesceriptorList: LabelDescriptor[] = rather than a type assertion as LabelDescriptor here? The type annotation will cause the code to fail to compile if the interface changes so would help detect refactoring bugs.

*/
static createMetric(
metricDescriptor: OCMetricDescriptor, labelValues: LabelValue[],
metricPrefix: string): {type: string; labels: {[key: string]: string};} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The {type: string; labels: {[key: string]: string};} type looks fairly complex, what would you think about making it a first class exported type alias?

static createPoint(
point: TimeSeriesPoint, startTimeStamp: Timestamp,
valueType: ValueType): Point {
let value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: you can avoid use of let here by creating a separate function like createValue or similar that would take in the value type and value and return the value, then just have const value = createValue(valueType, point)

this.createDistribution(point.value as DistributionValue)
};
} else {
// console.log(`${valueType} is not supported.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this commented code and else black?

return str.replace('000Z', `${nsStr}Z`);
}

private static padNS(ns: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this function name a bit more descriptive? It seems like it's left padding a number, but I wouldn't know that without reading the code.

@mayurkale22 mayurkale22 force-pushed the stackdriver-stats-utils branch 3 times, most recently from de638fb to eeaf2a2 Compare January 9, 2019 20:21
@mayurkale22
Copy link
Member Author

@draffensperger thanks for the reviews. I have addressed all the comments, PTAL.

Copy link
Contributor

@draffensperger draffensperger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes!

Copy link
Contributor

@justindsmith justindsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, just one small log removal


/** Returns a task label value in the format of 'nodejs-<pid>@<hostname>'. */
function generateDefaultTaskValue(): string {
console.log('inside generateDefaultTaskValue');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@codecov-io
Copy link

codecov-io commented Jan 10, 2019

Codecov Report

Merging #266 into master will increase coverage by 3.3%.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #266     +/-   ##
========================================
+ Coverage    91.5%   94.8%   +3.3%     
========================================
  Files         108     110      +2     
  Lines        7520    7779    +259     
  Branches      692     713     +21     
========================================
+ Hits         6881    7375    +494     
+ Misses        639     404    -235
Impacted Files Coverage Δ
test/test-stackdriver-stats-utils.ts 100% <0%> (ø)
src/stackdriver-stats-utils.ts 99.03% <0%> (ø)
src/http.ts 89.8% <0%> (+0.97%) ⬆️
src/http2.ts 89.83% <0%> (+82.2%) ⬆️
test/test-http2.ts 98.66% <0%> (+91.33%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 47a672e...4c4a190. Read the comment docs.

@mayurkale22 mayurkale22 merged commit 3db8bf5 into census-instrumentation:master Jan 10, 2019
@mayurkale22 mayurkale22 deleted the stackdriver-stats-utils branch January 10, 2019 06:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants