Skip to content

Commit

Permalink
feat: update propagator and ID generator to support opentelemetry-js …
Browse files Browse the repository at this point in the history
…0.12.x (#36)

* feat: update propagator to support opentelemetry-js 0.12.x

* chore: bump @opentelemetry/core dependency version to ^0.12.0

* chore: update integration tests for opentelemetry-js 0.12.x

Also remove some unused dependencies and set private to true
in package.json to prevent accidental npm publishes.
  • Loading branch information
mickdekkers committed Dec 1, 2020
1 parent b3d9142 commit 56d37a6
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 92 deletions.
23 changes: 10 additions & 13 deletions integ-test/js-sdk-emitter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "test-app",
"version": "0.11.0",
"version": "0.12.0",
"private": true,
"description": "test-application for javascript SDK",
"main": "index.js",
"scripts": {
Expand All @@ -17,19 +18,15 @@
},
"dependencies": {
"@lerna/link": "^3.21.0",
"@opentelemetry/api": "^0.11.0",
"@opentelemetry/context-base": "^0.11.0",
"@opentelemetry/core": "^0.11.0",
"@opentelemetry/plugin-grpc": "^0.11.0",
"@opentelemetry/api": "^0.12.0",
"@opentelemetry/core": "^0.12.0",
"@opentelemetry/plugin-grpc": "^0.12.0",
"@opentelemetry/exporter-collector-grpc": "^0.12.0",
"@opentelemetry/node": "^0.11.0",
"@opentelemetry/plugin-http": "^0.11.0",
"@opentelemetry/propagator-jaeger": "^0.9.0",
"@opentelemetry/resource-detector-aws": "^0.10.3-alpha.28",
"@opentelemetry/resource-detector-gcp": "^0.10.3-alpha.28",
"@opentelemetry/resources": "^0.11.0",
"@opentelemetry/sdk-node": "^0.11.0",
"@opentelemetry/tracing": "^0.11.0",
"@opentelemetry/node": "^0.12.0",
"@opentelemetry/plugin-http": "^0.12.0",
"@opentelemetry/resource-detector-aws": "^0.12.0",
"@opentelemetry/resources": "^0.12.0",
"@opentelemetry/tracing": "^0.12.0",
"AWSXRayIdGenerator": "file:../../packages/opentelemetry-id-generator-aws-xray",
"AWSXRayPropagator": "file:../../packages/opentelemetry-propagator-aws-xray"
},
Expand Down
2 changes: 1 addition & 1 deletion integ-test/js-sdk-emitter/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { AwsXRayIdGenerator } = require('AWSXRayIdGenerator');

const { context, propagation, trace } = require("@opentelemetry/api");
const { awsEc2Detector } = require('@opentelemetry/resource-detector-aws');
const { detectResources } = require('@opentelemetry/resources/build/src/platform/node/detect-resources');
const { detectResources } = require('@opentelemetry/resources');

module.exports = (serviceName) => {
// set global propagator
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-id-generator-aws-xray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
"webpack": "4.43.0"
},
"dependencies": {
"@opentelemetry/core": "^0.10.2"
"@opentelemetry/core": "^0.12.0"
}
}
5 changes: 3 additions & 2 deletions packages/opentelemetry-propagator-aws-xray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"access": "public"
},
"devDependencies": {
"@opentelemetry/context-base": "^0.12.0",
"@opentelemetry/core": "^0.12.0",
"@types/mocha": "7.0.2",
"@types/node": "12.12.47",
"@types/sinon": "9.0.4",
Expand All @@ -71,7 +73,6 @@
"webpack": "4.43.0"
},
"dependencies": {
"@opentelemetry/api": "^0.10.2",
"@opentelemetry/core": "^0.10.2"
"@opentelemetry/api": "^0.12.0"
}
}
42 changes: 19 additions & 23 deletions packages/opentelemetry-propagator-aws-xray/src/AWSXRayPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

import {
Context,
HttpTextPropagator,
TextMapPropagator,
SpanContext,
TraceFlags,
SetterFunction,
GetterFunction,
INVALID_TRACE_ID,
INVALID_SPAN_ID,
} from '@opentelemetry/api';
import {
isSpanContextValid,
isValidSpanId,
isValidTraceId,
INVALID_TRACEID,
INVALID_SPANID,
getParentSpanContext,
setExtractedSpanContext,
INVALID_SPAN_CONTEXT,
isValid,
} from '@opentelemetry/core';
} from '@opentelemetry/api';

