newrelic-experimental / newrelic-nextjs-integration

NewRelic Browser and APM agent NextJs integration (front and back-end respectively)
Apache License 2.0
27 stars 22 forks source link

Cannot build nextjs when integrate new relic on CI/CD #16

Closed hebertfsilva closed 3 days ago

hebertfsilva commented 1 year ago

I have problem after integrate new relic into nextjs application. I followed instruction from blog post https://newrelic.com/blog/how-to-relic/nextjs-monitor-application-data.

Currently, I run build nextjs app on CI/CD then cannot build with error detail:

Restarted static page generation for <page url> because it took more than 60 seconds

I think the error issued when nextjs generate static site in _document component

/ eslint-disable react/react-in-jsx-scope /

import Document, {

Html,

Main,

NextScript,

DocumentContext,

Head,

DocumentInitialProps,

} from 'next/document';

import newrelic from 'newrelic';

type NewRelicProps = {

browserTimingHeader: string;

};

export default class MyDocument extends Document {

static async getInitialProps(

ctx: DocumentContext,

): Promise<DocumentInitialProps & NewRelicProps> {

const sheet = new ServerStyleSheet();

const originalRenderPage = ctx.renderPage;

try {

ctx.renderPage = () =>

originalRenderPage({

enhanceApp: (App) => (props) =>

sheet.collectStyles(<App {...props} />),

});

const initialProps = await Document.getInitialProps(ctx);

/**

*/

console.log('isconnected: ', newrelic.agent.collector.isConnected());

if (!newrelic.agent.collector.isConnected()) {

await new Promise((resolve) => {

newrelic.agent.on('connected', resolve);

});

}

const browserTimingHeader = newrelic.getBrowserTimingHeader({

hasToRemoveScriptWrapper: true,

allowTransactionlessInjection: true,

});

return {

...initialProps,

browserTimingHeader,

styles: (

<>

{initialProps.styles}

{sheet.getStyleElement()}

</>

),

};

} catch (error) {

console.log('Debug build: ', error);

} finally {

sheet.seal();

}

}

render() {

return (

);

}

}

This is my docuker file

base node image

FROM <nodejs docker image url> as base RUN apk update

Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.

RUN apk add --no-cache libc6-compat WORKDIR /app ENV YARN_CACHE_FOLDER=.yarn-cache

FROM base as builder ARG WEB_SERVICE=main ARG ENVIRONMENT=dev RUN yarn global add turbo COPY . . RUN turbo prune --scope=${WEB_SERVICE} --docker

FROM base as installer ARG WEB_SERVICE=main ARG ENVIRONMENT=dev ARG NEW_RELIC_LICENSE_KEY_ARG

ENV NEW_RELIC_LICENSE_KEY=$NEW_RELIC_LICENSE_KEY_ARG

COPY --from=builder /app/out/json/ . COPY --from=builder /app/out/yarn.lock ./yarn.lock COPY --from=builder /app/out/full/ . COPY .gitignore .gitignore COPY turbo.json turbo.json COPY .env. ./ RUN mv .env.${ENVIRONMENT} .env && rm -rf .env. RUN yarn install RUN yarn build CMD ["yarn", "start"]

Any one have any solution, please help me! thanks

matewilk commented 1 year ago

Have you checked your New Relic config? This might be related to https://github.com/newrelic-experimental/newrelic-nextjs-integration/issues/14

Have you checked your logs when running the container? Can you post them here please?