Skip to content

Commit

Permalink
feat: add entityClassName to metrics adapter events (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman authored Oct 8, 2020
1 parent d0cd8d8 commit f4cdc01
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 32 deletions.
9 changes: 6 additions & 3 deletions packages/entity/src/EntityCompanionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export default class EntityCompanionProvider {
>
): EntityCompanion<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
const tableDataCoordinator = this.getTableDataCoordinatorForEntity(
entityCompanionDefinition.entityConfiguration
entityCompanionDefinition.entityConfiguration,
entityClass.name
);
return computeIfAbsent(this.companionMap, entityClass.name, () => {
return new EntityCompanion(
Expand All @@ -226,7 +227,8 @@ export default class EntityCompanionProvider {
}

private getTableDataCoordinatorForEntity<TFields>(
entityConfiguration: EntityConfiguration<TFields>
entityConfiguration: EntityConfiguration<TFields>,
entityClassName: string
): EntityTableDataCoordinator<TFields> {
return computeIfAbsent(this.tableDataCoordinatorMap, entityConfiguration.tableName, () => {
const entityDatabaseAdapterFlavor = this.databaseAdapterFlavors[
Expand All @@ -240,7 +242,8 @@ export default class EntityCompanionProvider {
entityDatabaseAdapterFlavor.adapterProvider,
entityCacheAdapterFlavor.cacheAdapterProvider,
entityDatabaseAdapterFlavor.queryContextProvider,
this.metricsAdapter
this.metricsAdapter,
entityClassName
);
});
}
Expand Down
9 changes: 6 additions & 3 deletions packages/entity/src/EntityMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export class CreateMutator<
async createAsync(): Promise<Result<TEntity>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.CREATE
EntityMetricsMutationType.CREATE,
this.entityClass.name
)(this.createInTransactionAsync());
}

Expand Down Expand Up @@ -329,7 +330,8 @@ export class UpdateMutator<
async updateAsync(): Promise<Result<TEntity>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.UPDATE
EntityMetricsMutationType.UPDATE,
this.entityClass.name
)(this.updateInTransactionAsync());
}

Expand Down Expand Up @@ -498,7 +500,8 @@ export class DeleteMutator<
async deleteAsync(): Promise<Result<void>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.DELETE
EntityMetricsMutationType.DELETE,
this.entityClass.name
)(this.deleteInTransactionAsync());
}

Expand Down
9 changes: 6 additions & 3 deletions packages/entity/src/__tests__/EntityLoader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
Expand Down Expand Up @@ -138,7 +139,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
Expand Down Expand Up @@ -200,7 +202,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
Expand Down
6 changes: 5 additions & 1 deletion packages/entity/src/__tests__/EntityMutator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ const createEntityMutatorFactory = (
databaseAdapter,
entityCache,
StubQueryContextProvider,
metricsAdapter
metricsAdapter,
TestEntity.name
);
const entityLoaderFactory = new EntityLoaderFactory(
testEntityConfiguration.idField,
Expand Down Expand Up @@ -930,20 +931,23 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.CREATE,
entityClassName: TestEntity.name,
})
)
).once();
verify(
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.UPDATE,
entityClassName: TestEntity.name,
})
)
).once();
verify(
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.DELETE,
entityClassName: TestEntity.name,
})
)
).once();
Expand Down
12 changes: 8 additions & 4 deletions packages/entity/src/internal/EntityDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default class EntityDataManager<TFields> {
private readonly databaseAdapter: EntityDatabaseAdapter<TFields>,
private readonly entityCache: ReadThroughEntityCache<TFields>,
private readonly queryContextProvider: EntityQueryContextProvider,
private readonly metricsAdapter: IEntityMetricsAdapter
private readonly metricsAdapter: IEntityMetricsAdapter,
private readonly entityClassName: string
) {}

