Skip to content

Commit

Permalink
Add build information to logs
Browse files Browse the repository at this point in the history
and parameterize env var names
  • Loading branch information
hannah-macdonald1 committed Nov 6, 2024
1 parent 5d332c5 commit be016b3
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 15 deletions.
23 changes: 21 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import configuration from './configuration/configuration';
Expand All @@ -15,7 +15,26 @@ import { ExternalApiModule } from './external-api/external-api.module';
ConfigModule.forRoot({
load: [configuration],
}),
LoggerModule.forRoot(),
LoggerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
pinoHttp: {
customSuccessObject: (req, res, loggableObject) => {
return {
...loggableObject,
buildNumber: configService.get('buildInfo.buildNumber'),
};
},
customErrorObject: (req, res, loggableObject) => {
return {
...loggableObject,
buildNumber: configService.get('buildInfo.buildNumber'),
};
},
},
}),
}),
CacheModule.register({ isGlobal: true }),
CommonModule,
ControllersModule,
Expand Down
9 changes: 9 additions & 0 deletions src/common/constants/upstream-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const baseUrlEnvVarName = 'UPSTREAM_BASE_URL';
const supportNetworkEndpointEnvVarName = 'SUPPORT_NETWORK_ENDPOINT';
const inPersonVisitsEndpointEnvVarName = 'IN_PERSON_VISITS_ENDPOINT';

export {
baseUrlEnvVarName,
supportNetworkEndpointEnvVarName,
inPersonVisitsEndpointEnvVarName,
};
16 changes: 11 additions & 5 deletions src/common/guards/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
import { firstValueFrom } from 'rxjs';
import { AxiosError } from 'axios';
import { TokenRefresherService } from '../../../external-api/token-refresher/token-refresher.service';
import { baseUrlEnvVarName } from '../../../common/constants/upstream-constants';

