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

🔊 Upgrade felles logging #141

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions auth/beskyttetApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { isMock } from '../utils/environments';
import { verifyIdportenAccessToken } from './verifyIdPortenAccessToken';
import { logger } from '@navikt/aap-felles-utils';
import { ErrorMedStatus } from './ErrorMedStatus';
import { logError, logInfo, logWarning } from '@navikt/aap-felles-utils';

type ApiHandler = (req: NextApiRequest, res: NextApiResponse) => void | Promise<void>;

Expand All @@ -17,27 +17,27 @@ export function beskyttetApi(handler: ApiHandler): ApiHandler {

try {
if (isMock()) {
logger.warn('handling request for mocked environment, should not happen in production');
logWarning('handling request for mocked environment, should not happen in production');
return handler(req, res);
}

const bearerToken: string | null | undefined = req.headers['authorization'];
if (!bearerToken) {
logger.warn({ message: 'ingen bearer token', path: req?.url });
logWarning(`ingen bearer token, path: ${req?.url}`);
return send401();
}
try {
await verifyIdportenAccessToken(bearerToken);
} catch (e) {
logger.error(e, 'kunne ikke validere idportentoken i beskyttetApi');
logError('kunne ikke validere idportentoken i beskyttetApi', e);
return send401();
}
return handler(req, res);
} catch (e) {
logger.error(e);
logger.info('handling error in beskyttetApi');
logError('beskyttetApi', e);
logInfo('handling error in beskyttetApi');
if (e instanceof ErrorMedStatus) {
logger.info(`sending error with status ${e.status} and message ${e.message}`);
logInfo(`sending error with status ${e.status} and message ${e.message}`);
return res.status(e.status).json({ message: e.message, navCallId: e.navCallId });
}
}
Expand Down
10 changes: 5 additions & 5 deletions auth/beskyttetSide.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextPageContext, GetServerSidePropsResult } from 'next';
import { isMock } from '../utils/environments';
import { isMock } from 'utils/environments';
import { getAccessToken } from './accessToken';
import { verifyIdportenAccessToken } from './verifyIdPortenAccessToken';
import { logger } from '@navikt/aap-felles-utils';
import { logError } from '@navikt/aap-felles-utils';

type PageHandler = (context: NextPageContext) => void | Promise<GetServerSidePropsResult<{}>>;

