Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Relic: add error for agent not being found #1457

Merged
merged 5 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/usenewrelic-agent-error-msg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@envelop/newrelic": patch
---

New Relic: add error for agent not being found
Adds an error message when initializing the new relic plugin
- This error message will occur when the new relic agent is not found when initializing the plugin. Signalling information to a developer that new relic may not be
- installed correctly or may be disabled where this plugin is being instantiated.
75 changes: 39 additions & 36 deletions packages/plugins/newrelic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const useNewRelic = (rawOptions?: UseNewRelicOptions): Plugin => {
const instrumentationApi$ = import('newrelic')
.then(m => m.default || m)
.then(({ shim }) => {
if (!shim?.agent) {
throw new Error("Agent unavailable. Please check your New Relic Agent configuration and ensure New Relic is enabled.")
}
shim.agent.metrics
.getOrCreateMetric(`Supportability/ExternalModules/${AttributeName.COMPONENT_NAME}`)
.incrementCallCount();
Expand Down Expand Up @@ -125,50 +128,50 @@ export const useNewRelic = (rawOptions?: UseNewRelicOptions): Plugin => {

const onResolverCalled: OnResolverCalledHook | undefined = options.trackResolvers
? async ({ args: resolversArgs, info }) => {
const logger = await logger$;
const { returnType, path, parentType } = info;
const formattedPath = flattenPath(path, delimiter);
const currentSegment = instrumentationApi.getActiveSegment();

if (!currentSegment) {
logger.trace('No active segment found at resolver call. Not recording resolver (%s).', formattedPath);
return () => {};
}
const logger = await logger$;
const { returnType, path, parentType } = info;
const formattedPath = flattenPath(path, delimiter);
const currentSegment = instrumentationApi.getActiveSegment();

if (!currentSegment) {
logger.trace('No active segment found at resolver call. Not recording resolver (%s).', formattedPath);
return () => { };
}

const resolverSegment = instrumentationApi.createSegment(
`resolver${delimiter}${formattedPath}`,
null,
operationSegment
);
const resolverSegment = instrumentationApi.createSegment(
`resolver${delimiter}${formattedPath}`,
null,
operationSegment
);

if (!resolverSegment) {
logger.trace('Resolver segment was not created (%s).', formattedPath);
return () => {};
}
if (!resolverSegment) {
logger.trace('Resolver segment was not created (%s).', formattedPath);
return () => { };
}

resolverSegment.start();
resolverSegment.start();

resolverSegment.addAttribute(AttributeName.RESOLVER_FIELD_PATH, formattedPath);
resolverSegment.addAttribute(AttributeName.RESOLVER_TYPE_NAME, parentType.toString());
resolverSegment.addAttribute(AttributeName.RESOLVER_RESULT_TYPE, returnType.toString());
resolverSegment.addAttribute(AttributeName.RESOLVER_FIELD_PATH, formattedPath);
resolverSegment.addAttribute(AttributeName.RESOLVER_TYPE_NAME, parentType.toString());
resolverSegment.addAttribute(AttributeName.RESOLVER_RESULT_TYPE, returnType.toString());

if (options.includeResolverArgs) {
const rawArgs = resolversArgs || {};
const resolverArgsToTrack = options.isResolverArgsRegex
? filterPropertiesByRegex(rawArgs, options.includeResolverArgs as RegExp)
: rawArgs;
if (options.includeResolverArgs) {
const rawArgs = resolversArgs || {};
const resolverArgsToTrack = options.isResolverArgsRegex
? filterPropertiesByRegex(rawArgs, options.includeResolverArgs as RegExp)
: rawArgs;

resolverSegment.addAttribute(AttributeName.RESOLVER_ARGS, JSON.stringify(resolverArgsToTrack));
}
resolverSegment.addAttribute(AttributeName.RESOLVER_ARGS, JSON.stringify(resolverArgsToTrack));
}

return ({ result }) => {
if (options.includeRawResult) {
resolverSegment.addAttribute(AttributeName.RESOLVER_RESULT, JSON.stringify(result));
}
return ({ result }) => {
if (options.includeRawResult) {
resolverSegment.addAttribute(AttributeName.RESOLVER_RESULT, JSON.stringify(result));
}

resolverSegment.end();
};
}
resolverSegment.end();
};
}
: undefined;

return {
Expand Down