Skip to content

Commit

Permalink
fix-3976 fully propagate traceId to user executed code and into bot-i…
Browse files Browse the repository at this point in the history
…njected medplum client (medplum#3977)

* fix-3976 fully propagate traceId to user executed code and into bot-injected medplum client

* refactor/simplify
  • Loading branch information
dillonstreator committed Feb 16, 2024
1 parent 7fdb50c commit de46769
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export interface BotEvent<T = Resource | Hl7Message | string | Record<string, an
readonly contentType: string;
readonly input: T;
readonly secrets: Record<string, ProjectSecret>;
readonly traceId?: string;
}

export interface InviteRequest {
Expand Down
11 changes: 8 additions & 3 deletions packages/server/src/fhir/operations/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ const PdfPrinter = require("pdfmake");
const userCode = require("./user.js");
exports.handler = async (event, context) => {
const { baseUrl, accessToken, contentType, secrets } = event;
const { baseUrl, accessToken, contentType, secrets, traceId } = event;
const medplum = new MedplumClient({
baseUrl,
fetch,
fetch: function(url, options = {}) {
options.headers ||= {};
options.headers['X-Trace-Id'] = traceId;
options.headers['traceparent'] = traceId;
return fetch(url, options);
},
createPdf,
});
medplum.setAccessToken(accessToken);
Expand All @@ -47,7 +52,7 @@ exports.handler = async (event, context) => {
if (contentType === ContentType.HL7_V2 && input) {
input = Hl7Message.parse(input);
}
let result = await userCode.handler(medplum, { input, contentType, secrets });
let result = await userCode.handler(medplum, { input, contentType, secrets, traceId });
if (contentType === ContentType.HL7_V2 && result) {
result = result.toString();
}
Expand Down
11 changes: 8 additions & 3 deletions packages/server/src/fhir/operations/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,23 @@ async function runInVmContext(request: BotExecutionRequest): Promise<BotExecutio
// End user code
(async () => {
const { baseUrl, accessToken, contentType, secrets } = event;
const { baseUrl, accessToken, contentType, secrets, traceId } = event;
const medplum = new MedplumClient({
baseUrl,
fetch,
fetch: function(url, options = {}) {
options.headers ||= {};
options.headers['X-Trace-Id'] = traceId;
options.headers['traceparent'] = traceId;
return fetch(url, options);
},
});
medplum.setAccessToken(accessToken);
try {
let input = event.input;
if (contentType === ContentType.HL7_V2 && input) {
input = Hl7Message.parse(input);
}
let result = await exports.handler(medplum, { input, contentType, secrets });
let result = await exports.handler(medplum, { input, contentType, secrets, traceId });
if (contentType === ContentType.HL7_V2 && result) {
result = result.toString();
}
Expand Down

0 comments on commit de46769

Please sign in to comment.