@oas-tools/oas-telemetry 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -13,6 +13,7 @@ var _jsYaml = _interopRequireDefault(require("js-yaml"));
13
13
  var _ui = _interopRequireDefault(require("./ui.cjs"));
14
14
  var _axios = _interopRequireDefault(require("axios"));
15
15
  var _importFromString = require("import-from-string");
16
+ var _dynamicInstaller = require("dynamic-installer");
16
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18
  // telemetryMiddleware.js
18
19
 
@@ -224,7 +225,20 @@ const registerPlugin = async (req, res) => {
224
225
  res.status(400).send(`Plugin code could not be loaded`);
225
226
  return;
226
227
  }
228
+ //install dependencies if any
229
+ if (pluginResource.install) {
230
+ const dependenciesStatus = await (0, _dynamicInstaller.installDependencies)(pluginResource.install);
231
+ if (!dependenciesStatus.success) {
232
+ if (pluginResource.install.ignoreErrors === true) {
233
+ console.warn(`Warning: Error installing dependencies: ${JSON.stringify(dependenciesStatus.details)}`);
234
+ } else {
235
+ res.status(400).send(`Error installing dependencies: ${JSON.stringify(dependenciesStatus.details)}`);
236
+ return;
237
+ }
238
+ }
239
+ }
227
240
  dbglog("Plugin size: " + pluginCode?.length);
241
+ dbglog("Plugin format: " + pluginResource?.moduleFormat);
228
242
  if (pluginResource?.moduleFormat && pluginResource.moduleFormat.toUpperCase() == "ESM") {
229
243
  console.log("ESM detected");
230
244
  module = await (0, _importFromString.importFromString)(pluginCode);
@@ -238,19 +252,28 @@ const registerPlugin = async (req, res) => {
238
252
  res.status(400).send(`Error loading plugin: ${error}`);
239
253
  return;
240
254
  }
241
- if (module.plugin == undefined) {
255
+
256
+ // Check if the plugin is in module.default or module.plugin (supports default syntax)
257
+ const plugin = module.default?.plugin || module.plugin;
258
+ if (plugin == undefined) {
242
259
  res.status(400).send(`Plugin code should export a "plugin" object`);
243
260
  console.log("Error in plugin code: no plugin object exported");
244
261
  return;
245
262
  }
246
263
  for (let requiredFunction of ["load", "getName", "isConfigured"]) {
247
- if (module.plugin[requiredFunction] == undefined) {
264
+ if (plugin[requiredFunction] == undefined) {
248
265
  res.status(400).send(`The plugin code exports a "plugin" object, however it should have a "${requiredFunction}" method`);
249
266
  console.log("Error in plugin code: some required functions are missing");
250
267
  return;
251
268
  }
252
269
  }
253
- let plugin = module.plugin;
270
+ try {
271
+ await plugin.load(pluginResource.config);
272
+ } catch (error) {
273
+ console.error(`Error loading plugin configuration: ${error}`);
274
+ res.status(400).send(`Error loading plugin configuration: ${error}`);
275
+ return;
276
+ }
254
277
  if (plugin.isConfigured()) {
255
278
  dbglog(`Loaded plugin <${plugin.getName()}>`);
256
279
  pluginResource.plugin = plugin;
@@ -0,0 +1,16 @@
1
+ export class InMemoryExporter {
2
+ static plugins: any[];
3
+ _spans: any;
4
+ _stopped: boolean;
5
+ export(readableSpans: any, resultCallback: any): any;
6
+ start(): void;
7
+ stop(): void;
8
+ shutdown(): Promise<void>;
9
+ /**
10
+ * Exports any pending spans in the exporter
11
+ */
12
+ forceFlush(): Promise<void>;
13
+ reset(): void;
14
+ getFinishedSpans(): any;
15
+ activatePlugin(plugin: any): void;
16
+ }
@@ -0,0 +1 @@
1
+ export default function oasTelemetry(tlConfig?: any): import("express-serve-static-core").Router;
@@ -0,0 +1,2 @@
1
+ export const inMemoryExporter: InMemoryExporter;
2
+ import { InMemoryExporter } from './exporters/InMemoryDbExporter.js';
@@ -0,0 +1,4 @@
1
+ export default function ui(): {
2
+ main: string;
3
+ detail: string;
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oas-tools/oas-telemetry",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "This package exports an Express middleware that traces requests and responses of an Express application using OpenTelemetry.",
5
5
  "author": "Manuel Otero",
6
6
  "contributors": [
@@ -47,7 +47,7 @@
47
47
  "@opentelemetry/resources": "^1.24.0",
48
48
  "@opentelemetry/sdk-node": "^0.49.1",
49
49
  "axios": "^1.6.8",
50
- "dynamic-installer": "^1.0.1",
50
+ "dynamic-installer": "^1.1.1",
51
51
  "express": "^4.19.2",
52
52
  "import-from-string": "^0.0.4",
53
53
  "js-yaml": "^4.1.0",
package/src/index.js CHANGED
@@ -254,6 +254,7 @@ const registerPlugin = async (req, res) => {
254
254
  }
255
255
 
256
256
  dbglog("Plugin size: " + pluginCode?.length);
257
+ dbglog("Plugin format: " + pluginResource?.moduleFormat);
257
258
  if (pluginResource?.moduleFormat && pluginResource.moduleFormat.toUpperCase() == "ESM") {
258
259
  console.log("ESM detected")
259
260
  module = await importFromString(pluginCode)
@@ -268,20 +269,22 @@ const registerPlugin = async (req, res) => {
268
269
  return;
269
270
  }
270
271
 
271
- if (module.plugin == undefined) {
272
- res.status(400).send(`Plugin code should export a "plugin" object`);
273
- console.log("Error in plugin code: no plugin object exported")
272
+ // Check if the plugin is in module.default or module.plugin (supports default syntax)
273
+ const plugin = module.default?.plugin || module.plugin;
274
+
275
+ if (plugin == undefined) {
276
+ res.status(400).send(`Plugin code should export a "plugin" object`);
277
+ console.log("Error in plugin code: no plugin object exported");
278
+ return;
279
+ }
280
+ for (let requiredFunction of ["load", "getName", "isConfigured"]) {
281
+ if (plugin[requiredFunction] == undefined) {
282
+ res.status(400).send(`The plugin code exports a "plugin" object, however it should have a "${requiredFunction}" method`);
283
+ console.log("Error in plugin code: some required functions are missing");
274
284
  return;
275
285
  }
276
- for (let requiredFunction of ["load", "getName", "isConfigured"]) {
277
- if (module.plugin[requiredFunction] == undefined) {
278
- res.status(400).send(`The plugin code exports a "plugin" object, however it should have a "${requiredFunction}" method`);
279
- console.log("Error in plugin code: some required functions are missing")
280
- return;
281
- }
282
- }
286
+ }
283
287
 
284
- let plugin = module.plugin
285
288
  try {
286
289
  await plugin.load(pluginResource.config);
287
290
  } catch (error) {