Skip to content

Commit

Permalink
export resource to exporters (#843)
Browse files Browse the repository at this point in the history
* export resource to exporters

* update Zipkin and Stackdriver exporter to use Resource

* chore: remove redundant dependency

* update Collector exporter to use Resource

* rebase with #846

* minor

* fix collector resource

* fix build
  • Loading branch information
mayurkale22 committed Mar 12, 2020
1 parent 02c1d66 commit 4627892
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-exporter-collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
},
"devDependencies": {
"@babel/core": "^7.6.0",
"@opentelemetry/resources": "^0.4.0",
"@types/mocha": "^5.2.5",
"@types/node": "^12.6.8",
"@types/sinon": "^7.0.13",
Expand Down Expand Up @@ -83,6 +82,7 @@
"@opentelemetry/api": "^0.4.0",
"@opentelemetry/base": "^0.4.0",
"@opentelemetry/core": "^0.4.0",
"@opentelemetry/resources": "^0.4.0",
"@opentelemetry/tracing": "^0.4.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ 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';

/**
* Collector Exporter Config
Expand Down Expand Up @@ -100,10 +101,13 @@ export class CollectorExporter implements SpanExporter {
toCollectorSpan(span)
);
this.logger.debug('spans to be sent', spansToBeSent);
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
sendSpans(spansToBeSent, resolve, reject, this);
sendSpans(spansToBeSent, resolve, reject, this, resource);
} catch (e) {
reject(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ export function onShutdown(shutdownF: EventListener) {
* @param onSuccess
* @param onError
* @param collectorExporter
* @param resource
*/
export function sendSpans(
spans: collectorTypes.Span[],
onSuccess: () => void,
onError: (status?: number) => void,
collectorExporter: CollectorExporter
collectorExporter: CollectorExporter,
resource: collectorTypes.Resource
) {
const exportTraceServiceRequest: collectorTypes.ExportTraceServiceRequest = {
node: {
Expand All @@ -66,7 +68,7 @@ export function sendSpans(
},
attributes: collectorExporter.attributes,
},
// resource: '', not implemented
resource,
spans,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,20 @@ export function onShutdown(shutdownF: Function) {}
* @param onSuccess
* @param onError
* @param collectorExporter
* @param resource
*/
export function sendSpans(
spans: collectorTypes.Span[],
onSuccess: () => void,
onError: (status?: number) => void,
collectorExporter: CollectorExporter
collectorExporter: CollectorExporter,
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: '', not implemented
const exportTraceServiceRequest = toCollectorTraceServiceRequest(
spans,
};
collectorExporter,
resource
);
const body = JSON.stringify(exportTraceServiceRequest);
const parsedUrl = url.parse(collectorExporter.url);

Expand Down Expand Up @@ -105,3 +93,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
6 changes: 6 additions & 0 deletions packages/opentelemetry-exporter-jaeger/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export function spanToThrift(span: ReadableSpan): ThriftSpan {
if (span.kind !== undefined) {
tags.push({ key: 'span.kind', value: SpanKind[span.kind] });
}
Object.keys(span.resource.labels).forEach(name =>
tags.push({
key: name,
value: toTagValue(span.resource.labels[name]),
})
);

const spanTags: ThriftTag[] = ThriftUtils.getThriftTags(tags);

Expand Down
29 changes: 26 additions & 3 deletions packages/opentelemetry-exporter-jaeger/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ describe('transform', () => {
},
],
duration: [32, 800000000],
resource: Resource.empty(),
resource: new Resource({
service: 'ui',
version: 1,
cost: 112.12,
}),
};

const thriftSpan = spanToThrift(readableSpan);
Expand All @@ -95,8 +99,18 @@ describe('transform', () => {
thriftSpan.startTime,
Utils.encodeInt64(hrTimeToMicroseconds(readableSpan.startTime))
);
assert.strictEqual(thriftSpan.tags.length, 6);
const [tag1, tag2, tag3, tag4, tag5, tag6] = thriftSpan.tags;
assert.strictEqual(thriftSpan.tags.length, 9);
const [
tag1,
tag2,
tag3,
tag4,
tag5,
tag6,
tag7,
tag8,
tag9,
] = thriftSpan.tags;
assert.strictEqual(tag1.key, 'testBool');
assert.strictEqual(tag1.vType, 'BOOL');
assert.strictEqual(tag1.vBool, true);
Expand All @@ -115,6 +129,15 @@ describe('transform', () => {
assert.strictEqual(tag6.key, 'span.kind');
assert.strictEqual(tag6.vType, 'STRING');
assert.strictEqual(tag6.vStr, 'INTERNAL');
assert.strictEqual(tag7.key, 'service');
assert.strictEqual(tag7.vType, 'STRING');
assert.strictEqual(tag7.vStr, 'ui');
assert.strictEqual(tag8.key, 'version');
assert.strictEqual(tag8.vType, 'DOUBLE');
assert.strictEqual(tag8.vDouble, 1);
assert.strictEqual(tag9.key, 'cost');
assert.strictEqual(tag9.vType, 'DOUBLE');
assert.strictEqual(tag9.vDouble, 112.12);
assert.strictEqual(thriftSpan.references.length, 0);

assert.strictEqual(thriftSpan.logs.length, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"access": "public"
},
"devDependencies": {
"@opentelemetry/resources": "^0.4.0",
"@types/mocha": "^5.2.7",
"@types/nock": "^11.1.0",
"@types/node": "^12.6.9",
Expand All @@ -63,6 +62,7 @@
"@opentelemetry/api": "^0.4.0",
"@opentelemetry/base": "^0.4.0",
"@opentelemetry/core": "^0.4.0",
"@opentelemetry/resources": "^0.4.0",
"@opentelemetry/tracing": "^0.4.0",
"google-auth-library": "^5.7.0",
"googleapis": "^46.0.0"
Expand Down
24 changes: 18 additions & 6 deletions packages/opentelemetry-exporter-stackdriver-trace/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TruncatableString,
} from './types';
import { VERSION } from './version';
import { Resource } from '@opentelemetry/resources';

const AGENT_LABEL_KEY = 'g.co/agent';
const AGENT_LABEL_VALUE = `opentelemetry-js [${CORE_VERSION}]; stackdriver-trace-exporter [${VERSION}]`;
Expand All @@ -38,10 +39,14 @@ export function getReadableSpanTransformer(
projectId: string
): (span: ReadableSpan) => Span {
return span => {
const attributes = transformAttributes(span.attributes, {
project_id: projectId,
[AGENT_LABEL_KEY]: AGENT_LABEL_VALUE,
});
const attributes = transformAttributes(
span.attributes,
{
project_id: projectId,
[AGENT_LABEL_KEY]: AGENT_LABEL_VALUE,
},
span.resource
);

const out: Span = {
attributes,
Expand Down Expand Up @@ -85,9 +90,16 @@ function transformLink(link: ot.Link): Link {

function transformAttributes(
requestAttributes: ot.Attributes = {},
serviceAttributes: ot.Attributes = {}
serviceAttributes: ot.Attributes = {},
resource: Resource = Resource.empty()
): Attributes {
const attributes = Object.assign({}, requestAttributes, serviceAttributes);
const attributes = Object.assign(
{},
requestAttributes,
serviceAttributes,
resource.labels
);

const attributeMap = transformAttributeValues(attributes);
return {
attributeMap: attributeMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ describe('transform', () => {
name: 'my-span',
spanContext,
status: { code: types.CanonicalCode.OK },
resource: Resource.empty(),
resource: new Resource({
service: 'ui',
version: 1,
cost: 112.12,
}),
};
});

Expand All @@ -67,6 +71,9 @@ describe('transform', () => {
value: `opentelemetry-js [${CORE_VERSION}]; stackdriver-trace-exporter [${VERSION}]`,
},
},
cost: { intValue: '112' },
service: { stringValue: { value: 'ui' } },
version: { intValue: '1' },
},
droppedAttributesCount: 0,
},
Expand Down Expand Up @@ -130,7 +137,7 @@ describe('transform', () => {
assert.deepStrictEqual(result.attributes!.droppedAttributesCount, 1);
assert.deepStrictEqual(
Object.keys(result.attributes!.attributeMap!).length,
2
5
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-exporter-zipkin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"access": "public"
},
"devDependencies": {
"@opentelemetry/resources": "^0.4.0",
"@types/mocha": "^5.2.7",
"@types/nock": "^10.0.3",
"@types/node": "^12.6.9",
Expand All @@ -59,6 +58,7 @@
"@opentelemetry/api": "^0.4.0",
"@opentelemetry/base": "^0.4.0",
"@opentelemetry/core": "^0.4.0",
"@opentelemetry/resources": "^0.4.0",
"@opentelemetry/tracing": "^0.4.0"
}
}
Loading

0 comments on commit 4627892

Please sign in to comment.