Expand All @@ -15,7 +15,7 @@ const wonderwallRedirect = {

export function beskyttetSide(handler: PageHandler) {
return async function withBearerTokenHandler(
context: NextPageContext
context: NextPageContext,
): Promise<ReturnType<typeof handler>> {
if (isMock()) {
return handler(context);
Expand All @@ -30,7 +30,7 @@ export function beskyttetSide(handler: PageHandler) {
try {
await verifyIdportenAccessToken(bearerToken);
} catch (e) {
logger.error(e, 'kunne ikke validere idportentoken i beskyttetSide');
logError('kunne ikke validere idportentoken i beskyttetSide', e);
return wonderwallRedirect;
}
return handler(context);
Expand All @@ -42,5 +42,5 @@ export const beskyttetSideUtenProps = beskyttetSide(
return {
props: {},
};
}
},
);
8 changes: 4 additions & 4 deletions lib/utils/TokenxProxy.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { proxyApiRouteRequest } from '@navikt/next-api-proxy';
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest, getTokenX, logger } from '@navikt/aap-felles-utils';
import { getAccessTokenFromRequest, getTokenX, logError, logInfo } from '@navikt/aap-felles-utils';
import metrics from '../../utils/metrics';

export const tokenXProxy = async (
req: NextApiRequest,
res: NextApiResponse,
path: string,
prometheusPath: string
prometheusPath: string,
) => {
const accessToken = getAccessTokenFromRequest(req)?.substring('Bearer '.length)!;
let tokenxToken;
try {
tokenxToken = await getTokenX(accessToken, process.env.SOKNAD_API_AUDIENCE!);
} catch (err: any) {
logger.error({ msg: 'getTokenXError', error: err });
logError('getTokenXError', err);
}
const stopTimer = metrics.backendApiDurationHistogram.startTimer({ path: prometheusPath });
const result = await proxyApiRouteRequest({
Expand All @@ -26,7 +26,7 @@ export const tokenXProxy = async (
https: false,
});

logger.info(`res from tokenXProxy: ${JSON.stringify(res.status)}`);
logInfo(`res from tokenXProxy: ${res.status}`);
stopTimer();

return result;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"dependencies": {
"@amplitude/analytics-browser": "^2.4.0",
"@grafana/faro-web-sdk": "^1.3.7",
"@navikt/aap-felles-css": "^0.0.96",
"@navikt/aap-felles-react": "^0.0.96",
"@navikt/aap-felles-utils": "^0.0.96",
"@navikt/aap-felles-utils-client": "^0.0.96",
"@navikt/aap-felles-css": "^0.0.97",
"@navikt/aap-felles-react": "^0.0.97",
"@navikt/aap-felles-utils": "^0.0.97",
"@navikt/aap-felles-utils-client": "^0.0.97",
"@navikt/ds-css": "^5.17.4",
"@navikt/ds-icons": "^3.4.3",
"@navikt/ds-react": "^5.17.4",
Expand Down
18 changes: 9 additions & 9 deletions pages/[step].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { scrollRefIntoView } from 'utils/dom';
import { Steg0 } from 'components/pageComponents/standard/Steg0/Steg0';
import * as classes from './step.module.css';
import { FormattedMessage, useIntl } from 'react-intl';
import { logger } from '@navikt/aap-felles-utils';
import { SoknadContextProvider, SoknadContextState } from 'context/soknadcontext/soknadContext';
import { useSoknad } from 'hooks/SoknadHook';
import {
Expand All @@ -49,6 +48,7 @@ import { Barn, getBarn } from 'pages/api/oppslag/barn';
import { formatNavn } from 'utils/StringFormatters';
import { hentMellomlagring } from 'pages/api/mellomlagring/les';
import { RequiredVedlegg } from 'types/SoknadContext';
import { logError, logInfo, logWarning } from '@navikt/aap-felles-utils';

interface PageProps {
søker: SokerOppslagState;
Expand Down Expand Up @@ -235,7 +235,7 @@ export const getServerSideProps = beskyttetSide(
try {
kontaktinformasjon = await getKrr(bearerToken);
} catch (e) {
logger.error({ message: `Noe gikk galt i kallet mot oppslag/krr: ${e?.toString()}` });
logError(`Noe gikk galt i kallet mot oppslag/krr`, e);
}

let mellomlagretSøknad: SoknadContextState | undefined;
Expand All @@ -245,23 +245,23 @@ export const getServerSideProps = beskyttetSide(
await Promise.all([lesBucket('STANDARD', bearerToken), hentMellomlagring(bearerToken)]);

if (mellomlagretSøknadFraAapInnsending && mellomlagretSøknadFraSoknadApi) {
logger.error('pages/step: finner mellomlagring fra begge kilder');
logError('pages/step: finner mellomlagring fra begge kilder');
}
if (mellomlagretSøknadFraSoknadApi) {
logger.info('pages/step: velger mellomlagring fra søknad-api');
logInfo('pages/step: velger mellomlagring fra søknad-api');
mellomlagretSøknad = {
...mellomlagretSøknadFraSoknadApi,
brukerMellomLagretSøknadFraAApInnsending: false,
};
} else if (mellomlagretSøknadFraAapInnsending) {
logger.info('pages/step: velger mellomlagring fra innsending');
logInfo('pages/step: velger mellomlagring fra innsending');
mellomlagretSøknad = {
...mellomlagretSøknadFraAapInnsending,
brukerMellomLagretSøknadFraAApInnsending: true,
};
}
} catch (e) {
logger.error('Noe gikk galt i innhenting av mellomlagret søknad', e);
logError('Noe gikk galt i innhenting av mellomlagret søknad', e);
}

let barn: Barn[] = søker?.søker?.barn?.map((barn) => {
Expand All @@ -271,17 +271,17 @@ export const getServerSideProps = beskyttetSide(
try {
barn = await getBarn(bearerToken);
} catch (e) {
logger.error('Noe gikk galt i kallet mot barn fra aap-oppslag', e);
logError('Noe gikk galt i kallet mot barn fra aap-oppslag', e);
}

stopTimer();

if (mellomlagretSøknad && !mellomlagretSøknad.lagretStepList) {
logger.error('Mellomlagret søknad finnes, men mangler stepList');
logError('Mellomlagret søknad finnes, men mangler stepList');
}

if (!mellomlagretSøknad?.lagretStepList) {
logger.warn('lagretStepList mangler i mellomlagret søknad, redirecter til startsiden');
logWarning('lagretStepList mangler i mellomlagret søknad, redirecter til startsiden');
return {
redirect: {
destination: '/',
Expand Down
5 changes: 2 additions & 3 deletions pages/api/buckets/lagre.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { tokenXApiProxy, logger } from '@navikt/aap-felles-utils';
import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { lagreCache } from 'mock/mellomlagringsCache';
import { erGyldigSøknadsType, GYLDIGE_SØKNADS_TYPER, SøknadsType } from 'utils/api';
import { isFunctionalTest, isMock } from 'utils/environments';
Expand All @@ -28,7 +28,7 @@ const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) =
(e: StepType) => e.active,
)?.stepIndex;

logger.error(
logError(
`Overskriver eksisterende søknad med en tom søknad på side ${activeStepIndex ?? 'ukjent'}`,
);
}
Expand All @@ -49,7 +49,6 @@ export const lagreBucket = async (type: SøknadsType, data: string, accessToken?
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});
return;
};
Expand Down
5 changes: 2 additions & 3 deletions pages/api/buckets/les.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { logger, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { logInfo, tokenXApiProxy } from '@navikt/aap-felles-utils';
import metrics from 'utils/metrics';
import { erGyldigSøknadsType, GYLDIGE_SØKNADS_TYPER, SøknadsType } from 'utils/api';
import { isMock } from 'utils/environments';
Expand Down Expand Up @@ -40,7 +40,6 @@ export const lesBucket = async (
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});

if (!mellomlagretSøknad) {
Expand All @@ -54,7 +53,7 @@ export const lesBucket = async (
await new Promise((resolve) => setTimeout(resolve, 300));
return await lesBucket(type, accessToken, retryCount - 1);
}
logger.info('Fant ingen mellomlagret søknad hos soknad-api');
logInfo('Fant ingen mellomlagret søknad hos soknad-api');
return undefined;
}
};
Expand Down
3 changes: 1 addition & 2 deletions pages/api/buckets/slett.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { tokenXApiProxy, logger } from '@navikt/aap-felles-utils';
import { tokenXApiProxy } from '@navikt/aap-felles-utils';
import metrics from 'utils/metrics';
import { deleteCache } from 'mock/mellomlagringsCache';
import { erGyldigSøknadsType, GYLDIGE_SØKNADS_TYPER, SøknadsType } from 'utils/api';
Expand Down Expand Up @@ -34,7 +34,6 @@ export const slettBucket = async (type: SøknadsType, accessToken?: string) => {
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});
};

Expand Down
3 changes: 1 addition & 2 deletions pages/api/innsending/soknadapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomUUID } from 'crypto';
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { tokenXApiProxy, logger } from '@navikt/aap-felles-utils';
import { tokenXApiProxy } from '@navikt/aap-felles-utils';
import { isFunctionalTest, isMock } from 'utils/environments';
import { slettBucket } from '../buckets/slett';
import { ErrorMedStatus } from 'auth/ErrorMedStatus';
Expand Down Expand Up @@ -40,7 +40,6 @@ export const sendSoknad = async (data: string, accessToken?: string) => {
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});
return søknad;
};
Expand Down
5 changes: 2 additions & 3 deletions pages/api/innsending/soknadinnsending.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beskyttetApi } from 'auth/beskyttetApi';
import { logger, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { NextApiRequest, NextApiResponse } from 'next';
import metrics from 'utils/metrics';
import { ErrorMedStatus } from 'auth/ErrorMedStatus';
Expand Down Expand Up @@ -113,7 +113,7 @@ const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) =
metrics.sendSoknadCounter.inc({ type: 'STANDARD' });
res.status(201).send('Vi har mottat søknaden');
} catch (err) {
logger.error(`Noe gikk galt ved innsending av søknad: ${err?.toString()}`);
logError(`Noe gikk galt ved innsending av søknad`, err);

if (err instanceof ErrorMedStatus) {
res.status(err.status).json({ navCallId: err.navCallId });
Expand Down Expand Up @@ -164,7 +164,6 @@ export const sendSoknadViaAapInnsending = async (
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
noResponse: true,
});
return søknad;
Expand Down
5 changes: 2 additions & 3 deletions pages/api/mellomlagring/lagre.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { logger, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { lagreCache } from 'mock/mellomlagringsCache';
import { isFunctionalTest, isMock } from 'utils/environments';
import metrics from 'utils/metrics';
Expand All @@ -23,7 +23,7 @@ const handler = beskyttetApi(async (req: NextApiRequest, res: NextApiResponse) =
(e: StepType) => e.active,
)?.stepIndex;

logger.error(
logError(
`Overskriver eksisterende søknad med en tom søknad på side ${activeStepIndex ?? 'ukjent'}`,
);
}
Expand All @@ -44,7 +44,6 @@ export const lagreBucket = async (data: string, accessToken?: string) => {
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});
return;
};
Expand Down
5 changes: 2 additions & 3 deletions pages/api/mellomlagring/les.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { logger, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { logError, tokenXApiProxy } from '@navikt/aap-felles-utils';
import metrics from 'utils/metrics';
import { lesCache } from 'mock/mellomlagringsCache';
import { isFunctionalTest, isMock } from 'utils/environments';
Expand Down Expand Up @@ -39,12 +39,11 @@ export const hentMellomlagring = async (
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});

return mellomlagretSøknad;
} catch (error: any) {
logger.error('Noe gikk galt i henting av mellomlagring fra aap-innsending', error);
logError('Noe gikk galt i henting av mellomlagring fra aap-innsending', error);
return undefined;
}
};
Expand Down
3 changes: 1 addition & 2 deletions pages/api/mellomlagring/slett.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getAccessTokenFromRequest } from 'auth/accessToken';
import { beskyttetApi } from 'auth/beskyttetApi';
import { logger, tokenXApiProxy } from '@navikt/aap-felles-utils';
import { tokenXApiProxy } from '@navikt/aap-felles-utils';
import metrics from 'utils/metrics';
import { deleteCache } from 'mock/mellomlagringsCache';
import { isMock } from 'utils/environments';
Expand All @@ -27,7 +27,6 @@ export const slettBucket = async (accessToken?: string) => {
bearerToken: accessToken,
metricsStatusCodeCounter: metrics.backendApiStatusCodeCounter,
metricsTimer: metrics.backendApiDurationHistogram,
logger: logger,
});
};

Expand Down
Loading
Loading