-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(views): handle view conflicts. (#2734)
- Loading branch information
1 parent
a901732
commit 5b83c18
Showing
8 changed files
with
493 additions
and
23 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
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
91 changes: 91 additions & 0 deletions
91
experimental/packages/opentelemetry-sdk-metrics-base/src/state/MetricStorageRegistry.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,91 @@ | ||
/* | ||
* 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 { MetricStorage } from './MetricStorage'; | ||
import { isDescriptorCompatibleWith } from '../InstrumentDescriptor'; | ||
import * as api from '@opentelemetry/api'; | ||
import { Maybe } from '../utils'; | ||
import { getConflictResolutionRecipe, getIncompatibilityDetails } from '../view/RegistrationConflicts'; | ||
|
||
/** | ||
* Internal class for storing {@link MetricStorage} | ||
*/ | ||
export class MetricStorageRegistry { | ||
private readonly _metricStorageRegistry = new Map<string, MetricStorage[]>(); | ||
|
||
getStorages(): MetricStorage[] { | ||
let storages: MetricStorage[] = []; | ||
for (const metricStorages of this._metricStorageRegistry.values()) { | ||
storages = storages.concat(metricStorages); | ||
} | ||
|
||
return storages; | ||
} | ||
|
||
register<T extends MetricStorage>(storage: T): Maybe<T> { | ||
const expectedDescriptor = storage.getInstrumentDescriptor(); | ||
const existingStorages = this._metricStorageRegistry.get(expectedDescriptor.name); | ||
|
||
// Add storage if it does not exist. | ||
if (existingStorages === undefined) { | ||
this._metricStorageRegistry.set(expectedDescriptor.name, [storage]); | ||
return storage; | ||
} | ||
|
||
let compatibleStorage = null; | ||
|
||
for (const existingStorage of existingStorages) { | ||
const existingDescriptor = existingStorage.getInstrumentDescriptor(); | ||
|
||
if (isDescriptorCompatibleWith(existingDescriptor, expectedDescriptor)) { | ||
// Use the longer description if it does not match. | ||
if (existingDescriptor.description !== expectedDescriptor.description) { | ||
if (expectedDescriptor.description.length > existingDescriptor.description.length) { | ||
existingStorage.updateDescription(expectedDescriptor.description); | ||
} | ||
|
||
api.diag.warn('A view or instrument with the name ', | ||
expectedDescriptor.name, | ||
' has already been registered, but has a different description and is incompatible with another registered view.\n', | ||
'Details:\n', | ||
getIncompatibilityDetails(existingDescriptor, expectedDescriptor), | ||
'The longer description will be used.\nTo resolve the conflict:', | ||
getConflictResolutionRecipe(existingDescriptor, expectedDescriptor)); | ||
} | ||
// Storage is fully compatible. There will never be more than one pre-existing fully compatible storage. | ||
compatibleStorage = existingStorage as T; | ||
} else { | ||
// The implementation SHOULD warn about duplicate instrument registration | ||
// conflicts after applying View configuration. | ||
api.diag.warn('A view or instrument with the name ', | ||
expectedDescriptor.name, | ||
' has already been registered and is incompatible with another registered view.\n', | ||
'Details:\n', | ||
getIncompatibilityDetails(existingDescriptor, expectedDescriptor), | ||
'To resolve the conflict:\n', | ||
getConflictResolutionRecipe(existingDescriptor, expectedDescriptor)); | ||
} | ||
} | ||
|
||
if (compatibleStorage != null) { | ||
return compatibleStorage; | ||
} | ||
|
||
// None of the storages were compatible, add the current one to the list. | ||
existingStorages.push(storage); | ||
return storage; | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
experimental/packages/opentelemetry-sdk-metrics-base/src/view/RegistrationConflicts.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,91 @@ | ||
/* | ||
* 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 { InstrumentSelectorCriteria } from './InstrumentSelector'; | ||
import { InstrumentDescriptor } from '../InstrumentDescriptor'; | ||
|
||
export function getIncompatibilityDetails(existing: InstrumentDescriptor, otherDescriptor: InstrumentDescriptor) { | ||
let incompatibility = ''; | ||
if (existing.unit !== otherDescriptor.unit) { | ||
incompatibility += `\t- Unit '${existing.unit}' does not match '${otherDescriptor.unit}'\n`; | ||
} | ||
if (existing.type !== otherDescriptor.type) { | ||
incompatibility += `\t- Type '${existing.type}' does not match '${otherDescriptor.type}'\n`; | ||
} | ||
if (existing.valueType !== otherDescriptor.valueType) { | ||
incompatibility += `\t- Value Type '${existing.valueType}' does not match '${otherDescriptor.valueType}'\n`; | ||
} | ||
if (existing.description !== otherDescriptor.description) { | ||
incompatibility += `\t- Description '${existing.description}' does not match '${otherDescriptor.description}'\n`; | ||
} | ||
|
||
return incompatibility; | ||
} | ||
|
||
export function getValueTypeConflictResolutionRecipe(existing: InstrumentDescriptor, otherDescriptor: InstrumentDescriptor) { | ||
return `\t- use valueType '${existing.valueType}' on instrument creation or use an instrument name other than '${otherDescriptor.name}'`; | ||
} | ||
|
||
export function getUnitConflictResolutionRecipe(existing: InstrumentDescriptor, otherDescriptor: InstrumentDescriptor) { | ||
return `\t- use unit '${existing.unit}' on instrument creation or use an instrument name other than '${otherDescriptor.name}'`; | ||
} | ||
|
||
export function getTypeConflictResolutionRecipe(existing: InstrumentDescriptor, otherDescriptor: InstrumentDescriptor) { | ||
const selector: InstrumentSelectorCriteria = { | ||
name: otherDescriptor.name, | ||
type: otherDescriptor.type | ||
}; | ||
|
||
const selectorString = JSON.stringify(selector); | ||
|
||
return `\t- create a new view with a name other than '${existing.name}' and InstrumentSelector '${selectorString}'`; | ||
} | ||
|
||
export function getDescriptionResolutionRecipe(existing: InstrumentDescriptor, otherDescriptor: InstrumentDescriptor): string { | ||
const selector: InstrumentSelectorCriteria = { | ||
name: otherDescriptor.name, | ||
type: otherDescriptor.type | ||
}; | ||
|
||
const selectorString = JSON.stringify(selector); | ||
|
||
return `\t- create a new view with a name other than '${existing.name}' and InstrumentSelector '${selectorString}' | ||
\t- OR - create a new view with the name ${existing.name} and description '${existing.description}' and InstrumentSelector ${selectorString} | ||
\t- OR - create a new view with the name ${otherDescriptor.name} and description '${existing.description}' and InstrumentSelector ${selectorString}`; | ||
} | ||
|
||
export function getConflictResolutionRecipe(existing: InstrumentDescriptor, otherDescriptor: InstrumentDescriptor): string { | ||
// Conflicts that cannot be solved via views. | ||
if (existing.valueType !== otherDescriptor.valueType) { | ||
return getValueTypeConflictResolutionRecipe(existing, otherDescriptor); | ||
} | ||
|
||
if (existing.unit !== otherDescriptor.unit) { | ||
return getUnitConflictResolutionRecipe(existing, otherDescriptor); | ||
} | ||
|
||
// Conflicts that can be solved via views. | ||
if (existing.type !== otherDescriptor.type) { | ||
// this will automatically solve possible description conflicts. | ||
return getTypeConflictResolutionRecipe(existing, otherDescriptor); | ||
} | ||
|
||
if (existing.description !== otherDescriptor.description) { | ||
return getDescriptionResolutionRecipe(existing, otherDescriptor); | ||
} | ||
|
||
return ''; | ||
} |
Oops, something went wrong.