has anyone gotten XRay instrumentation working in ...
# help
s
has anyone gotten XRay instrumentation working in SST when using
sst start
? I keep getting an error:
Error: Failed to get the current sub/segment from the context. at Object.contextMissingRuntimeError [as contextMissing]
but this doesn’t happen once I deploy to production. this probably has something to do with SST’s trickery of executing function code locally
f
Hey @Sam Hulick when running locally, can u try not wrapping the aws sdk with xray, and see if the error goes away?
s
Oh yeah, if I don't capture the client with x-ray, it's fine
f
I see. And if u log the context object, and u share what u get?
s
Which context?
f
The lambda functions should get an event and a context object. That context.
s
Ah ok. Cuz there's an X-ray segment context too. I'll check in a bit and get back to you
ok, I’m around. gonna check this out now
Copy code
{
  context: {
    callbackWaitsForEmptyEventLoop: [Getter/Setter],
    succeed: [Function (anonymous)],
    fail: [Function (anonymous)],
    done: [Function (anonymous)],
    functionVersion: '$LATEST',
    functionName: 'dev-microservices-api-users-getGroups',
    memoryLimitInMB: undefined,
    logGroupName: undefined,
    logStreamName: undefined,
    clientContext: {},
    identity: {},
    invokedFunctionArn: 'arn:aws:lambda:us-east-1:xxx:function:dev-microservices-api-users-getGroups',
    awsRequestId: '8c58c305-68de-4cde-829e-b28d7013c859',
    getRemainingTimeInMillis: [Function: getRemainingTimeInMillis]
  }
}
f
Hey @Sam Hulick, just added a fix for this to disable X-Ray when functions are invoked locally (sst start).
s
@Frank oh thanks! I actually wrote a handy function that detects if
IS_LOCAL
is present, and if so, it makes a sort of mock X-Ray interface that console.logs the segments & runtimes
f
oh interesting.. can u share what the interface looks like?
s
of course. I abstracted XRay a bit because it’s too verbose
usage:
Copy code
startSegment('Do the thing');
doIt();
endSegment();
also, I didn’t want to use dependency injection to pass the subsegment into any functions called by the calling function.. so this is way easier
f
I see. So I just added the
AWS_XRAY_CONTEXT_MISSING
environment variable so xray doesn’t throw the error. Would that work for you? Or is there something u needed from mocking it?
s
well, I think if you set that var, it won’t throw an error but it will dump a bunch of stuff to the console, correct?
f
I’m also setting
AWS_XRAY_LOG_LEVEL
to
silent
s
ahh! cool
yeah, that works 🙂
I’ll still use my mock function for local, though, since it sorta imitates X-Ray by dumping segments to the console
f
Makes sense. Took a note of ur mock approach here in the case ppl are looking for something similar.
s
nice!