I'm trying to add epsagon as a layer instead of wr...
# sst
r
I'm trying to add epsagon as a layer instead of wrapping all my functions. I'm using
Copy code
const epsagon = LayerVersion.fromLayerVersionArn(this, 'EpsagonLayer', process.env.EPSAGON_LAYER_ARN);
    this.addDefaultFunctionLayers([epsagon]);
    this.addDefaultFunctionEnv({
      EPSAGON_TOKEN: process.env.EPSAGON_TOKEN,
      EPSAGON_APP_NAME: process.env.EPSAGON_APP_NAME,
      NODE_OPTIONS: '-r epsagon-frameworks',
    });
I can see that all the necessary env vars are getting set. However, and everything deploys okay with
sst start
but when executing a function it fails and I can see a log in Cloudwatch that says:
Copy code
internal/modules/cjs/loader.js:892
throw err;
^
Error: Cannot find module 'epsagon-frameworks'
Require stack:
- internal/preload
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
at Function.Module._load (internal/modules/cjs/loader.js:745:27)
at Module.require (internal/modules/cjs/loader.js:961:19)
at Module._preloadModules (internal/modules/cjs/loader.js:1231:12)
at loadPreloadModules (internal/bootstrap/pre_execution.js:468:5)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:71:3)
at internal/main/run_main_module.js:7:1 {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'internal/preload' ]
}
a
Is not the same.
Wrapping gives you tracing.
Layer is not.
For me, it worked.
I think the node-options is not needed, that’s what @thdxr told me I think.
This what I did….
Copy code
app.setDefaultFunctionProps(stack => {
    return {
      runtime: 'nodejs14.x',
      srcPath: 'backend/src/',
      logRetention: RetentionDays.TWO_WEEKS,
      tracing: Tracing.DISABLED,
      layers: [getEpsagonLayer(stack)],
      environment: {
        EPSAGON_APP_NAME: stack.stackName,
        EPSAGON_TOKEN: getEpsagonToken(stack),
      },
    };
  });
r
I'll try that It should allow tracing though, docs state
Copy code
Our layers are publicly available for Python and Node.js Lambda runtimes. With just a few Environment Variables, the layer will automatically add tracing to your functions.
a
Copy code
export function getEpsagonLayer(stack: Stack): ILayerVersion {
  return LayerVersion.fromLayerVersionArn(stack, 'EpsagonLayer', constants.EPSAGON.LAYER_ARN);
}

export function getEpsagonToken(stack: Stack): string {
  return StringParameter.valueForStringParameter(stack, constants.PARAMETERS.EPSAGON_TOKEN);
}
I don’t get traces.
Not sure why.
I talked to Support and they told me to use wrapper.
😞
I wish Layer would work.
r
I think the epsagon-frameworks module is needed from tracing
a
Maybe.
That’s why you need wrapper thoguth.
r
that sucks
!
a
Yeah but.
If you can get more details, maybe there is a way.
With what Ive showed you, is not doing tracing.
😞
So not sure what it does to be honest.
Because I need to enable it manually from the site.
r
Ok - I'll get on to them, will have to go wrapper route for now though as I've got a lot of time pressure 😞
a
Sh*t.
Well I’m using middy.
I was looking how to create a middleware for epsagon.
But can’t find a way.
Do you use middy?
r
I don't, I know the guys are thinking about options for middleware in SST
f
@Ross Coundon just to clarify, executing the function worked in
sst start
, but not after
sst deploy
?
Let me loop in @thdxr
r
I'd only tried
sst start
f
Ah yeah that makes sense, and the above error you are getting in your terminal right?
r
No, that was in Cloudwatch. However, I'm now struggling to get local debugging working even after removing it.
@Adrián Mouly experienced similar regarding epsagon. However, I suspect I've got something else unusual going on regarding live debugging this app
a
Yeah I didn’t get this error.
Only concerned about not having auto tracing.
f
@Ross Coundon can u try removing
NODE_OPTIONS: '-r epsagon-frameworks',
?
r
I can, but that's effectively the tracing library
f
Just talked to @thdxr, can you try this:
Copy code
if (!app.local) {
  stack.addDefaultFunctionEnv({
    EPSAGON_TOKEN: "<token>",
    EPSAGON_APP_NAME: "<app_name>",
    NODE_OPTIONS: "-r epsagon-frameworks"
  })
}
t
I figured you don't need tracing in local dev
a
Going to try
NODE_OPTIONS
too.
@Frank I have this in my config:
Copy code
app.setDefaultFunctionProps(stack => {
    return {
      runtime: 'nodejs14.x',
      srcPath: 'backend/src/services',
      logRetention: RetentionDays.TWO_WEEKS,
      tracing: Tracing.DISABLED,
      layers: [getEpsagonLayer(stack)],
      environment: {
        EPSAGON_APP_NAME: stack.stackName,
        EPSAGON_TOKEN: getEpsagonToken(stack),
      },
    };
  });
In my
index
.
How you propose to do it?
I’m thinking to add
NODE_OPTIONS
with an inline if.
Like…
Copy code
environment: {
        EPSAGON_APP_NAME: stack.stackName,
        EPSAGON_TOKEN: getEpsagonToken(stack),
        NODE_OPTIONS: app.local ? '-r epsagon-frameworks' : '',
      },