Skip to content

Commit

Permalink
load testing
Browse files Browse the repository at this point in the history
  • Loading branch information
TBonnin committed Feb 9, 2024
1 parent 6055a95 commit ff49fa8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
17 changes: 15 additions & 2 deletions packages/server/lib/controllers/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
slackNotificationService
} from '@nangohq/shared';
import { getUserAccountAndEnvironmentFromSession } from '../utils/utils.js';
import tracer from '../apm.js';

class ConnectionController {
/**
Expand Down Expand Up @@ -235,6 +236,7 @@ class ConnectionController {
*/

async getConnectionCreds(req: Request, res: Response, next: NextFunction) {
const span = tracer.startSpan('getConnectionCreds');
try {
const environmentId = getEnvironmentId(res);
const accountId = getAccount(res);
Expand All @@ -249,11 +251,20 @@ class ConnectionController {
success,
error,
response: connection
} = await connectionService.getConnectionCredentials(accountId, environmentId, connectionId, providerConfigKey, null, action, instantRefresh);
} = await connectionService.getConnectionCredentials(
accountId,
environmentId,
connectionId,
providerConfigKey,
null,
action,
instantRefresh,
tracer
);

if (!success) {
errorManager.errResFromNangoErr(res, error);

span.setTag('error', error);
return;
}

Expand All @@ -270,7 +281,9 @@ class ConnectionController {
}

res.status(200).send(connection);
span.finish();
} catch (err) {
span.finish();
next(err);
}
}
Expand Down
36 changes: 33 additions & 3 deletions packages/shared/lib/services/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { Locking } from '../utils/lock/locking.js';
import { InMemoryKVStore } from '../utils/kvstore/InMemoryStore.js';
import { RedisKVStore } from '../utils/kvstore/RedisStore.js';
import type { KVStore } from '../utils/kvstore/KVStore.js';
import type { Tracer } from 'dd-trace';

type KeyValuePairs = Record<string, string | boolean>;

Expand Down Expand Up @@ -305,7 +306,12 @@ class ConnectionService {
return result[0];
}

public async getConnection(connectionId: string, providerConfigKey: string, environment_id: number): Promise<ServiceResponse<Connection>> {
public async getConnection(
connectionId: string,
providerConfigKey: string,
environment_id: number,
tracer: Tracer | null = null
): Promise<ServiceResponse<Connection>> {
if (!environment_id) {
const error = new NangoError('missing_environment');

Expand Down Expand Up @@ -336,6 +342,7 @@ class ConnectionService {
return { success: false, error, response: null };
}

const qSpan = tracer?.startSpan('queryConnection');
const result: StoredConnection[] | null = (await schema()
.select('*')
.from<StoredConnection>(`_nango_connections`)
Expand All @@ -354,10 +361,15 @@ class ConnectionService {
providerConfigKey
});

qSpan?.setTag('error', error);
qSpan?.finish();
return { success: false, error, response: null };
}
qSpan?.finish();

const dSpan = tracer?.startSpan('decryptConnection');
const connection = encryptionManager.decryptConnection(storedConnection);
dSpan?.finish();

// Parse the token expiration date.
if (connection != null) {
Expand Down Expand Up @@ -544,23 +556,29 @@ class ConnectionService {
providerConfigKey: string,
activityLogId?: number | null,
action?: LogAction,
instantRefresh = false
instantRefresh = false,
tracer: Tracer | null = null
): Promise<ServiceResponse<Connection>> {
const span = tracer?.startSpan('getConnectionCredentials');
if (connectionId === null) {
const error = new NangoError('missing_connection');

span?.setTag('error', error);
return { success: false, error, response: null };
}

if (providerConfigKey === null) {
const error = new NangoError('missing_provider_config');

span?.setTag('error', error);
return { success: false, error, response: null };
}

const { success, error, response: connection } = await this.getConnection(connectionId, providerConfigKey, environmentId);
const spanA = tracer?.startSpan('getConnection');
const { success, error, response: connection } = await this.getConnection(connectionId, providerConfigKey, environmentId, tracer);

if (!success) {
span?.setTag('error', error);
return { success, error, response: null };
}

Expand All @@ -577,9 +595,13 @@ class ConnectionService {
const environmentName = await environmentService.getEnvironmentName(environmentId);
const error = new NangoError('unknown_connection', { connectionId, providerConfigKey, environmentName });

span?.setTag('error', error);
spanA?.setTag('error', error);
return { success: false, error, response: null };
}
spanA?.finish();

const spanB = tracer?.startSpan('getProviderConfig');
const config: ProviderConfig | null = await configService.getProviderConfig(connection?.provider_config_key as string, environmentId);

if (activityLogId) {
Expand All @@ -598,12 +620,16 @@ class ConnectionService {
}

const error = new NangoError('unknown_provider_config');
span?.setTag('error', error);
spanB?.setTag('error', error);
return { success: false, error, response: null };
}
spanB?.finish();

const template: ProviderTemplate | undefined = configService.getTemplate(config?.provider as string);

if (connection?.credentials?.type === ProviderAuthModes.OAuth2 || connection?.credentials?.type === ProviderAuthModes.App) {
const spanC = tracer?.startSpan('refreshCredentialsIfNeeded');
const {
success,
error,
Expand All @@ -619,12 +645,16 @@ class ConnectionService {
);

if (!success) {
span?.setTag('error', error);
spanC?.setTag('error', error);
return { success, error, response: null };
}
spanC?.finish();

connection.credentials = credentials as OAuth2Credentials;
}

span?.finish();
return { success: true, error: null, response: connection };
}

Expand Down
19 changes: 8 additions & 11 deletions packages/shared/lib/utils/error.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,14 @@ class ErrorManager {
}

if (config.metadata) {
const metadata = Object.entries(config.metadata).reduce(
(acc, [key, value]) => {
if (typeof value === 'object') {
acc[key] = JSON.stringify(value);
} else {
acc[key] = value;
}
return acc;
},
{} as Record<string, unknown>
);
const metadata = Object.entries(config.metadata).reduce((acc, [key, value]) => {
if (typeof value === 'object') {
acc[key] = JSON.stringify(value);
} else {
acc[key] = value;
}
return acc;
}, {} as Record<string, unknown>);
scope.setContext('metadata', metadata);
}

Expand Down

0 comments on commit ff49fa8

Please sign in to comment.