示例#1
0
        private async Task FlushEventsAsync(FlushPayload payload)
        {
            EventOutputFormatter formatter = new EventOutputFormatter(_config);
            string jsonEvents;
            int    eventCount;

            try
            {
                jsonEvents = formatter.SerializeOutputEvents(payload.Events, payload.Summary, out eventCount);
            }
            catch (Exception e)
            {
                LogHelpers.LogException(_logger, "Error preparing events, will not send", e);
                return;
            }

            var result = await _eventSender.SendEventDataAsync(EventDataKind.AnalyticsEvents,
                                                               jsonEvents, eventCount);

            if (result.Status == DeliveryStatus.FailedAndMustShutDown)
            {
                _disabled = true;
            }
            if (result.TimeFromServer.HasValue)
            {
                Interlocked.Exchange(ref _lastKnownPastTime,
                                     UnixMillisecondTime.FromDateTime(result.TimeFromServer.Value).Value);
            }
        }
        public void DebugModeExpiresBasedOnServerTimeIfServerTimeIsLaterThanClientTime()
        {
            // Pick a server time that is somewhat ahead of the client time
            var serverTime = DateTime.Now.Add(TimeSpan.FromSeconds(20));

            var mockSender = MakeMockSender();
            var captured   = EventCapture.From(mockSender,
                                               new EventSenderResult(DeliveryStatus.Succeeded, serverTime));

            using (var ep = MakeProcessor(_config, mockSender))
            {
                // Send and flush an event we don't care about, just to set the last server time
                RecordIdentify(ep, _fixedTimestamp, User.WithKey("otherUser"));
                FlushAndWait(ep);
                captured.Events.Clear();

                // Now send an event with debug mode on, with a "debug until" time that is further in
                // the future than the client time, but in the past compared to the server.
                var flag = BasicFlag;
                flag.DebugEventsUntilDate = UnixMillisecondTime.FromDateTime(serverTime).PlusMillis(-1000);
                RecordEval(ep, flag, BasicEval);
                FlushAndWait(ep);

                // Should get a summary event only, not a full feature event
                Assert.Collection(captured.Events,
                                  item => CheckIndexEvent(item, BasicEval.Timestamp, _userJson),
                                  item => CheckSummaryEvent(item));
            }
        }
示例#3
0
        public void DataSinceFromLastDiagnostic()
        {
            IDiagnosticStore _serverDiagnosticStore = CreateDiagnosticStore(null);
            DiagnosticEvent  periodicEvent          = _serverDiagnosticStore.CreateEventAndReset();

            Assert.Equal(periodicEvent.JsonValue.Get("creationDate").AsLong,
                         UnixMillisecondTime.FromDateTime(_serverDiagnosticStore.DataSince).Value);
        }
        public void DataSinceFromLastDiagnostic()
        {
            var             store         = new DiagnosticStoreImpl();
            DiagnosticEvent periodicEvent = store.CreateEventAndReset();

            Assert.Equal(periodicEvent.JsonValue.Get("creationDate").AsLong,
                         UnixMillisecondTime.FromDateTime(store.DataSince).Value);
        }
        public void AddStreamInit(DateTime timestamp, TimeSpan duration, bool failed)
        {
            var streamInitObject = LdValue.BuildObject();

            streamInitObject.Add("timestamp", UnixMillisecondTime.FromDateTime(timestamp).Value);
            streamInitObject.Add("durationMillis", duration.TotalMilliseconds);
            streamInitObject.Add("failed", failed);
            lock (StreamInitsLock)
            {
                StreamInits.Add(streamInitObject.Build());
            }
        }
示例#6
0
        public void PeriodicEventDefaultValuesAreCorrect()
        {
            IDiagnosticStore _serverDiagnosticStore = CreateDiagnosticStore(null);
            DateTime         dataSince     = _serverDiagnosticStore.DataSince;
            LdValue          periodicEvent = _serverDiagnosticStore.CreateEventAndReset().JsonValue;

            Assert.Equal("diagnostic", periodicEvent.Get("kind").AsString);
            Assert.Equal(UnixMillisecondTime.FromDateTime(dataSince).Value, periodicEvent.Get("dataSinceDate").AsLong);
            Assert.Equal(0, periodicEvent.Get("eventsInLastBatch").AsInt);
            Assert.Equal(0, periodicEvent.Get("droppedEvents").AsInt);
            Assert.Equal(0, periodicEvent.Get("deduplicatedUsers").AsInt);

            LdValue streamInits = periodicEvent.Get("streamInits");

            Assert.Equal(0, streamInits.Count);
        }
示例#7
0
        public void CanAddStreamInit()
        {
            IDiagnosticStore _serverDiagnosticStore = CreateDiagnosticStore(null);
            DateTime         timestamp = DateTime.Now;

            _serverDiagnosticStore.AddStreamInit(timestamp, TimeSpan.FromMilliseconds(200.0), true);
            DiagnosticEvent periodicEvent = _serverDiagnosticStore.CreateEventAndReset();

            LdValue streamInits = periodicEvent.JsonValue.Get("streamInits");

            Assert.Equal(1, streamInits.Count);

            LdValue streamInit = streamInits.Get(0);

            Assert.Equal(UnixMillisecondTime.FromDateTime(timestamp).Value, streamInit.Get("timestamp").AsLong);
            Assert.Equal(200, streamInit.Get("durationMillis").AsInt);
            Assert.True(streamInit.Get("failed").AsBool);
        }
        public DiagnosticEvent CreateEventAndReset()
        {
            DateTime currentTime       = DateTime.Now;
            long     droppedEvents     = Interlocked.Exchange(ref DroppedEvents, 0);
            long     deduplicatedUsers = Interlocked.Exchange(ref DeduplicatedUsers, 0);
            long     eventsInLastBatch = Interlocked.Exchange(ref EventsInLastBatch, 0);
            long     dataSince         = Interlocked.Exchange(ref DataSince, currentTime.ToBinary());

            var statEvent = LdValue.BuildObject();

            AddDiagnosticCommonFields(statEvent, "diagnostic", currentTime);
            statEvent.Add("eventsInLastBatch", eventsInLastBatch);
            statEvent.Add("dataSinceDate", UnixMillisecondTime.FromDateTime(DateTime.FromBinary(dataSince)).Value);
            statEvent.Add("droppedEvents", droppedEvents);
            statEvent.Add("deduplicatedUsers", deduplicatedUsers);
            lock (StreamInitsLock) {
                statEvent.Add("streamInits", StreamInits.Build());
                StreamInits = LdValue.BuildArray();
            }

            return(new DiagnosticEvent(statEvent.Build()));
        }
 private void AddDiagnosticCommonFields(LdValue.ObjectBuilder fieldsBuilder, string kind, DateTime creationDate)
 {
     fieldsBuilder.Add("kind", kind);
     fieldsBuilder.Add("id", EncodeDiagnosticId(DiagnosticId));
     fieldsBuilder.Add("creationDate", UnixMillisecondTime.FromDateTime(creationDate).Value);
 }