diff --git a/package.json b/package.json index f61bc26..cd260fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adobe/plugin-hooks", - "version": "0.3.1", + "version": "0.3.2-beta", "publishConfig": { "access": "public" }, diff --git a/src/index.js b/src/index.js index 1cd8d1f..a5641ef 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,8 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const newrelic = require("newrelic"); +const agent = require('./telemetry/register'); + const handleBeforeAllHooks = require("./handleBeforeAllHooks"); async function hooksPlugin(config) { @@ -34,7 +35,7 @@ async function hooksPlugin(config) { return { async onExecute({ args, setResultAndStopExecution, extendContext }) { - await newrelic.startSegment('handleBeforeAllHooks:onExecute', true, async() => { + await agent.startSegment('handleBeforeAllHooks:onExecute', true, async() => { const query = args.contextValue.params.query; diff --git a/src/telemetry/agents/log.js b/src/telemetry/agents/log.js new file mode 100644 index 0000000..68c3597 --- /dev/null +++ b/src/telemetry/agents/log.js @@ -0,0 +1,15 @@ +/** + * Stub replacement for newrelic.startSegment used by `plugin-hooks`. Does NOT report telemetry back to newrelic. + * @param {string} name The name to give the new segment. This will also be the name of the metric. + * @param {boolean} record Indicates if the segment should be recorded as a metric. Metrics will show up on the transaction breakdown table and server breakdown graph. Segments just show up in transaction traces. + * @param {() => Promise}handler The function to track as a segment. + * @param {Function?} _callback An optional callback for the handler. This will indicate the end of the timing if provided. + */ +const startSegment = async (name, record, handler, _callback) => { + console.log(name); + await handler(); +} + +module.exports = { + startSegment, +} diff --git a/src/telemetry/register.js b/src/telemetry/register.js new file mode 100644 index 0000000..47a6079 --- /dev/null +++ b/src/telemetry/register.js @@ -0,0 +1,16 @@ +let agent; + +// Attempt to register newrelic agent +try { + agent = require("newrelic"); +} catch (err) { + console.warn('newrelic agent not installed...'); +} + +// No other telemetry agents were registered so fall back on logging +if (!agent) { + console.log('Falling back on log agent...'); + agent = require('./agents/log'); +} + +module.exports = agent;