@@ -18,12 +18,20 @@ import { LoggerFacade } from "../../../logging/logger";
1818import { IOptimizelyUserContext } from "../../../optimizely_user_context" ;
1919import { ProjectConfig } from "../../../project_config/project_config"
2020import { OptimizelyDecideOption , UserAttributes } from "../../../shared_types"
21- import { Cache , CacheWithRemove } from "../../../utils/cache/cache" ;
21+ import { CacheWithRemove } from "../../../utils/cache/cache" ;
2222import { CmabClient } from "./cmab_client" ;
2323import { v4 as uuidV4 } from 'uuid' ;
2424import murmurhash from "murmurhash" ;
2525import { DecideOptionsMap } from ".." ;
2626import { SerialRunner } from "../../../utils/executor/serial_runner" ;
27+ import {
28+ CMAB_CACHE_ATTRIBUTES_MISMATCH ,
29+ CMAB_CACHE_HIT ,
30+ CMAB_CACHE_MISS ,
31+ IGNORE_CMAB_CACHE ,
32+ INVALIDATE_CMAB_CACHE ,
33+ RESET_CMAB_CACHE ,
34+ } from 'log_message' ;
2735
2836export type CmabDecision = {
2937 variationId : string ,
@@ -59,6 +67,7 @@ export type CmabServiceOptions = {
5967}
6068
6169const SERIALIZER_BUCKETS = 1000 ;
70+ const LOGGER_NAME = 'CmabService' ;
6271
6372export class DefaultCmabService implements CmabService {
6473 private cmabCache : CacheWithRemove < CmabCacheValue > ;
@@ -72,6 +81,7 @@ export class DefaultCmabService implements CmabService {
7281 this . cmabCache = options . cmabCache ;
7382 this . cmabClient = options . cmabClient ;
7483 this . logger = options . logger ;
84+ this . logger ?. setName ( LOGGER_NAME ) ;
7585 }
7686
7787 private getSerializerIndex ( userId : string , experimentId : string ) : number {
@@ -98,19 +108,23 @@ export class DefaultCmabService implements CmabService {
98108 ruleId : string ,
99109 options : DecideOptionsMap ,
100110 ) : Promise < CmabDecision > {
111+ const userId = userContext . getUserId ( ) ;
101112 const filteredAttributes = this . filterAttributes ( projectConfig , userContext , ruleId ) ;
102113
103114 if ( options [ OptimizelyDecideOption . IGNORE_CMAB_CACHE ] ) {
104- return this . fetchDecision ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
115+ this . logger ?. debug ( IGNORE_CMAB_CACHE , userId , ruleId ) ;
116+ return this . fetchDecision ( ruleId , userId , filteredAttributes ) ;
105117 }
106118
107119 if ( options [ OptimizelyDecideOption . RESET_CMAB_CACHE ] ) {
120+ this . logger ?. debug ( RESET_CMAB_CACHE , userId , ruleId ) ;
108121 this . cmabCache . reset ( ) ;
109122 }
110123
111- const cacheKey = this . getCacheKey ( userContext . getUserId ( ) , ruleId ) ;
124+ const cacheKey = this . getCacheKey ( userId , ruleId ) ;
112125
113126 if ( options [ OptimizelyDecideOption . INVALIDATE_USER_CMAB_CACHE ] ) {
127+ this . logger ?. debug ( INVALIDATE_CMAB_CACHE , userId , ruleId ) ;
114128 this . cmabCache . remove ( cacheKey ) ;
115129 }
116130
@@ -121,13 +135,17 @@ export class DefaultCmabService implements CmabService {
121135
122136 if ( cachedValue ) {
123137 if ( cachedValue . attributesHash === attributesHash ) {
138+ this . logger ?. debug ( CMAB_CACHE_HIT , userId , ruleId ) ;
124139 return { variationId : cachedValue . variationId , cmabUuid : cachedValue . cmabUuid } ;
125140 } else {
141+ this . logger ?. debug ( CMAB_CACHE_ATTRIBUTES_MISMATCH , userId , ruleId ) ;
126142 this . cmabCache . remove ( cacheKey ) ;
127143 }
144+ } else {
145+ this . logger ?. debug ( CMAB_CACHE_MISS , userId , ruleId ) ;
128146 }
129147
130- const variation = await this . fetchDecision ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
148+ const variation = await this . fetchDecision ( ruleId , userId , filteredAttributes ) ;
131149 this . cmabCache . save ( cacheKey , {
132150 attributesHash,
133151 variationId : variation . variationId ,
0 commit comments