@Injectable()
export class AuthService {
cacheTime: number;
baseUrl: string;
buildNumber: string;
private readonly logger = new Logger(AuthService.name);

constructor(
Expand All @@ -30,7 +32,8 @@ export class AuthService {
private readonly tokenRefresherService: TokenRefresherService,
) {
this.cacheTime = this.configService.get<number>('recordCache.cacheTtlMs');
this.baseUrl = this.configService.get<string>('UPSTREAM_BASE_URL');
this.baseUrl = this.configService.get<string>(baseUrlEnvVarName);
this.buildNumber = this.configService.get<string>('buildInfo.buildNumber');
}

async getRecordAndValidate(req: Request): Promise<boolean> {
Expand All @@ -45,8 +48,6 @@ export class AuthService {
const key = `${id}|${recordType}`;
let upstreamResult: string | null | undefined =
await this.cacheManager.get(key);
// TODO: Remove this console log once guard is verified working
this.logger.log(`Cache result: ${upstreamResult}`);

if (upstreamResult === undefined) {
upstreamResult = await this.getAssignedIdirUpstream(id, recordType);
Expand Down Expand Up @@ -114,9 +115,14 @@ export class AuthService {
return idir;
} catch (error) {
if (error instanceof AxiosError) {
this.logger.error(error.message, error.stack, error.cause);
this.logger.error({
msg: error.message,
stack: error.stack,
cause: error.cause,
buildNumber: this.buildNumber,
});
} else {
this.logger.error(error);
this.logger.error({ error, buildNumber: this.buildNumber });
}
}
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ export default () => ({
inPersonVisits: undefined,
},
skipAuthGuard: process.env.SKIP_AUTH_GUARD === 'true',
buildInfo: {
buildNumber:
process.env.VPI_APP_LABEL === undefined
? 'localBuild'
: process.env.VPI_APP_LABEL,
},
});
16 changes: 13 additions & 3 deletions src/external-api/request-preparer/request-preparer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import { TokenRefresherService } from '../token-refresher/token-refresher.servic
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { AxiosError } from 'axios';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class RequestPreparerService {
buildNumber: string;
private readonly logger = new Logger(RequestPreparerService.name);
constructor(
private readonly utilitiesService: UtilitiesService,
private readonly tokenRefresherService: TokenRefresherService,
private readonly httpService: HttpService,
) {}
private readonly configService: ConfigService,
) {
this.buildNumber = this.configService.get<string>('buildInfo.buildNumber');
}

prepareHeadersAndParams(
baseSearchSpec: string,
Expand Down Expand Up @@ -71,12 +76,17 @@ export class RequestPreparerService {
);
} catch (error) {
if (error instanceof AxiosError) {
this.logger.error(error.message, error.stack, error.cause);
this.logger.error({
msg: error.message,
stack: error.stack,
cause: error.cause,
buildNumber: this.buildNumber,
});
if (error.status === 404) {
throw new HttpException({}, HttpStatus.NO_CONTENT, { cause: error });
}
} else {
this.logger.error(error);
this.logger.error({ error, buildNumber: this.buildNumber });
}
throw new HttpException(
{
Expand Down
9 changes: 8 additions & 1 deletion src/external-api/token-refresher/token-refresher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class TokenRefresherService {
accessTokenUrl: string;
clientId: string;
clientSecret: string;
buildNumber: string;
private readonly logger = new Logger(TokenRefresherService.name);

constructor(
Expand All @@ -26,6 +27,7 @@ export class TokenRefresherService {
this.accessTokenUrl = this.configService.get('oauth.accessTokenUrl');
this.clientId = this.configService.get('oauth.clientId');
this.clientSecret = this.configService.get('oauth.clientSecret');
this.buildNumber = this.configService.get('buildInfo.buildNumber');
}

async refreshUpstreamBearerToken(): Promise<string | undefined> {
Expand Down Expand Up @@ -78,7 +80,12 @@ export class TokenRefresherService {
return [bearer_token, expiryMs];
} catch (error) {
if (error instanceof AxiosError) {
this.logger.error(error.message, error.stack, error.cause);
this.logger.error({
msg: error.message,
stack: error.stack,
cause: error.cause,
buildNumber: this.buildNumber,
});
}
return [undefined, undefined];
}
Expand Down
8 changes: 6 additions & 2 deletions src/helpers/in-person-visits/in-person-visits.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
NestedInPersonVisitsEntity,
InPersonVisitsEntity,
} from '../../entities/in-person-visits.entity';
import {
baseUrlEnvVarName,
inPersonVisitsEndpointEnvVarName,
} from '../../common/constants/upstream-constants';

@Injectable()
export class InPersonVisitsService {
Expand All @@ -19,8 +23,8 @@ export class InPersonVisitsService {
private readonly requestPreparerService: RequestPreparerService,
) {
this.url = (
this.configService.get<string>('UPSTREAM_BASE_URL') +
this.configService.get<string>('IN_PERSON_VISITS_ENDPOINT')
this.configService.get<string>(baseUrlEnvVarName) +
this.configService.get<string>(inPersonVisitsEndpointEnvVarName)
).replace(/\s/g, '%20');
this.workspace = this.configService.get('workspaces.inPersonVisits');
this.sinceFieldName = this.configService.get(
Expand Down
8 changes: 6 additions & 2 deletions src/helpers/support-network/support-network.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
import { IdPathParams } from '../../dto/id-path-params.dto';
import { SinceQueryParams } from '../../dto/since-query-params.dto';
import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service';
import {
baseUrlEnvVarName,
supportNetworkEndpointEnvVarName,
} from '../../common/constants/upstream-constants';

@Injectable()
export class SupportNetworkService {
Expand All @@ -22,8 +26,8 @@ export class SupportNetworkService {
private readonly requestPreparerService: RequestPreparerService,
) {
this.url = (
this.configService.get<string>('UPSTREAM_BASE_URL') +
this.configService.get<string>('SUPPORT_NETWORK_ENDPOINT')
this.configService.get<string>(baseUrlEnvVarName) +
this.configService.get<string>(supportNetworkEndpointEnvVarName)
).replace(/\s/g, '%20');
this.workspace = this.configService.get('workspaces.supportNetwork');
this.sinceFieldName = this.configService.get(
Expand Down

0 comments on commit be016b3

Please sign in to comment.