private getFieldDataLoaderForFieldName<N extends keyof TFields>(
Expand Down Expand Up @@ -86,7 +87,8 @@ export default class EntityDataManager<TFields> {
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>> {
return await timeAndLogLoadMapEventAsync(
this.metricsAdapter,
EntityMetricsLoadType.LOAD_MANY
EntityMetricsLoadType.LOAD_MANY,
this.entityClassName
)(this.loadManyByFieldEqualingInternalAsync(queryContext, fieldName, fieldValues));
}

Expand Down Expand Up @@ -127,7 +129,8 @@ export default class EntityDataManager<TFields> {
): Promise<readonly Readonly<TFields>[]> {
return await timeAndLogLoadEventAsync(
this.metricsAdapter,
EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION
EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION,
this.entityClassName
)(
this.databaseAdapter.fetchManyByFieldEqualityConjunctionAsync(
queryContext,
Expand All @@ -154,7 +157,8 @@ export default class EntityDataManager<TFields> {
): Promise<readonly Readonly<TFields>[]> {
return await timeAndLogLoadEventAsync(
this.metricsAdapter,
EntityMetricsLoadType.LOAD_MANY_RAW
EntityMetricsLoadType.LOAD_MANY_RAW,
this.entityClassName
)(
this.databaseAdapter.fetchManyByRawWhereClauseAsync(
queryContext,
Expand Down
6 changes: 4 additions & 2 deletions packages/entity/src/internal/EntityTableDataCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ export default class EntityTableDataCoordinator<TFields> {
databaseAdapterProvider: IEntityDatabaseAdapterProvider,
cacheAdapterProvider: IEntityCacheAdapterProvider,
private readonly queryContextProvider: EntityQueryContextProvider,
metricsAdapter: IEntityMetricsAdapter
metricsAdapter: IEntityMetricsAdapter,
entityClassName: string
) {
this.databaseAdapter = databaseAdapterProvider.getDatabaseAdapter(entityConfiguration);
this.cacheAdapter = cacheAdapterProvider.getCacheAdapter(entityConfiguration);
this.dataManager = new EntityDataManager(
this.databaseAdapter,
new ReadThroughEntityCache(entityConfiguration, this.cacheAdapter),
queryContextProvider,
metricsAdapter
metricsAdapter,
entityClassName
);
}

Expand Down
41 changes: 28 additions & 13 deletions packages/entity/src/internal/__tests__/EntityDataManager-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import EntityDatabaseAdapter from '../../EntityDatabaseAdapter';
import IEntityMetricsAdapter, { EntityMetricsLoadType } from '../../metrics/IEntityMetricsAdapter';
import NoOpEntityMetricsAdapter from '../../metrics/NoOpEntityMetricsAdapter';
import { testEntityConfiguration, TestFields } from '../../testfixtures/TestEntity';
import TestEntity, { testEntityConfiguration, TestFields } from '../../testfixtures/TestEntity';
import {
NoCacheStubCacheAdapterProvider,
InMemoryFullCacheStubCacheAdapterProvider,
Expand Down Expand Up @@ -69,7 +69,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -116,7 +117,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -163,15 +165,17 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
// use second data manager to ensure that cache is hit instead of data loader
const entityDataManager2 = new EntityDataManager(
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);

const dbSpy = jest.spyOn(databaseAdapter, 'fetchManyWhereAsync');
Expand Down Expand Up @@ -205,7 +209,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -240,7 +245,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -283,7 +289,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -321,7 +328,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -359,7 +367,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);

const dbSpy = jest.spyOn(databaseAdapter, 'fetchManyWhereAsync');
Expand Down Expand Up @@ -396,7 +405,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand Down Expand Up @@ -441,7 +451,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand All @@ -467,7 +478,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
metricsAdapter
metricsAdapter,
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();

Expand All @@ -476,6 +488,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY,
entityClassName: TestEntity.name,
count: 1,
})
)
Expand All @@ -495,6 +508,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION,
entityClassName: TestEntity.name,
count: 1,
})
)
Expand All @@ -520,6 +534,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY_RAW,
entityClassName: TestEntity.name,
count: 0,
})
)
Expand Down
12 changes: 9 additions & 3 deletions packages/entity/src/metrics/EntityMetricsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import IEntityMetricsAdapter, {

export const timeAndLogLoadEventAsync = (
metricsAdapter: IEntityMetricsAdapter,
loadType: EntityMetricsLoadType
loadType: EntityMetricsLoadType,
entityClassName: string
) => async <TFields>(promise: Promise<readonly Readonly<TFields>[]>) => {
const startTime = Date.now();
const result = await promise;
const endTime = Date.now();

metricsAdapter.logDataManagerLoadEvent({
type: loadType,
entityClassName,
duration: endTime - startTime,
count: result.length,
});
Expand All @@ -23,7 +25,8 @@ export const timeAndLogLoadEventAsync = (

export const timeAndLogLoadMapEventAsync = (
metricsAdapter: IEntityMetricsAdapter,
loadType: EntityMetricsLoadType
loadType: EntityMetricsLoadType,
entityClassName: string
) => async <TFields, N extends keyof TFields>(
promise: Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>>
) => {
Expand All @@ -35,6 +38,7 @@ export const timeAndLogLoadMapEventAsync = (

metricsAdapter.logDataManagerLoadEvent({
type: loadType,
entityClassName,
duration: endTime - startTime,
count,
});
Expand All @@ -44,14 +48,16 @@ export const timeAndLogLoadMapEventAsync = (

export const timeAndLogMutationEventAsync = (
metricsAdapter: IEntityMetricsAdapter,
mutationType: EntityMetricsMutationType
mutationType: EntityMetricsMutationType,
entityClassName: string
) => async <T>(promise: Promise<T>) => {
const startTime = Date.now();
const result = await promise;
const endTime = Date.now();

metricsAdapter.logMutatorMutationEvent({
type: mutationType,
entityClassName,
duration: endTime - startTime,
});

Expand Down
Loading

0 comments on commit f4cdc01

Please sign in to comment.