-
Notifications
You must be signed in to change notification settings - Fork 96
Stats/Stackdriver: Add user agent as client header #320
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,22 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import {logger, Logger, Measurement, Metric, MetricDescriptor as OCMetricDescriptor, MetricProducerManager, Metrics, StatsEventListener, TagKey, TagValue, View} from '@opencensus/core'; | ||
import {logger, Logger, Measurement, Metric, MetricDescriptor as OCMetricDescriptor, MetricProducerManager, Metrics, StatsEventListener, TagKey, TagValue, version, View} from '@opencensus/core'; | ||
import {auth, JWT} from 'google-auth-library'; | ||
import {google} from 'googleapis'; | ||
|
||
import {createMetricDescriptorData, createTimeSeriesList, getDefaultResource} from './stackdriver-stats-utils'; | ||
import {MonitoredResource, StackdriverExporterOptions, TimeSeries} from './types'; | ||
|
||
google.options({headers: {'x-opencensus-outgoing-request': 0x1}}); | ||
const OC_USER_AGENT = { | ||
product: 'opencensus-node', | ||
version | ||
}; | ||
const OC_HEADER = { | ||
'x-opencensus-outgoing-request': 0x1 | ||
}; | ||
|
||
google.options({headers: OC_HEADER}); | ||
const monitoring = google.monitoring('v3'); | ||
const GOOGLEAPIS_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'; | ||
|
||
|
@@ -151,10 +160,13 @@ export class StackdriverStatsExporter implements StatsEventListener { | |
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
monitoring.projects.timeSeries.create(request, (err: Error) => { | ||
this.logger.debug('sent time series', request.resource.timeSeries); | ||
err ? reject(err) : resolve(); | ||
}); | ||
monitoring.projects.timeSeries.create( | ||
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]}, | ||
(err: Error) => { | ||
this.logger.debug( | ||
'sent time series', request.resource.timeSeries); | ||
err ? reject(err) : resolve(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
@@ -173,10 +185,12 @@ export class StackdriverStatsExporter implements StatsEventListener { | |
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
monitoring.projects.metricDescriptors.create(request, (err: Error) => { | ||
this.logger.debug('sent metric descriptor', request.resource); | ||
err ? reject(err) : resolve(); | ||
}); | ||
monitoring.projects.metricDescriptors.create( | ||
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you need similar logic for other types of Google API calls in the exporter, e.g. writing traces? If so, what would you think about exporting the larger There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per my understanding, User Agent requirement is only Stackdriver stats exporter specific. For Stackdriver Trace exporter, we add agent information as a part of the label (example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, makes sense, then keeping these constants in this file only seems fine. |
||
(err: Error) => { | ||
this.logger.debug('sent metric descriptor', request.resource); | ||
err ? reject(err) : resolve(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional nit: since you are checking for the presence of
err
, should you have the parameter beerr?: Error
?(Although the package doesn't have strict null checks enabled, I think it's still helpful to think in terms of them and then if eventually we want to turn them on they will be easier to enable).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.