-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a80701f
commit da450e4
Showing
16 changed files
with
678 additions
and
8 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
32 changes: 32 additions & 0 deletions
32
experimental/packages/opentelemetry-sdk-metrics-base/src/export/CollectorInfo.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,32 @@ | ||
/* | ||
* Copyright The 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 { AggregationTemporality } from '@opentelemetry/api-metrics-wip'; | ||
|
||
/** | ||
* A opaque handle for a collection-pipeline of metrics. | ||
* | ||
* This provides an efficient means of leasing and tracking metric readers. | ||
*/ | ||
export type CollectorHandle = symbol; | ||
|
||
/** | ||
* An internal record about a {@link MetricReader} used when collecting metrics. | ||
*/ | ||
export interface CollectorInfo { | ||
collectorHandle: CollectorHandle; | ||
aggregationTemporality: AggregationTemporality; | ||
} |
57 changes: 57 additions & 0 deletions
57
experimental/packages/opentelemetry-sdk-metrics-base/src/state/DeltaMetricStorage.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,57 @@ | ||
/* | ||
* Copyright The 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 { Context } from '@opentelemetry/api'; | ||
import { Attributes } from '@opentelemetry/api-metrics-wip'; | ||
import { hashAttributes, Maybe } from '../utils'; | ||
import { Accumulation, AccumulationRecord, Aggregator } from '../aggregator/types'; | ||
|
||
/** | ||
* Allows synchronous collection of metrics. | ||
* | ||
* This storage should allow allocation of new aggregation cells for metrics and unique reporting | ||
* of per-collection delta accumulations. | ||
*/ | ||
export class DeltaMetricStorage<T extends Maybe<Accumulation>> { | ||
private _activeCollectionStorage = new Map<string, AccumulationRecord<T>>(); | ||
|
||
constructor(private _aggregator: Aggregator<T>) {} | ||
|
||
/** Bind an efficient storage handle for a set of attributes. */ | ||
private bind(attributes: Attributes) { | ||
const hash = hashAttributes(attributes); | ||
let accumulationRecord: AccumulationRecord<T>; | ||
if (this._activeCollectionStorage.has(hash)) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
accumulationRecord = this._activeCollectionStorage.get(hash)!; | ||
} else { | ||
accumulationRecord = [attributes, this._aggregator.createAccumulation()] as AccumulationRecord<T>; | ||
this._activeCollectionStorage.set(hash, accumulationRecord); | ||
} | ||
return accumulationRecord[1]; | ||
} | ||
|
||
record(value: number, attributes: Attributes, _context: Context) { | ||
const accumulation = this.bind(attributes); | ||
accumulation?.record(value); | ||
} | ||
|
||
collect() { | ||
const unreportedDelta = this._activeCollectionStorage; | ||
this._activeCollectionStorage = new Map(); | ||
return unreportedDelta; | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
experimental/packages/opentelemetry-sdk-metrics-base/src/state/MetricStorage.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,39 @@ | ||
/* | ||
* Copyright The 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 { HrTime } from '@opentelemetry/api'; | ||
import { InstrumentationLibrary } from '@opentelemetry/core'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { CollectorInfo } from '../export/CollectorInfo'; | ||
import { MetricData } from '../export/MetricData'; | ||
import { Maybe } from '../utils'; | ||
|
||
export interface MetricStorage { | ||
/** | ||
* Collects the metrics from this storage and resets for the next | ||
* collection period. | ||
* | ||
* Note: This is a stateful operation and will reset any interval-related | ||
* state for the CollectorInfo. | ||
*/ | ||
collectAndReset( | ||
collectorInfo: CollectorInfo, | ||
resource: Resource, | ||
instrumentationLibrary: InstrumentationLibrary, | ||
sdkStartTime: HrTime, | ||
collectionTime: HrTime, | ||
): Promise<Maybe<MetricData>>; | ||
} |
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
75 changes: 75 additions & 0 deletions
75
experimental/packages/opentelemetry-sdk-metrics-base/src/state/SyncMetricStorage.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,75 @@ | ||
/* | ||
* Copyright The 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 { Context, HrTime } from '@opentelemetry/api'; | ||
import { Attributes } from '@opentelemetry/api-metrics-wip'; | ||
import { WritableMetricStorage } from './WritableMetricStorage'; | ||
import { Accumulation, Aggregator } from '../aggregator/types'; | ||
import { View } from '../view/View'; | ||
import { createInstrumentDescriptorWithView, InstrumentDescriptor } from '../InstrumentDescriptor'; | ||
import { AttributesProcessor } from '../view/AttributesProcessor'; | ||
import { MetricStorage } from './MetricStorage'; | ||
import { CollectorInfo } from '../export/CollectorInfo'; | ||
import { InstrumentationLibrary } from '@opentelemetry/core'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { MetricData } from '../export/MetricData'; | ||
import { DeltaMetricStorage } from './DeltaMetricStorage'; | ||
import { TemporalMetricStorage } from './TemporalMetricStorage'; | ||
import { Maybe } from '../utils'; | ||
|
||
/** | ||
* Stores and aggregate {@link MetricData} for synchronous instruments. | ||
*/ | ||
export class SyncMetricStorage<T extends Maybe<Accumulation>> implements WritableMetricStorage, MetricStorage { | ||
private _deltaMetricStorage: DeltaMetricStorage<T>; | ||
private _temporalMetricStorage: TemporalMetricStorage<T>; | ||
|
||
constructor(private _instrumentDescriptor: InstrumentDescriptor, aggregator: Aggregator<T>, private _attributesProcessor: AttributesProcessor) { | ||
this._deltaMetricStorage = new DeltaMetricStorage(aggregator); | ||
this._temporalMetricStorage = new TemporalMetricStorage(aggregator); | ||
} | ||
|
||
record(value: number, attributes: Attributes, context: Context) { | ||
attributes = this._attributesProcessor.process(attributes, context); | ||
this._deltaMetricStorage.record(value, attributes, context); | ||
} | ||
|
||
/** | ||
* Collects the metrics from this storage and resets for the next | ||
* collection period. | ||
* | ||
* Note: This is a stateful operation and will reset any interval-related | ||
* state for the CollectorInfo. | ||
*/ | ||
async collectAndReset( | ||
collectorInfo: CollectorInfo, | ||
resource: Resource, | ||
instrumentationLibrary: InstrumentationLibrary, | ||
sdkStartTime: HrTime, | ||
collectionTime: HrTime, | ||
): Promise<Maybe<MetricData>> { | ||
const accumulations = this._deltaMetricStorage.collect(); | ||
|
||
return this._temporalMetricStorage.buildMetrics(collectorInfo, resource, instrumentationLibrary, this._instrumentDescriptor, accumulations, sdkStartTime, collectionTime); | ||
} | ||
|
||
static create(view: View, instrument: InstrumentDescriptor): SyncMetricStorage<Maybe<Accumulation>> { | ||
instrument = createInstrumentDescriptorWithView(view, instrument); | ||
const aggregator = view.aggregation.createAggregator(instrument); | ||
const storage = new SyncMetricStorage(instrument, aggregator, view.attributesProcessor); | ||
return storage; | ||
} | ||
} |
Oops, something went wrong.