-
Notifications
You must be signed in to change notification settings - Fork 67
fix: Stackdriver batch limit for TimeSeries #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a1f0b5b
bb18997
d6e3d3d
3e325bf
2c84305
ea93f51
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 |
|---|---|---|
|
|
@@ -99,7 +99,7 @@ function transformValueType(valueType: OTValueType): ValueType { | |
| } | ||
|
|
||
| /** | ||
| * Converts metric's timeseries to a list of TimeSeries, so that metric can be | ||
| * Converts metric's timeseries to a TimeSeries, so that metric can be | ||
|
Contributor
Author
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. Updated the comment here since a single TimeSeries is being returned. I think the discrepancy was made when this code was copied over. |
||
| * uploaded to StackDriver. | ||
| */ | ||
| export function createTimeSeries( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import { | ||
| TimeSeries, | ||
| } from './types'; | ||
|
|
||
| /** Returns an array with arrays of the given size. */ | ||
|
||
| export function partitionList(list: TimeSeries[], chunkSize: number) { | ||
|
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. It would be better to copy the array first, since this will mutate the original array passed into the function. E.g.
Contributor
Author
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. Thanks for the feedback, updated! |
||
| const results = []; | ||
| while (list.length) { | ||
| results.push(list.splice(0, chunkSize)); | ||
| } | ||
| return results; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,9 @@ describe('MetricExporter', () => { | |
| let exporter: MetricExporter; | ||
| let logger: ConsoleLogger; | ||
| /* tslint:disable no-any */ | ||
| let metricDescriptors: sinon.SinonSpy<[any, any], any>; | ||
| let metricDescriptors: sinon.SinonSpy<[any, any, any], any>; | ||
| /* tslint:disable no-any */ | ||
| let timeSeries: sinon.SinonSpy<[any, any, any], any>; | ||
| let debug: sinon.SinonSpy; | ||
| let info: sinon.SinonSpy; | ||
| let warn: sinon.SinonSpy; | ||
|
|
@@ -69,7 +71,7 @@ describe('MetricExporter', () => { | |
|
|
||
| metricDescriptors = sinon.spy( | ||
| /* tslint:disable no-any */ | ||
| (request: any, callback: (err: Error | null) => void): any => { | ||
| (request: any, params: any, callback: (err: Error | null) => void): any => { | ||
|
||
| callback(null); | ||
| } | ||
| ); | ||
|
|
@@ -81,6 +83,20 @@ describe('MetricExporter', () => { | |
| metricDescriptors as any | ||
| ); | ||
|
|
||
| timeSeries = sinon.spy( | ||
| /* tslint:disable no-any */ | ||
| (request: any, params: any, callback: (err: Error | null) => void): any => { | ||
| callback(null); | ||
| } | ||
| ); | ||
|
|
||
| sinon.replace( | ||
| MetricExporter['_monitoring'].projects.timeSeries, | ||
| 'create', | ||
| /* tslint:disable no-any */ | ||
| timeSeries as any | ||
| ); | ||
|
|
||
| sinon.replace(exporter['_auth'], 'getClient', () => { | ||
| if (getClientShouldFail) { | ||
| throw new Error('fail'); | ||
|
|
@@ -139,5 +155,46 @@ describe('MetricExporter', () => { | |
|
|
||
| assert.deepStrictEqual(result, ExportResult.SUCCESS); | ||
| }); | ||
|
|
||
| it('should enforce batch size limit on metrics', async () => { | ||
|
|
||
| const meter = new MeterProvider().getMeter('test-meter'); | ||
|
|
||
| const labels: Labels = { ['keyb']: 'value2', ['keya']: 'value1' }; | ||
| let nMetrics = 401 | ||
| while (nMetrics > 0) { | ||
| nMetrics -= 1 | ||
| const counter = meter.createCounter(`name${nMetrics.toString()}`, { | ||
| labelKeys: ['keya', 'keyb'], | ||
| }); | ||
| counter.bind(labels).add(10); | ||
| } | ||
| meter.collect(); | ||
| const records = meter.getBatcher().checkPointSet(); | ||
|
|
||
| const result = await new Promise((resolve, reject) => { | ||
| exporter.export(records, result => { | ||
| resolve(result); | ||
| }); | ||
| }); | ||
|
|
||
| assert.deepStrictEqual( | ||
| metricDescriptors.getCall(0).args[0].resource.type, | ||
| 'custom.googleapis.com/opentelemetry/name400' | ||
| ); | ||
| assert.deepStrictEqual( | ||
| metricDescriptors.getCall(100).args[0].resource.type, | ||
| 'custom.googleapis.com/opentelemetry/name300' | ||
| ); | ||
| assert.deepStrictEqual( | ||
| metricDescriptors.getCall(400).args[0].resource.type, | ||
| 'custom.googleapis.com/opentelemetry/name0' | ||
| ); | ||
|
|
||
| assert.equal(metricDescriptors.callCount, 401) | ||
| assert.equal(timeSeries.callCount, 3) | ||
|
|
||
| assert.deepStrictEqual(result, ExportResult.SUCCESS); | ||
| }); | ||
| }); | ||
| }); | ||
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.
Added an
awaithere for the async code in_sendTimeSeriesto be properly called and spied on in tests.I was wondering if there was a preferred testing practice so we don't have to wait here, or if it's ok to wait so we can handle if an error is thrown?