Issue creating plugin for Momentjs library

I want to be able to use the momentjs library in my filter expressions so i have been attempting to create a sensu asset for it.

This is what i have done https://github.com/Chilinot/sensu_momentjs

This is my sensu asset definition:

    ---
    type: Asset
    api_version: core/v2
    metadata:
      name: sensu-momentjs
    spec:
      builds:
        - url: https://github.com/Chilinot/sensu_momentjs/releases/download/0.2.0/sensu-momentjs_0.2.0.tar.gz
          sha512: 86464c27f32c261d19fb2b1997d70ba35a71d6e3a6fc49db3053104e963317d0e5153453877995b58ae990329678840bc16b30941bb133031c484ba06066622d

However, in my sensu-backend logs i get this error:
{"component":"pipelined","error":"error evaluating /var/cache/sensu/sensu-backend/86464c27f32c261d19fb2b1997d70ba35a71d6e3a6fc49db3053104e963317d0e5153453877995b58ae990329678840bc16b30941bb133031c484ba06066622d/lib/._moment.min.js: (anonymous): Line 1:1 Unexpected token ILLEGAL (and 216 more errors)","level":"error","msg":"error executing JS","time":"2020-08-10T16:53:30Z"}

Reading through the sensu-go source code i found out that sensu uses the Otto javascript interpreter. So i installed it on my own system and tried to execute the library using it. However, that resulted in no errors. Everything seemed to work when i did it manually.

Is there something im missing? The javascript code is proper, is it the otto version bundled in sensu that is outdated?

1 Like

Can you post your filter definition that calls this library?

runtime_assets:
  - sensu-momentjs

is all that the filter is doing at the moment until i get it properly loaded

Reading the web site for the library, I understand what it’s supposed to do. However, pasting the code from moment.min.js into https://jshint.com/ and http://www.jslint.com/ both produced a lot of warnings/errors.

I’m not entirely sure why when running with otto directly it doesn’t produce any errors. And I am no JS expert, but the Otto README does state:

* Otto targets ES5. ES6 features (eg: Typed Arrays) are not supported.

So I would be curious to know if that has any effect on it.

Hi @lucasemanuel

It looks like you used the _moment.min.js file, which I believe is an ES6 module; could you try to use directly the moment.js file?

Alternatively, you could try to use a version prior to 2.10.0 (https://github.com/moment/moment/blob/develop/CHANGELOG.md#2100), which wasn’t published as an ES6 module.

I did not myself specify the _.moment.min.js file. That must’ve been the script itself trying to import it. I believe however that i have got it working now. I bundled the non-minified version now and there are no longer any syntax errors in the logs.

However, if anyone else is interested in the momentjs library for their own filters. There are some issues with momentjs and momentjs-timezone. The order in which they are loaded is important. So i will have to create some workaround for that. As sensu-go seems to load all assets in random order.