Skip to content
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

fix(exporter-collector): wrong data type for numbers #1938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ export function ensureResourceIsCorrect(
{
key: 'version',
value: {
doubleValue: 1,
value: 'doubleValue',
intValue: '1',
value: 'intValue',
},
},
{
Expand Down
11 changes: 10 additions & 1 deletion packages/opentelemetry-exporter-collector/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import {
CollectorExporterConfigBase,
} from './types';

const MAX_INTEGER_VALUE = 2147483647;
const MIN_INTEGER_VALUE = -2147483648;

/**
* Converts attributes to KeyValue array
* @param attributes
Expand Down Expand Up @@ -95,8 +98,14 @@ export function toCollectorAnyValue(
anyValue.stringValue = value;
} else if (typeof value === 'boolean') {
anyValue.boolValue = value;
} else if (
typeof value === 'number' &&
value <= MAX_INTEGER_VALUE &&
value >= MIN_INTEGER_VALUE &&
Number.isInteger(value)
) {
anyValue.intValue = value;
} else if (typeof value === 'number') {
// all numbers will be treated as double
anyValue.doubleValue = value;
} else if (Array.isArray(value)) {
anyValue.arrayValue = toCollectorArrayValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ describe('transform', () => {
]);
});

it('should convert attribute integer', () => {
it('should convert attribute integer to integer', () => {
const attributes: SpanAttributes = {
foo: 13,
};
assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [
{ key: 'foo', value: { doubleValue: 13 } },
{ key: 'foo', value: { intValue: 13 } },
]);
});

it('should convert attribute integer to double', () => {
const attributes: SpanAttributes = {
foo: 2247483647,
};
assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [
{ key: 'foo', value: { doubleValue: 2247483647 } },
]);
});

Expand Down Expand Up @@ -118,7 +127,7 @@ describe('transform', () => {
},
{
key: 'version',
value: { doubleValue: 1 },
value: { intValue: 1 },
},
{ key: 'success', value: { boolValue: true } },
],
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export function ensureWebResourceIsCorrect(
{
key: 'version',
value: {
doubleValue: 1,
intValue: 1,
},
},
{
Expand Down