Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Jsdocs (#20)
Browse files Browse the repository at this point in the history
* Add docstrings for public methods

* Update package version
  • Loading branch information
eduardo-mw authored Apr 25, 2024
1 parent 15a8253 commit 64174f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middleware.io/node-apm",
"version": "1.2.12",
"version": "1.2.13",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
51 changes: 50 additions & 1 deletion packages/init.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,62 @@
const otel = require('@opentelemetry/api')
const {log} = require('./logger')
let config;

/**
* Initialize the APM with the provided configuration object
* @param {{serviceName: string, accessToken: string, target:string, DEBUG: otel.DiagLogLevel, collectMetrics: boolean, enableProfiling:boolean, consoleLog: boolean}} newConfig - Configuration object
* @returns {undefined}
*/
module.exports.track = (newConfig = {}) => {
config = require('./config').init(newConfig)
require('./profiler').init(config);
};

/**
* Info level log
* @param {string} message - Log message
* @param {Object<string,any>} attributes - Log attributes as key value pairs
* @returns {undefined}
*/
module.exports.info = (message, attributes = {}) => {
log('INFO', message, attributes);
};

/**
* Warn level log
* @param {string} message - Log message
* @param {Object<string,any>} attributes - Log attributes as key value pairs
* @returns {undefined}
*/
module.exports.warn = (message, attributes = {}) => {
log('WARN', message, attributes);
};

/**
* Debug level log
* @param {string} message - Log message
* @param {Object<string,any>} attributes - Log attributes as key value pairs
* @returns {undefined}
*/
module.exports.debug = (message, attributes = {}) => {
log('DEBUG', message, attributes);
};

/**
* Error level log
* @param {string} message - Log message
* @param {Object<string,any>} attributes - Log attributes as key value pairs
* @returns {undefined}
*/
module.exports.error = (message, attributes = {}) => {
log('ERROR', message, attributes);
};


/**
* Record an error in the current span
* @param {Error} e - Error object
* @returns {undefined}
*/
module.exports.errorRecord = (e) => {
const span = otel.trace.getSpan(otel.context.active())
if(span){
Expand All @@ -31,19 +65,34 @@ module.exports.errorRecord = (e) => {
}
};

/**
* Set an attribute in the current span
* @param {string} name - Attribute key
* @param {any} value - Attribute value
* @returns {undefined}
*/
module.exports.setAttribute = (name,value) => {
const span = otel.trace.getSpan(otel.context.active())
if(span){
span.setAttribute(name,value)
}
};

/**
* Creates a meter from the meter provider. Used to create custom metrics.
* @returns {otel.Meter}
*/
module.exports.getMeter =() => {
if (config.meterProvider){
return config.meterProvider.getMeter(config.serviceName)
}
return false
}

/**
* Creates a tracer from the tracer provider. Used to create custom spans.
* @returns {otel.Tracer}
*/
module.exports.getTracer =() => {
return otel.trace.getTracer(config.serviceName)
}

0 comments on commit 64174f2

Please sign in to comment.