Skip to content

Commit

Permalink
chore: fixing conversion of id to hex and base64 (#1627)
Browse files Browse the repository at this point in the history
* chore: fixing conversion of id to hex and base64

* chore: adding test
  • Loading branch information
obecny authored Oct 29, 2020
1 parent eb35306 commit 435d608
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
# Collector
collector:
image: otel/opentelemetry-collector:0.12.0
image: otel/opentelemetry-collector:0.13.0
# image: otel/opentelemetry-collector:latest
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"]
volumes:
Expand Down
42 changes: 17 additions & 25 deletions packages/opentelemetry-exporter-collector-grpc/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,25 @@ const meterProvider = new MeterProvider({
const meter = meterProvider.getMeter('default', '0.0.1');

const traceIdArr = [
213,
253,
116,
211,
199,
92,
241,
237,
187,
209,
239,
57,
115,
141,
26,
209,
222,
31,
16,
8,
220,
223,
221,
253,
111,
110,
252,
142,
39,
14,
133,
196,
10,
13,
124,
57,
57,
178,
120,
];
const spanIdArr = [229, 237, 116, 239, 110, 181, 127, 174, 31, 107, 157, 222];
const parentIdArr = [239, 198, 188, 247, 94, 116, 247, 207, 58, 227, 127, 60];
const spanIdArr = [94, 16, 114, 97, 246, 79, 165, 62];
const parentIdArr = [120, 168, 145, 80, 152, 134, 67, 136];

export async function mockCounter(): Promise<MetricRecord> {
const name = 'int-counter';
Expand Down
29 changes: 19 additions & 10 deletions packages/opentelemetry-exporter-collector-proto/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { TraceFlags, ValueType } from '@opentelemetry/api';
import { hexToBase64 } from '@opentelemetry/core';
import { ReadableSpan } from '@opentelemetry/tracing';
import { Resource } from '@opentelemetry/resources';
import { collectorTypes } from '@opentelemetry/exporter-collector';
Expand Down Expand Up @@ -97,11 +98,11 @@ export const mockedReadableSpan: ReadableSpan = {
name: 'documentFetch',
kind: 0,
spanContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
traceId: traceIdHex,
spanId: spanIdHex,
traceFlags: TraceFlags.SAMPLED,
},
parentSpanId: '78a8915098864388',
parentSpanId: parentIdHex,
startTime: [1574120165, 429803070],
endTime: [1574120165, 438688070],
ended: true,
Expand All @@ -110,8 +111,8 @@ export const mockedReadableSpan: ReadableSpan = {
links: [
{
context: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '78a8915098864388',
traceId: traceIdHex,
spanId: parentIdHex,
},
attributes: { component: 'document-load' },
},
Expand Down Expand Up @@ -222,8 +223,8 @@ export function ensureProtoLinksAreCorrect(
attributes,
[
{
traceId: traceIdHex,
spanId: parentIdHex,
traceId: hexToBase64(traceIdHex),
spanId: hexToBase64(parentIdHex),
attributes: [
{
key: 'component',
Expand Down Expand Up @@ -251,11 +252,19 @@ export function ensureProtoSpanIsCorrect(
if (span.links) {
ensureProtoLinksAreCorrect(span.links);
}
assert.deepStrictEqual(span.traceId, traceIdHex, 'traceId is wrong');
assert.deepStrictEqual(span.spanId, spanIdHex, 'spanId is wrong');
assert.deepStrictEqual(
span.traceId,
hexToBase64(traceIdHex),
'traceId is' + ' wrong'
);
assert.deepStrictEqual(
span.spanId,
hexToBase64(spanIdHex),
'spanId is' + ' wrong'
);
assert.deepStrictEqual(
span.parentSpanId,
parentIdHex,
hexToBase64(parentIdHex),
'parentIdArr is wrong'
);
assert.strictEqual(span.name, 'documentFetch', 'name is wrong');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CollectorTraceExporter
convert(
spans: ReadableSpan[]
): collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest {
return toCollectorExportTraceServiceRequest(spans, this);
return toCollectorExportTraceServiceRequest(spans, this, true);
}

getDefaultUrl(config: CollectorExporterConfigBase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CollectorTraceExporter
convert(
spans: ReadableSpan[]
): collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest {
return toCollectorExportTraceServiceRequest(spans, this);
return toCollectorExportTraceServiceRequest(spans, this, true);
}

getDefaultUrl(config: CollectorExporterConfigBase): string {
Expand Down
60 changes: 45 additions & 15 deletions packages/opentelemetry-exporter-collector/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,20 @@ export function toCollectorEvents(
/**
* Converts links
* @param span
* @param useHex - if ids should be kept as hex without converting to base64
*/
export function toCollectorLinks(
span: ReadableSpan
function toCollectorLinks(
span: ReadableSpan,
useHex?: boolean
): opentelemetryProto.trace.v1.Span.Link[] {
return span.links.map((link: Link) => {
const protoLink: opentelemetryProto.trace.v1.Span.Link = {
traceId: link.context.traceId,
spanId: link.context.spanId,
traceId: useHex
? link.context.traceId
: core.hexToBase64(link.context.traceId),
spanId: useHex
? link.context.spanId
: core.hexToBase64(link.context.spanId),
attributes: toCollectorAttributes(link.attributes || {}),
droppedAttributesCount: 0,
};
Expand All @@ -151,14 +157,24 @@ export function toCollectorLinks(
/**
* Converts span
* @param span
* @param useHex - if ids should be kept as hex without converting to base64
*/
export function toCollectorSpan(
span: ReadableSpan
span: ReadableSpan,
useHex?: boolean
): opentelemetryProto.trace.v1.Span {
return {
traceId: span.spanContext.traceId,
spanId: span.spanContext.spanId,
parentSpanId: span.parentSpanId ? span.parentSpanId : undefined,
traceId: useHex
? span.spanContext.traceId
: core.hexToBase64(span.spanContext.traceId),
spanId: useHex
? span.spanContext.spanId
: core.hexToBase64(span.spanContext.spanId),
parentSpanId: span.parentSpanId
? useHex
? span.parentSpanId
: core.hexToBase64(span.parentSpanId)
: undefined,
traceState: toCollectorTraceState(span.spanContext.traceState),
name: span.name,
kind: toCollectorKind(span.kind),
Expand All @@ -169,7 +185,7 @@ export function toCollectorSpan(
events: toCollectorEvents(span.events),
droppedEventsCount: 0,
status: span.status,
links: toCollectorLinks(span),
links: toCollectorLinks(span, useHex),
droppedLinksCount: 0,
};
}
Expand Down Expand Up @@ -224,6 +240,7 @@ export function toCollectorTraceState(
* Prepares trace service request to be sent to collector
* @param spans spans
* @param collectorExporterBase
* @param useHex - if ids should be kept as hex without converting to base64
*/
export function toCollectorExportTraceServiceRequest<
T extends CollectorExporterConfigBase
Expand All @@ -233,7 +250,8 @@ export function toCollectorExportTraceServiceRequest<
T,
ReadableSpan,
opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest
>
>,
useHex?: boolean
): opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest {
const groupedSpans: Map<
Resource,
Expand All @@ -249,7 +267,11 @@ export function toCollectorExportTraceServiceRequest<
);

return {
resourceSpans: toCollectorResourceSpans(groupedSpans, additionalAttributes),
resourceSpans: toCollectorResourceSpans(
groupedSpans,
additionalAttributes,
useHex
),
};
}

Expand Down Expand Up @@ -283,13 +305,15 @@ export function groupSpansByResourceAndLibrary(
* Convert to InstrumentationLibrarySpans
* @param instrumentationLibrary
* @param spans
* @param useHex - if ids should be kept as hex without converting to base64
*/
function toCollectorInstrumentationLibrarySpans(
instrumentationLibrary: core.InstrumentationLibrary,
spans: ReadableSpan[]
spans: ReadableSpan[],
useHex?: boolean
): opentelemetryProto.trace.v1.InstrumentationLibrarySpans {
return {
spans: spans.map(toCollectorSpan),
spans: spans.map(span => toCollectorSpan(span, useHex)),
instrumentationLibrary,
};
}
Expand All @@ -298,18 +322,24 @@ function toCollectorInstrumentationLibrarySpans(
* Returns a list of resource spans which will be exported to the collector
* @param groupedSpans
* @param baseAttributes
* @param useHex - if ids should be kept as hex without converting to base64
*/
function toCollectorResourceSpans(
groupedSpans: Map<Resource, Map<core.InstrumentationLibrary, ReadableSpan[]>>,
baseAttributes: Attributes
baseAttributes: Attributes,
useHex?: boolean
): opentelemetryProto.trace.v1.ResourceSpans[] {
return Array.from(groupedSpans, ([resource, libSpans]) => {
return {
resource: toCollectorResource(resource, baseAttributes),
instrumentationLibrarySpans: Array.from(
libSpans,
([instrumentationLibrary, spans]) =>
toCollectorInstrumentationLibrarySpans(instrumentationLibrary, spans)
toCollectorInstrumentationLibrarySpans(
instrumentationLibrary,
spans,
useHex
)
),
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ describe('transform', () => {
});

describe('toCollectorSpan', () => {
it('should convert span', () => {
ensureSpanIsCorrect(transform.toCollectorSpan(mockedReadableSpan));
it('should convert span using hex', () => {
ensureSpanIsCorrect(transform.toCollectorSpan(mockedReadableSpan, true));
});
it('should convert span using base64', () => {
ensureSpanIsCorrect(transform.toCollectorSpan(mockedReadableSpan), false);
});
});

Expand Down
28 changes: 19 additions & 9 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TraceFlags, ValueType } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/tracing';
import { Resource } from '@opentelemetry/resources';
import { MetricRecord, MeterProvider } from '@opentelemetry/metrics';
import { InstrumentationLibrary } from '@opentelemetry/core';
import { hexToBase64, InstrumentationLibrary } from '@opentelemetry/core';
import * as assert from 'assert';
import { opentelemetryProto } from '../src/types';
import * as collectorTypes from '../src/types';
Expand Down Expand Up @@ -387,14 +387,15 @@ export function ensureAttributesAreCorrect(
}

export function ensureLinksAreCorrect(
attributes: opentelemetryProto.trace.v1.Span.Link[]
attributes: opentelemetryProto.trace.v1.Span.Link[],
useHex?: boolean
) {
assert.deepStrictEqual(
attributes,
[
{
traceId: traceIdHex,
spanId: parentIdHex,
traceId: useHex ? traceIdHex : hexToBase64(traceIdHex),
spanId: useHex ? parentIdHex : hexToBase64(parentIdHex),
attributes: [
{
key: 'component',
Expand All @@ -411,7 +412,8 @@ export function ensureLinksAreCorrect(
}

export function ensureSpanIsCorrect(
span: collectorTypes.opentelemetryProto.trace.v1.Span
span: collectorTypes.opentelemetryProto.trace.v1.Span,
useHex = true
) {
if (span.attributes) {
ensureAttributesAreCorrect(span.attributes);
Expand All @@ -420,13 +422,21 @@ export function ensureSpanIsCorrect(
ensureEventsAreCorrect(span.events);
}
if (span.links) {
ensureLinksAreCorrect(span.links);
ensureLinksAreCorrect(span.links, useHex);
}
assert.deepStrictEqual(span.traceId, traceIdHex, 'traceId is wrong');
assert.deepStrictEqual(span.spanId, spanIdHex, 'spanId is wrong');
assert.deepStrictEqual(
span.traceId,
useHex ? traceIdHex : hexToBase64(traceIdHex),
'traceId is' + ' wrong'
);
assert.deepStrictEqual(
span.spanId,
useHex ? spanIdHex : hexToBase64(spanIdHex),
'spanId is' + ' wrong'
);
assert.deepStrictEqual(
span.parentSpanId,
parentIdHex,
useHex ? parentIdHex : hexToBase64(parentIdHex),
'parentIdArr is wrong'
);
assert.strictEqual(span.name, 'documentFetch', 'name is wrong');
Expand Down

0 comments on commit 435d608

Please sign in to comment.