Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"generate-source": "scripts/protoc.sh",
"build": "npm run clean && npm run prepare && tsc --build src && OUT_DIR=lib/proto/ scripts/protoc.sh",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint --fix src/**/*.ts",
"lint:fix": "prettier --write \"src/**/*.ts\"",
"lint-staged": "lint-staged",
"test": "DEBUG=testcontainers* jest",
"format": "prettier --write \"src/**/*.ts\"",
Expand Down
47 changes: 28 additions & 19 deletions src/trace/context/ContextCarrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,28 @@ export class ContextCarrier extends CarrierItem {
};

get value(): string {
return [
'1',
this.encode(this.traceId?.toString() || ''),
this.encode(this.segmentId?.toString() || ''),
this.spanId?.toString(),
this.encode(this.service || ''),
this.encode(this.serviceInstance || ''),
this.encode(this.endpoint || ''),
this.encode(this.clientAddress || ''),
].join('-');
return this.isValid()
? [
'1',
this.encode(this.traceId?.toString() || ''),
this.encode(this.segmentId?.toString() || ''),
this.spanId?.toString(),
this.encode(this.service || ''),
this.encode(this.serviceInstance || ''),
this.encode(this.endpoint || ''),
this.encode(this.clientAddress || ''),
].join('-')
: '';
}

set value(val) {
if (!val) {
return;
}
const parts = val.split('-');
if (parts.length != 8) {
return;
}
this.traceId = new ID(this.decode(parts[1]));
this.segmentId = new ID(this.decode(parts[2]));
this.spanId = Number.parseInt(parts[3], 10);
Expand All @@ -70,22 +78,23 @@ export class ContextCarrier extends CarrierItem {
isValid(): boolean {
return Boolean(
this.traceId?.rawId &&
this.segmentId?.rawId &&
this.spanId !== undefined &&
!isNaN(this.spanId) &&
this.service &&
this.endpoint &&
this.clientAddress !== undefined
this.segmentId?.rawId &&
this.spanId !== undefined &&
!isNaN(this.spanId) &&
this.service &&
this.endpoint &&
this.clientAddress !== undefined,
);
}

public static from(map: { [key: string]: string }): ContextCarrier | undefined {
if (!map.hasOwnProperty('sw8'))
return;
if (!Object.prototype.hasOwnProperty.call(map, 'sw8')) return;

const carrier = new ContextCarrier();

carrier.items.filter((item) => map.hasOwnProperty(item.key)).forEach((item) => (item.value = map[item.key]));
carrier.items
.filter((item) => Object.prototype.hasOwnProperty.call(map, item.key))
.forEach((item) => (item.value = map[item.key]));

return carrier;
}
Expand Down