Skip to content

Commit

Permalink
fix(exporter-collector): wrong data type for numbers (#1938)
Browse files Browse the repository at this point in the history
* fix(exporter-collector): proper data types for int and double

* test(exporter-collector): add test for converting number to int and double

* test(exporter-collector-grpc): fix tests after changes in transform in exporter-collector
  • Loading branch information
kudlatyamroth committed Feb 25, 2021
1 parent 167bfd8 commit 8af4444
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-collector-grpc/test/helper.ts
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
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
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
Expand Up @@ -540,7 +540,7 @@ export function ensureWebResourceIsCorrect(
{
key: 'version',
value: {
doubleValue: 1,
intValue: 1,
},
},
{
Expand Down

0 comments on commit 8af4444

Please sign in to comment.