export const AWSXRAY_TRACE_ID_HEADER = 'X-Amzn-Trace-Id';

Expand All @@ -51,8 +51,6 @@ const SAMPLED_FLAG_LENGTH = 1;
const IS_SAMPLED = '1';
const NOT_SAMPLED = '0';

const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i;
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
/**
* Implementation of the AWS X-Ray Trace Header propagation protocol. See <a href=
* https://https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader>AWS
Expand All @@ -61,10 +59,10 @@ const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
* An example AWS Xray Tracing Header is shown below:
* X-Amzn-Trace-Id: Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1
*/
export class AWSXRayPropagator implements HttpTextPropagator {
export class AWSXRayPropagator implements TextMapPropagator {
inject(context: Context, carrier: unknown, setter: SetterFunction) {
const spanContext = getParentSpanContext(context);
if (!spanContext || !isValid(spanContext)) return;
if (!spanContext || !isSpanContextValid(spanContext)) return;

const otTraceId = spanContext.traceId;

Expand Down Expand Up @@ -96,7 +94,7 @@ export class AWSXRayPropagator implements HttpTextPropagator {

extract(context: Context, carrier: unknown, getter: GetterFunction): Context {
const spanContext = this.getSpanContextFromHeader(carrier, getter);
if (!isValid(spanContext)) return context;
if (!isSpanContextValid(spanContext)) return context;

return setExtractedSpanContext(context, spanContext);
}
Expand All @@ -114,8 +112,8 @@ export class AWSXRayPropagator implements HttpTextPropagator {

let pos = 0;
let trimmedPart: string;
let parsedTraceId = INVALID_TRACE_ID;
let parsedSpanId = INVALID_SPAN_ID;
let parsedTraceId = INVALID_TRACEID;
let parsedSpanId = INVALID_SPANID;
let parsedTraceFlags = null;
while (pos < traceHeader.length) {
const delimiterIndex = traceHeader.indexOf(TRACE_HEADER_DELIMITER, pos);
Expand Down Expand Up @@ -150,7 +148,7 @@ export class AWSXRayPropagator implements HttpTextPropagator {
traceFlags: parsedTraceFlags,
isRemote: true,
};
if (!isValid(resultSpanContext)) {
if (!isSpanContextValid(resultSpanContext)) {
return INVALID_SPAN_CONTEXT;
}
return resultSpanContext;
Expand All @@ -159,20 +157,20 @@ export class AWSXRayPropagator implements HttpTextPropagator {
private _parseTraceId(xrayTraceId: string): string {
// Check length of trace id
if (xrayTraceId.length !== TRACE_ID_LENGTH) {
return INVALID_TRACE_ID;
return INVALID_TRACEID;
}

// Check version trace id version
if (!xrayTraceId.startsWith(TRACE_ID_VERSION)) {
return INVALID_TRACE_ID;
return INVALID_TRACEID;
}

// Check delimiters
if (
xrayTraceId.charAt(TRACE_ID_DELIMITER_INDEX_1) !== TRACE_ID_DELIMITER ||
xrayTraceId.charAt(TRACE_ID_DELIMITER_INDEX_2) !== TRACE_ID_DELIMITER
) {
return INVALID_TRACE_ID;
return INVALID_TRACEID;
}

const epochPart = xrayTraceId.substring(
Expand All @@ -186,17 +184,15 @@ export class AWSXRayPropagator implements HttpTextPropagator {
const resTraceId = epochPart + uniquePart;

// Check the content of trace id
if (!VALID_TRACEID_REGEX.test(resTraceId)) {
return INVALID_TRACE_ID;
if (!isValidTraceId(resTraceId)) {
return INVALID_TRACEID;
}

return resTraceId;
}

private _parseSpanId(xrayParentId: string): string {
return VALID_SPANID_REGEX.test(xrayParentId)
? xrayParentId
: INVALID_SPAN_ID;
return isValidSpanId(xrayParentId) ? xrayParentId : INVALID_SPANID;
}

private _parseTraceFlag(xraySampledFlag: string): TraceFlags | null {
Expand Down
Loading

0 comments on commit 56d37a6

Please sign in to comment.