@Override public void onPreparingLog(@NonNull Log log, @NonNull String groupName) { /* * Since we enqueue start session logs, skip them to avoid infinite loop. * Also skip start service log as it's always sent and should not trigger a session. */ if (log instanceof StartSessionLog || log instanceof StartServiceLog) { return; } /* * If the log has already specified a timestamp, try correlating with a past session. * Note that it can also find the current session but that's ok: in that case that means * its a log that will be associated to current session but won't trigger expiration logic. */ Date timestamp = log.getTimestamp(); if (timestamp != null) { SessionContext.SessionInfo pastSession = SessionContext.getInstance().getSessionAt(timestamp.getTime()); if (pastSession != null) { log.setSid(pastSession.getSessionId()); } } /* If the log does not have a timestamp yet, then we just correlate with current session. */ else { /* Set current session identifier. */ log.setSid(mSid); /* Record queued time only if the log is using current session. */ mLastQueuedLogTime = SystemClock.elapsedRealtime(); } }
if (log.getDevice() == null) { log.setDevice(mDevice); if (log.getTimestamp() == null) { log.setTimestamp(new Date()); AppCenterLog.debug(LOG_TAG, "Log of type '" + log.getType() + "' was filtered out by listener(s)"); } else { if (mAppSecret == null && groupState.mIngestion == mIngestion) { AppCenterLog.debug(LOG_TAG, "Log of type '" + log.getType() + "' was not filtered out by listener(s) but no app secret was provided. Not persisting/sending the log."); return; Iterator<String> targetKeys = log.getTransmissionTargetTokens().iterator(); String targetKey = targetKeys.hasNext() ? PartAUtils.getTargetKey(targetKeys.next()) : null; if (groupState.mPausedTargetKeys.contains(targetKey)) {
@Override public Collection<CommonSchemaLog> toCommonSchemaLogs(Log log) { Collection<CommonSchemaLog> commonSchemaLogs = new LinkedList<>(); for (String transmissionTarget : log.getTransmissionTargetTokens()) { /* Part A common fields. */ CommonSchemaEventLog commonSchemaEventLog = new CommonSchemaEventLog(); /* Event name goes to Part A. */ EventLog eventLog = (EventLog) log; PartAUtils.setName(commonSchemaEventLog, eventLog.getName()); /* Add common Part A fields. */ PartAUtils.addPartAFromLog(log, commonSchemaEventLog, transmissionTarget); /* Part B, C and Part A metadata. */ CommonSchemaDataUtils.addCommonSchemaData(eventLog.getTypedProperties(), commonSchemaEventLog); commonSchemaLogs.add(commonSchemaEventLog); /* Copy tag. */ commonSchemaEventLog.setTag(log.getTag()); } return commonSchemaLogs; } }
@Test public void invalidGroup() throws Persistence.PersistenceException { Persistence persistence = mock(Persistence.class); Channel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, mock(Ingestion.class), mAppCenterHandler); /* Enqueue a log before group is registered = failure. */ Log log = mock(Log.class); channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS); verify(log, never()).setDevice(any(Device.class)); verify(log, never()).setTimestamp(any(Date.class)); verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt()); /* Trying remove group that not registered. */ channel.removeGroup(TEST_GROUP); verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class)); }
Log log = mock(Log.class); channel.enqueue(log, "", DEFAULTS); verify(log).setDevice(device); verify(log).setTimestamp(any(Date.class)); when(log2.getDevice()).thenReturn(device); when(log2.getTimestamp()).thenReturn(new Date(123L)); channel.enqueue(log2, "", DEFAULTS); verify(log2, never()).setDevice(any(Device.class)); verify(log2, never()).setTimestamp(any(Date.class)); Log log3 = mock(Log.class); channel.enqueue(log3, "", DEFAULTS); verify(log3).setDevice(device2); verify(log3).setTimestamp(any(Date.class));
@Test public void ignoreStartService() { Log startServiceLog = spy(new StartServiceLog()); mSessionTracker.onPreparingLog(startServiceLog, TEST_GROUP); verify(mChannel, never()).enqueue(any(Log.class), anyString(), anyInt()); verify(startServiceLog, never()).setSid(any(UUID.class)); }
Device device = src.getDevice(); dest.setTimestamp(src.getTimestamp()); dest.getExt().getUser().setLocalId(UserIdContext.getPrefixedUserId(src.getUserId())); dest.getExt().getUser().setLocale(device.getLocale().replace("_", "-"));
Log log = newEvent(); mSessionTracker.onPreparingLog(log, TEST_GROUP); assertNotNull(log.getSid()); SessionContext.SessionInfo session = SessionContext.getInstance().getSessionAt(firstSessionTime); assertNotNull(session); assertEquals(session.getSessionId(), log.getSid()); assertEquals(appLaunchTime, session.getAppLaunchTimestamp()); mSessionTracker = new SessionTracker(mChannel, TEST_GROUP); Log log2 = newEvent(); log2.setTimestamp(new Date(mMockTime)); mSessionTracker.onPreparingLog(log2, TEST_GROUP); assertNotEquals(log.getSid(), log2.getSid()); assertNull(log2.getSid());
/** * Checks if the log is compatible with One Collector. * * @param log The log. * @return true if the log is compatible with One Collector, false otherwise. */ private static boolean isOneCollectorCompatible(@NonNull Log log) { return !(log instanceof CommonSchemaLog) && !log.getTransmissionTargetTokens().isEmpty(); }
AppCenterLog.debug(LOG_TAG, "Storing a log to the Persistence database for log type " + log.getType() + " with flags=" + flags); String payload = getLogSerializer().serializeLog(log); ContentValues contentValues; throw new PersistenceException("Log is larger than " + PAYLOAD_MAX_SIZE + " bytes, cannot send to OneCollector."); targetToken = log.getTransmissionTargetTokens().iterator().next(); targetKey = PartAUtils.getTargetKey(targetToken); targetToken = CryptoUtils.getInstance(mContext).encrypt(targetToken); "Current maximum database size is " + maxSize + " bytes."); contentValues = getContentValues(group, isLargePayload ? null : payload, targetToken, log.getType(), targetKey, Flags.getPersistenceFlag(flags, false)); long databaseId = mDatabaseManager.put(contentValues, COLUMN_PRIORITY); if (databaseId == -1) { throw new PersistenceException("Failed to store a log to the Persistence database for log type " + log.getType() + "."); AppCenterLog.debug(LOG_TAG, "Stored a log to the Persistence database for log type " + log.getType() + " with databaseId=" + databaseId); if (isLargePayload) { AppCenterLog.debug(LOG_TAG, "Payload is larger than what SQLite supports, storing payload in a separate file.");
if (targetToken != null) { CryptoUtils.DecryptedData data = CryptoUtils.getInstance(mContext).decrypt(targetToken, false); log.addTransmissionTarget(data.getDecryptedData()); AppCenterLog.debug(LOG_TAG, "\t" + entry.getValue().getSid() + " / " + dbIdentifier);
@Override public Collection<CommonSchemaLog> toCommonSchemaLog(@NonNull Log log) { return mLogFactories.get(log.getType()).toCommonSchemaLogs(log); }
mSessionTracker.onPreparingLog(log, TEST_GROUP); mSessionTracker.onPreparingLog(expectedStartSessionLog, TEST_GROUP); assertNotNull(log.getSid()); expectedSid = log.getSid(); expectedStartSessionLog.setSid(expectedSid); verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP, DEFAULTS); mSessionTracker.onPreparingLog(log, TEST_GROUP); mSessionTracker.onPreparingLog(expectedStartSessionLog, TEST_GROUP); assertNotEquals(expectedSid, log.getSid()); expectedSid = log.getSid(); expectedStartSessionLog.setSid(expectedSid); verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP, DEFAULTS);
@Test public void checkPartAConversionDoesNotInstantiateExtensionObjectAgain() { Log log = mock(Log.class); when(log.getDevice()).thenReturn(getDevice(0)); MockCommonSchemaLog commonSchemaLog = new MockCommonSchemaLog(); Extensions extensions = new Extensions(); commonSchemaLog.setExt(extensions); PartAUtils.addPartAFromLog(log, commonSchemaLog, "T1UUID1-T2UUID2"); assertEquals(extensions, commonSchemaLog.getExt()); }
@Test public void logWithUserId() throws JSONException { MockLog expectedLog = AndroidTestUtils.generateMockLog(); expectedLog.setTimestamp(new Date()); expectedLog.setUserId("charlie"); /* Verify serialize and deserialize. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); String payload = serializer.serializeLog(expectedLog); Log actualLog = serializer.deserializeLog(payload, null); assertEquals(expectedLog, actualLog); assertEquals("charlie", actualLog.getUserId()); }
String transmissionTarget = "T1UUID1-T2UUID2"; Log log = mock(Log.class); when(log.getDevice()).thenReturn(device); when(log.getTimestamp()).thenReturn(timestamp); when(log.getUserId()).thenReturn("alice");
assertNotNull(log.getSid()); currentSid = firstSid = log.getSid(); assertEquals(currentSid, log.getSid()); log.setTimestamp(new Date(123L)); mSessionTracker.onPreparingLog(log, TEST_GROUP); assertNull(log.getSid()); Log log = newEvent(); mSessionTracker.onPreparingLog(log, TEST_GROUP); assertNotEquals(currentSid, log.getSid()); Set<String> sessions = SharedPreferencesManager.getStringSet("sessions"); assertNotNull(sessions); log.setTimestamp(new Date(firstSessionTime + 1)); mSessionTracker.onPreparingLog(log, TEST_GROUP); assertEquals(firstSid, log.getSid()); Set<String> sessions = SharedPreferencesManager.getStringSet("sessions"); assertNotNull(sessions); log.setTimestamp(new Date(firstSessionTime + 1)); mSessionTracker.onPreparingLog(log, TEST_GROUP); assertEquals(firstSid, log.getSid()); Set<String> sessions = SharedPreferencesManager.getStringSet("sessions"); assertNotNull(sessions); log.setTimestamp(new Date(1)); mSessionTracker.onPreparingLog(log, TEST_GROUP);
Set<String> apiKeys = new LinkedHashSet<>(); for (Log log : logContainer.getLogs()) { apiKeys.addAll(log.getTransmissionTargetTokens());