There's probably a better way to do this, but just...
# dev
j
There's probably a better way to do this, but just sharing this example of injecting
dd-trace
(DataDog's tracing library) into the Medplum server via Dockerfile and an... interesting... custom entrypoint:
Copy code
FROM medplum/medplum-server:latest

RUN npm install --save dd-trace

RUN echo "const tracer = require('dd-trace').init();\
const medplum = require(\"./packages/server/dist/index.js\");\
medplum.main(process.argv.length === 3 ? process.argv[2] : 'file:medplum.config.json').catch(console.log)" > entrypoint.js

ENTRYPOINT [ "node", "entrypoint.js" ]
Also tried using ESM loader:
Copy code
FROM medplum/medplum-server:latest
RUN npm install --save dd-trace
ENTRYPOINT [ "node", "--loader", "dd-trace/loader-hook.mjs", "packages/server/dist/index.js" ]
Unfortunately that doesn't work because it causes
require.main
to be undefined
r
Hm, is there some reason the Datadog agent integration won't work? Is it due to the custom setup? https://www.medplum.com/docs/self-hosting/datadog
j
The agent can be used for several features, including Logging and Tracing. Logging mostly works out of box on ECS if you are running a sidecar Agent container like this config. Tracing, however, requires instrumentation at the level of the application -- requires adding the
dd-trace
dependency.
I have a slight improvement over the above though, which is this:
Copy code
FROM medplum/medplum-server:latest

RUN npm install --save dd-trace

ENTRYPOINT [ "node", "--require", "dd-trace/init", "packages/server/dist/index.js" ]
I thought about submitting an upstream PR here but it probably doesn't make sense to so tightly couple this telemetry to a specific vendor. It would be best to instrument with OpenTelemetry in the server application -- the various vendors have SDKs that hook up to it (https://opentelemetry.io/docs/what-is-opentelemetry/)
r
Ah got it - thanks for this context!
j
Here's an example of this working (test data)
164 Views