private void TestInlineUserSerialization(User user, LdValue expectedJsonValue, EventsConfiguration config)
        {
            config.InlineUsersInEvents = true;
            var f = new EventOutputFormatter(config);

            var evalEvent = new EvaluationEvent { FlagKey = "flag", User = user };
            var outputEvent = SerializeOneEvent(f, evalEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.Equal(user.Anonymous ? LdValue.Of("anonymousUser") : LdValue.Null, outputEvent.Get("contextKind"));

            var identifyEvent = new IdentifyEvent { User = user };
            outputEvent = SerializeOneEvent(f, identifyEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.False(outputEvent.Dictionary.ContainsKey("contextKind"));

            var customEvent = new CustomEvent { EventKey = "customkey", User = user };
            outputEvent = SerializeOneEvent(f, customEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.Equal(user.Anonymous ? LdValue.Of("anonymousUser") : LdValue.Null, outputEvent.Get("contextKind"));

            var indexEvent = new IndexEvent { User = user };
            outputEvent = SerializeOneEvent(f, indexEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.False(outputEvent.Dictionary.ContainsKey("contextKind"));
        }
        public void UserKeyIsSetInsteadOfUserWhenNotInlined()
        {
            var user = User.Builder("userkey")
                .Name("me")
                .Build();
            var userJson = LdValue.Parse(@"{""key"":""userkey"",""name"":""me""}");
            var f = new EventOutputFormatter(new EventsConfiguration());

            var evalEvent = new EvaluationEvent { FlagKey = "flag", User = user };
            var outputEvent = SerializeOneEvent(f, evalEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            // user is always inlined in identify event
            var identifyEvent = new IdentifyEvent { User = user };
            outputEvent = SerializeOneEvent(f, identifyEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(userJson, outputEvent.Get("user"));

            var customEvent = new CustomEvent { EventKey = "customkey", User = user };
            outputEvent = SerializeOneEvent(f, customEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            // user is always inlined in index event
            var indexEvent = new IndexEvent { Timestamp = _fixedTimestamp, User = user };
            outputEvent = SerializeOneEvent(f, indexEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(userJson, outputEvent.Get("user"));
        }
        private void ProcessEvent(Event e, EventBuffer buffer)
        {
            if (_disabled)
            {
                return;
            }

            // Always record the event in the summarizer.
            buffer.AddToSummary(e);

            // Decide whether to add the event to the payload. Feature events may be added twice, once for
            // the event (if tracked) and once for debugging.
            bool  willAddFullEvent = false;
            Event debugEvent       = null;

            if (e is FeatureRequestEvent fe)
            {
                if (ShouldSampleEvent())
                {
                    willAddFullEvent = fe.TrackEvents;
                    if (ShouldDebugEvent(fe))
                    {
                        debugEvent = EventFactory.Default.NewDebugEvent(fe);
                    }
                }
            }
            else
            {
                willAddFullEvent = ShouldSampleEvent();
            }

            // Tell the user deduplicator, if any, about this user; this may produce an index event.
            // We only need to do this if there is *not* already going to be a full-fidelity event
            // containing an inline user.
            if (!(willAddFullEvent && _config.InlineUsersInEvents))
            {
                if (_userDeduplicator != null && e.User != null)
                {
                    bool needUserEvent = _userDeduplicator.ProcessUser(e.User);
                    if (needUserEvent && !(e is IdentifyEvent))
                    {
                        IndexEvent ie = new IndexEvent(e.CreationDate, e.User);
                        buffer.AddEvent(ie);
                    }
                }
            }

            if (willAddFullEvent)
            {
                buffer.AddEvent(e);
            }
            if (debugEvent != null)
            {
                buffer.AddEvent(debugEvent);
            }
        }
        public void UserKeyIsSetInsteadOfUserWhenNotInlined()
        {
            var user = User.Builder("userkey")
                       .Name("me")
                       .Build();
            var userJson     = LdValue.Parse(@"{""key"":""userkey"",""name"":""me""}");
            var f            = new EventOutputFormatter(new SimpleConfiguration());
            var emptySummary = new EventSummary();

            var featureEvent = EventFactory.Default.NewFeatureRequestEvent(
                new FlagEventPropertiesBuilder("flag").Build(),
                user,
                new EvaluationDetail <LdValue>(LdValue.Null, null, EvaluationReason.OffReason),
                LdValue.Null);
            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out var count)).Get(0);

            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            var identifyEvent = EventFactory.Default.NewIdentifyEvent(user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            var customEvent = EventFactory.Default.NewCustomEvent("custom", user, LdValue.Null);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            // user is always inlined in index event
            var indexEvent = new IndexEvent(0, user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { indexEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(userJson, outputEvent.Get("user"));
        }
        private void TestInlineUserSerialization(User user, LdValue expectedJsonValue, SimpleConfiguration config)
        {
            config.InlineUsersInEvents = true;
            var f            = new EventOutputFormatter(config);
            var emptySummary = new EventSummary();

            var featureEvent = EventFactory.Default.NewFeatureRequestEvent(
                new FlagEventPropertiesBuilder("flag").Build(),
                user,
                new EvaluationDetail <LdValue>(LdValue.Null, null, EvaluationReason.OffReason),
                LdValue.Null);
            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out var count)).Get(0);

            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));

            var identifyEvent = EventFactory.Default.NewIdentifyEvent(user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));

            var customEvent = EventFactory.Default.NewCustomEvent("custom", user, LdValue.Null);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { featureEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));

            var indexEvent = new IndexEvent(0, user);

            outputEvent = LdValue.Parse(f.SerializeOutputEvents(new Event[] { indexEvent }, emptySummary, out count)).Get(0);
            Assert.Equal(1, count);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
        }
Exemple #6
0
 private void Awake()
 {
     UpdatedColor = new IndexEvent();
 }
Exemple #7
0
        private void ProcessEvent(object e, EventBuffer buffer)
        {
            if (_disabled)
            {
                return;
            }

            // Decide whether to add the event to the payload. Feature events may be added twice, once for
            // the event (if tracked) and once for debugging.
            bool                willAddFullEvent = true;
            DebugEvent?         debugEvent       = null;
            UnixMillisecondTime timestamp;
            User                user = null;

            switch (e)
            {
            case EvaluationEvent ee:
                buffer.AddToSummary(ee);     // only evaluation events go into the summarizer
                timestamp        = ee.Timestamp;
                user             = ee.User;
                willAddFullEvent = ee.TrackEvents;
                if (ShouldDebugEvent(ee))
                {
                    debugEvent = new DebugEvent {
                        FromEvent = ee
                    };
                }
                break;

            case IdentifyEvent ie:
                timestamp = ie.Timestamp;
                user      = ie.User;
                break;

            case CustomEvent ce:
                timestamp = ce.Timestamp;
                user      = ce.User;
                break;

            default:
                timestamp = new UnixMillisecondTime();
                break;
            }

            // Tell the user deduplicator, if any, about this user; this may produce an index event.
            // We only need to do this if there is *not* already going to be a full-fidelity event
            // containing an inline user.
            if (!(willAddFullEvent && _config.InlineUsersInEvents) && user != null &&
                _userDeduplicator != null)
            {
                bool needUserEvent = _userDeduplicator.ProcessUser(user);
                if (needUserEvent && !(e is IdentifyEvent))
                {
                    IndexEvent ie = new IndexEvent {
                        Timestamp = timestamp, User = user
                    };
                    buffer.AddEvent(ie);
                }
                else if (!(e is IdentifyEvent))
                {
                    _diagnosticStore?.IncrementDeduplicatedUsers();
                }
            }

            if (willAddFullEvent)
            {
                buffer.AddEvent(e);
            }
            if (debugEvent != null)
            {
                buffer.AddEvent(debugEvent);
            }
        }