-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add scope attribute APIs #18165
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
base: develop
Are you sure you want to change the base?
Changes from 6 commits
0d59feb
c94bcd6
6fe4b81
db448cc
1bde956
49182eb
b360e4b
10db70f
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 |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| export type Attributes = Record<string, TypedAttributeValue>; | ||
|
|
||
| export type AttributeValueType = string | number | boolean | Array<string> | Array<number> | Array<boolean>; | ||
|
|
||
| export type TypedAttributeValue = ( | ||
| | { | ||
| value: string; | ||
| type: 'string'; | ||
| } | ||
| | { | ||
| value: number; | ||
| type: 'integer'; | ||
| } | ||
| | { | ||
| value: number; | ||
| type: 'double'; | ||
| } | ||
| | { | ||
| value: boolean; | ||
| type: 'boolean'; | ||
| } | ||
| | { | ||
| value: Array<string>; | ||
| type: 'string[]'; | ||
| } | ||
| | { | ||
| value: Array<number>; | ||
| type: 'integer[]'; | ||
| } | ||
| | { | ||
| value: Array<number>; | ||
| type: 'double[]'; | ||
| } | ||
| | { | ||
| value: Array<boolean>; | ||
| type: 'boolean[]'; | ||
| } | ||
| ) & { unit?: Units }; | ||
|
|
||
| type Units = 'ms' | 's' | 'bytes' | 'count' | 'percent'; | ||
|
|
||
| /** | ||
| * Converts an attribute value to a typed attribute value. | ||
| * | ||
| * Does not allow mixed arrays. In case of a mixed array, the value is stringified and the type is 'string'. | ||
| * | ||
| * @param value - The value of the passed attribute. | ||
| * @returns The typed attribute. | ||
| */ | ||
| export function attributeValueToTypedAttributeValue(value: AttributeValueType): TypedAttributeValue { | ||
| switch (typeof value) { | ||
| case 'number': | ||
| if (Number.isInteger(value)) { | ||
| return { | ||
| value, | ||
| type: 'integer', | ||
| }; | ||
| } | ||
| return { | ||
| value, | ||
| type: 'double', | ||
| }; | ||
|
Comment on lines
+74
to
+84
Collaborator
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. Q: Could |
||
| case 'boolean': | ||
| return { | ||
| value, | ||
| type: 'boolean', | ||
| }; | ||
| case 'string': | ||
| return { | ||
| value, | ||
| type: 'string', | ||
| }; | ||
| } | ||
|
|
||
| if (Array.isArray(value)) { | ||
| if (value.every(item => typeof item === 'string')) { | ||
| return { | ||
| value, | ||
| type: 'string[]', | ||
| }; | ||
| } | ||
| if (value.every(item => typeof item === 'number')) { | ||
| if (value.every(item => Number.isInteger(item))) { | ||
| return { | ||
| value, | ||
| type: 'integer[]', | ||
| }; | ||
| } else if (value.every(item => !Number.isInteger(item))) { | ||
| return { | ||
| value, | ||
| type: 'double[]', | ||
| }; | ||
| } | ||
| } | ||
| if (value.every(item => typeof item === 'boolean')) { | ||
| return { | ||
| value, | ||
| type: 'boolean[]', | ||
| }; | ||
| } | ||
|
Comment on lines
+98
to
+122
Collaborator
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. super L: do a single pass? const getItemType = (item) => {
const type = typeof item;
return type === 'number'
? (Number.isInteger(item) ? 'integer' : 'double')
: type;
};
let type = value.reduce((acc, item) => {
const itemType = getItemType(item);
return acc === itemType ? itemType : 'mixed';
}, getItemType(value[0]));
if (type === 'mixed') {
return {
value: JSON.stringify(value),
type: 'string',
};
}
return {
value,
type: `${type}[]`,
}; |
||
| } | ||
|
|
||
| // Fallback: stringify the passed value | ||
| return { | ||
| value: JSON.stringify(value), | ||
| type: 'string', | ||
| }; | ||
|
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. Bug: Type Inconsistency from Undefined InputWhen |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
In contrast to logs and metrics attribute definitions and helper functions, this one already handles array attributes. My thinking is, we introduce this here then unify logs, metrics (+ spans eventually) to use the types and APIs here. While we'll have to change the used types for logs and metrics, I don't think this is breaking as theoretically logs and metrics support subsets of the attribute value types specified here.