Skip to content

Commit

Permalink
fix collector resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Mar 12, 2020
1 parent 2c50036 commit b7c7dc0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { NoopLogger } from '@opentelemetry/core';
import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing';
import { Attributes, Logger } from '@opentelemetry/api';
import * as collectorTypes from './types';
import { toCollectorSpan } from './transform';
import { toCollectorSpan, toCollectorResource } from './transform';
import { onInit, onShutdown, sendSpans } from './platform/index';
import { Resource } from '@opentelemetry/resources';

Expand Down Expand Up @@ -101,8 +101,9 @@ export class CollectorExporter implements SpanExporter {
toCollectorSpan(span)
);
this.logger.debug('spans to be sent', spansToBeSent);
const resource =
spansToBeSent.length > 0 ? spans[0].resource : Resource.empty();
const resource = toCollectorResource(
spansToBeSent.length > 0 ? spans[0].resource : Resource.empty()
);

// Send spans to [opentelemetry collector]{@link https://github.com/open-telemetry/opentelemetry-collector}
// it will use the appropriate transport layer automatically depends on platform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Logger } from '@opentelemetry/api';
import { CollectorExporter } from '../../CollectorExporter';
import * as collectorTypes from '../../types';
import { VERSION } from '../../version';
import { Resource } from '@opentelemetry/resources';

/**
* function that is called once when {@link ExporterCollector} is initialised
Expand Down Expand Up @@ -51,7 +50,7 @@ export function sendSpans(
onSuccess: () => void,
onError: (status?: number) => void,
collectorExporter: CollectorExporter,
resource: Resource
resource: collectorTypes.Resource
) {
const exportTraceServiceRequest: collectorTypes.ExportTraceServiceRequest = {
node: {
Expand All @@ -69,7 +68,7 @@ export function sendSpans(
},
attributes: collectorExporter.attributes,
},
resource: resource.labels,
resource,
spans,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,13 @@ export function sendSpans(
onSuccess: () => void,
onError: (status?: number) => void,
collectorExporter: CollectorExporter,
resource: Resource
resource: collectorTypes.Resource
) {
const exportTraceServiceRequest: collectorTypes.ExportTraceServiceRequest = {
node: {
identifier: {
hostName: collectorExporter.hostName,
startTimestamp: core.hrTimeToTimeStamp(core.hrTime()),
},
libraryInfo: {
language: collectorTypes.LibraryInfoLanguage.NODE_JS,
coreLibraryVersion: core.VERSION,
exporterVersion: VERSION,
},
serviceInfo: {
name: collectorExporter.serviceName,
},
attributes: collectorExporter.attributes,
},
resource: resource.labels,
const exportTraceServiceRequest = toCollectorTraceServiceRequest(
spans,
};
collectorExporter,
resource
);
const body = JSON.stringify(exportTraceServiceRequest);
const parsedUrl = url.parse(collectorExporter.url);

Expand Down Expand Up @@ -108,3 +94,29 @@ export function sendSpans(
req.write(body);
req.end();
}

export function toCollectorTraceServiceRequest(
spans: collectorTypes.Span[],
collectorExporter: CollectorExporter,
resource: collectorTypes.Resource
): collectorTypes.ExportTraceServiceRequest {
return {
node: {
identifier: {
hostName: collectorExporter.hostName,
startTimestamp: core.hrTimeToTimeStamp(core.hrTime()),
},
libraryInfo: {
language: collectorTypes.LibraryInfoLanguage.NODE_JS,
coreLibraryVersion: core.VERSION,
exporterVersion: VERSION,
},
serviceInfo: {
name: collectorExporter.serviceName,
},
attributes: collectorExporter.attributes,
},
resource,
spans,
};
}
16 changes: 16 additions & 0 deletions packages/opentelemetry-exporter-collector/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { hexToBase64, hrTimeToTimeStamp } from '@opentelemetry/core';
import { ReadableSpan } from '@opentelemetry/tracing';
import { Attributes, Link, TimedEvent, TraceState } from '@opentelemetry/api';
import * as collectorTypes from './types';
import { Resource } from '@opentelemetry/resources';

const OT_MAX_STRING_LENGTH = 128;

Expand Down Expand Up @@ -201,6 +202,21 @@ export function toCollectorSpan(span: ReadableSpan): collectorTypes.Span {
};
}

/**
* converts span resource
* @param resource
*/
export function toCollectorResource(
resource: Resource
): collectorTypes.Resource {
const labels: { [key: string]: string } = {};
Object.keys(resource.labels).forEach(
name => (labels[name] = String(resource.labels[name]))
);
// @TODO: add type support
return { labels };
}

/**
* @param traceState
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Attributes, TimedEvent } from '@opentelemetry/api';
import * as assert from 'assert';
import * as transform from '../../src/transform';
import { ensureSpanIsCorrect, mockedReadableSpan } from '../helper';
import { Resource } from '@opentelemetry/resources';

describe('transform', () => {
describe('toCollectorTruncatableString', () => {
Expand Down Expand Up @@ -149,4 +150,23 @@ describe('transform', () => {
ensureSpanIsCorrect(transform.toCollectorSpan(mockedReadableSpan));
});
});

describe('toCollectorResource', () => {
it('should convert resource', () => {
const resource = transform.toCollectorResource(
new Resource({
service: 'ui',
version: 1.0,
success: true,
})
);
assert.deepStrictEqual(resource, {
labels: {
service: 'ui',
version: '1',
success: 'true',
},
});
});
});
});
6 changes: 5 additions & 1 deletion packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export const mockedReadableSpan: ReadableSpan = {
},
],
duration: [0, 8885000],
resource: Resource.empty(),
resource: new Resource({
service: 'ui',
version: 1,
cost: 112.12,
}),
};

export function ensureSpanIsCorrect(span: collectorTypes.Span) {
Expand Down

0 comments on commit b7c7dc0

Please sign in to comment.