Search in sources :

Example 1 with SdkExtension

use of com.microsoft.appcenter.ingestion.models.one.SdkExtension in project mobile-center-sdk-android by Microsoft.

the class OneCollectorChannelListener method onPreparedLog.

@Override
public void onPreparedLog(@NonNull Log log, @NonNull String groupName, int flags) {
    /* Nothing to do on common schema log prepared. */
    if (!isOneCollectorCompatible(log)) {
        return;
    }
    /* Convert logs to Common Schema. */
    Collection<CommonSchemaLog> commonSchemaLogs;
    try {
        commonSchemaLogs = mLogSerializer.toCommonSchemaLog(log);
    } catch (IllegalArgumentException e) {
        AppCenterLog.error(LOG_TAG, "Cannot send a log to one collector: " + e.getMessage());
        return;
    }
    /* Add additional part A fields that are not known by the modules during conversion. */
    for (CommonSchemaLog commonSchemaLog : commonSchemaLogs) {
        /* Add flags. */
        commonSchemaLog.setFlags((long) flags);
        /* Add SDK extension missing fields: installId, epoch and seq. libVer is already set. */
        EpochAndSeq epochAndSeq = mEpochsAndSeqsByIKey.get(commonSchemaLog.getIKey());
        if (epochAndSeq == null) {
            epochAndSeq = new EpochAndSeq(UUID.randomUUID().toString());
            mEpochsAndSeqsByIKey.put(commonSchemaLog.getIKey(), epochAndSeq);
        }
        SdkExtension sdk = commonSchemaLog.getExt().getSdk();
        sdk.setEpoch(epochAndSeq.epoch);
        sdk.setSeq(++epochAndSeq.seq);
        sdk.setInstallId(mInstallId);
    }
    /* Enqueue logs to one collector group. */
    String oneCollectorGroupName = getOneCollectorGroupName(groupName);
    for (CommonSchemaLog commonSchemaLog : commonSchemaLogs) {
        mChannel.enqueue(commonSchemaLog, oneCollectorGroupName, flags);
    }
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) SdkExtension(com.microsoft.appcenter.ingestion.models.one.SdkExtension)

Example 2 with SdkExtension

use of com.microsoft.appcenter.ingestion.models.one.SdkExtension in project mobile-center-sdk-android by Microsoft.

the class OneCollectorChannelListenerTest method enqueueConvertedLogs.

@Test
public void enqueueConvertedLogs() {
    /* Mock original log. */
    Log originalLog = mock(Log.class);
    when(originalLog.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("t1")));
    /* Mock a log. */
    CommonSchemaLog log1 = new MockCommonSchemaLog();
    log1.setIKey("t1");
    Extensions ext1 = new Extensions();
    ext1.setSdk(new SdkExtension());
    log1.setExt(ext1);
    /* Mock another log. */
    CommonSchemaLog log2 = new MockCommonSchemaLog();
    log2.setIKey("t1");
    Extensions ext2 = new Extensions();
    ext2.setSdk(new SdkExtension());
    log2.setExt(ext2);
    /* Mock conversion of logs. */
    Channel channel = mock(Channel.class);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Arrays.asList(log1, log2));
    /* Init listener. */
    UUID installId = UUID.randomUUID();
    OneCollectorChannelListener listener = new OneCollectorChannelListener(channel, logSerializer, createHttpClient(mock(Context.class)), installId);
    listener.onPreparedLog(originalLog, TEST_GROUP, DEFAULTS);
    listener.onPreparedLog(mock(CommonSchemaLog.class), TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
    /* Verify conversion. */
    verify(logSerializer).toCommonSchemaLog(originalLog);
    verifyNoMoreInteractions(logSerializer);
    /* Verify flags. */
    assertEquals(Long.valueOf(DEFAULTS), log1.getFlags());
    assertEquals(Long.valueOf(DEFAULTS), log2.getFlags());
    /* Verify same epoch. */
    assertNotNull(log1.getExt().getSdk().getEpoch());
    assertEquals(log1.getExt().getSdk().getEpoch(), log2.getExt().getSdk().getEpoch());
    /* Verify incremented sequence numbers. */
    assertEquals(Long.valueOf(1), log1.getExt().getSdk().getSeq());
    assertEquals(Long.valueOf(2), log2.getExt().getSdk().getSeq());
    /* Verify install ID set. */
    assertEquals(installId, log1.getExt().getSdk().getInstallId());
    assertEquals(installId, log2.getExt().getSdk().getInstallId());
    /* Verify enqueue. */
    verify(channel).enqueue(log1, TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
    verify(channel).enqueue(log2, TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
    /* We simulated that we see on prepared log on the enqueued log, verify no more enqueuing. */
    verify(channel, times(2)).enqueue(any(Log.class), anyString(), eq(DEFAULTS));
    /* Mock log with another key to see new seq/epoch. */
    when(originalLog.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("t2")));
    CommonSchemaLog log3 = new MockCommonSchemaLog();
    log3.setIKey("t2");
    Extensions ext3 = new Extensions();
    ext3.setSdk(new SdkExtension());
    log3.setExt(ext3);
    when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log3));
    listener.onPreparedLog(originalLog, TEST_GROUP, CRITICAL);
    assertEquals(Long.valueOf(CRITICAL), log3.getFlags());
    assertEquals(Long.valueOf(1), log3.getExt().getSdk().getSeq());
    assertNotNull(log3.getExt().getSdk().getEpoch());
    assertNotEquals(log1.getExt().getSdk().getEpoch(), log3.getExt().getSdk().getEpoch());
    /* Simulate disable/enable to reset epoch/seq. */
    listener.onGloballyEnabled(false);
    listener.onGloballyEnabled(true);
    /* Mock a 4rd log in first group to check reset. */
    CommonSchemaLog log4 = new MockCommonSchemaLog();
    log4.setIKey("t2");
    Extensions ext4 = new Extensions();
    ext4.setSdk(new SdkExtension());
    log4.setExt(ext4);
    when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log4));
    listener.onPreparedLog(originalLog, TEST_GROUP, NORMAL);
    /* Verify flags and reset of epoch/seq. */
    assertEquals(Long.valueOf(NORMAL), log4.getFlags());
    assertEquals(Long.valueOf(1), log4.getExt().getSdk().getSeq());
    assertNotNull(log4.getExt().getSdk().getEpoch());
    assertNotEquals(log3.getExt().getSdk().getEpoch(), log4.getExt().getSdk().getEpoch());
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) SdkExtension(com.microsoft.appcenter.ingestion.models.one.SdkExtension) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)2 SdkExtension (com.microsoft.appcenter.ingestion.models.one.SdkExtension)2 Log (com.microsoft.appcenter.ingestion.models.Log)1 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)1 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)1 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)1 UUID (java.util.UUID)1 Test (org.junit.Test)1