-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* chore(metrics): move each aggregator in its own file * feat(aggregators): implement histogram aggregator #927 * chore: address PR comments * chore: fix ConsoleMetricExporter * chore: address mayur comments * chore: add documentation on internal structure
- Loading branch information
Showing
11 changed files
with
387 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/opentelemetry-metrics/src/export/aggregators/countersum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 { Aggregator, Point } from '../types'; | ||
import { HrTime } from '@opentelemetry/api'; | ||
import { hrTime } from '@opentelemetry/core'; | ||
|
||
/** Basic aggregator which calculates a Sum from individual measurements. */ | ||
export class CounterSumAggregator implements Aggregator { | ||
private _current: number = 0; | ||
private _lastUpdateTime: HrTime = [0, 0]; | ||
|
||
update(value: number): void { | ||
this._current += value; | ||
this._lastUpdateTime = hrTime(); | ||
} | ||
|
||
toPoint(): Point { | ||
return { | ||
value: this._current, | ||
timestamp: this._lastUpdateTime, | ||
}; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/opentelemetry-metrics/src/export/aggregators/histogram.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 { Aggregator, Point, Histogram } from '../types'; | ||
import { HrTime } from '@opentelemetry/api'; | ||
import { hrTime } from '@opentelemetry/core'; | ||
|
||
/** | ||
* Basic aggregator which observes events and counts them in pre-defined buckets | ||
* and provides the total sum and count of all observations. | ||
*/ | ||
export class HistogramAggregator implements Aggregator { | ||
private _lastCheckpoint: Histogram; | ||
private _currentCheckpoint: Histogram; | ||
private _lastCheckpointTime: HrTime; | ||
private readonly _boundaries: number[]; | ||
|
||
constructor(boundaries: number[]) { | ||
if (boundaries === undefined || boundaries.length === 0) { | ||
throw new Error(`HistogramAggregator should be created with boundaries.`); | ||
} | ||
// we need to an ordered set to be able to correctly compute count for each | ||
// boundary since we'll iterate on each in order. | ||
this._boundaries = boundaries.sort(); | ||
this._lastCheckpoint = this._newEmptyCheckpoint(); | ||
this._lastCheckpointTime = hrTime(); | ||
this._currentCheckpoint = this._newEmptyCheckpoint(); | ||
} | ||
|
||
update(value: number): void { | ||
this._currentCheckpoint.count += 1; | ||
this._currentCheckpoint.sum += value; | ||
|
||
for (let i = 0; i < this._boundaries.length; i++) { | ||
if (value < this._boundaries[i]) { | ||
this._currentCheckpoint.buckets.counts[i] += 1; | ||
return; | ||
} | ||
} | ||
|
||
// value is above all observed boundaries | ||
this._currentCheckpoint.buckets.counts[this._boundaries.length] += 1; | ||
} | ||
|
||
reset(): void { | ||
this._lastCheckpointTime = hrTime(); | ||
this._lastCheckpoint = this._currentCheckpoint; | ||
this._currentCheckpoint = this._newEmptyCheckpoint(); | ||
} | ||
|
||
toPoint(): Point { | ||
return { | ||
value: this._lastCheckpoint, | ||
timestamp: this._lastCheckpointTime, | ||
}; | ||
} | ||
|
||
private _newEmptyCheckpoint(): Histogram { | ||
return { | ||
buckets: { | ||
boundaries: this._boundaries, | ||
counts: this._boundaries.map(() => 0).concat([0]), | ||
}, | ||
sum: 0, | ||
count: 0, | ||
}; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/opentelemetry-metrics/src/export/aggregators/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
export * from './countersum'; | ||
export * from './observer'; | ||
export * from './measureexact'; | ||
export * from './histogram'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/opentelemetry-metrics/src/export/aggregators/observer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 { Aggregator, Point } from '../types'; | ||
import { HrTime } from '@opentelemetry/api'; | ||
import { hrTime } from '@opentelemetry/core'; | ||
|
||
/** Basic aggregator for Observer which keeps the last recorded value. */ | ||
export class ObserverAggregator implements Aggregator { | ||
private _current: number = 0; | ||
private _lastUpdateTime: HrTime = [0, 0]; | ||
|
||
update(value: number): void { | ||
this._current = value; | ||
this._lastUpdateTime = hrTime(); | ||
} | ||
|
||
toPoint(): Point { | ||
return { | ||
value: this._current, | ||
timestamp: this._lastUpdateTime, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.