Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FSSDK-9775] Log invalid user attributes #369

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions OptimizelySDK.Tests/EventTests/UserEventFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
/*
*
* Copyright 2019-2020, Optimizely and contributors
* Copyright 2019-2020, 2023 Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -15,6 +15,7 @@
* limitations under the License.
*/

using System;
using Moq;
using NUnit.Framework;
using OptimizelySDK.Config;
Expand Down Expand Up @@ -96,6 +97,44 @@ public void ImpressionEventTestWithAttributes()
TestData.CompareObjects(expectedVisitorAttributes, impressionEvent.VisitorAttributes);
}

[Test]
public void EventFactoryBuildShouldLogInvalidUserAttributes()
{
var projectConfig = Config;
var invalidUserAttributes = new UserAttributes
{
// valid attribute keys in TestData.json but invalid values
{ "device_type", new DateTime() },
{ "location", new int[7] },
};
var expectedLogMessage =
$"User attributes: device_type, location were invalid and omitted.";

EventFactory.BuildAttributeList(invalidUserAttributes, projectConfig,
LoggerMock.Object);

LoggerMock.Verify(l => l.Log(LogLevel.WARN, expectedLogMessage), Times.Once());
}

[Test]
public void EventFactoryBuildShouldLogUserAttributesNotInDatafile()
{
var projectConfig = Config;
var userAttributes = new UserAttributes
{
{ "device_type", "valid_since_device_type_is_in_TestData_json" },
{ "attribute_not_in_TestData_json", 42 },
{ "another_attribute_not_in_TestData_json", true },
};
var expectedLogMessage =
$"User attributes: attribute_not_in_TestData_json, another_attribute_not_in_TestData_json are not supported by the datafile and will not be used.";

EventFactory.BuildAttributeList(userAttributes, projectConfig,
LoggerMock.Object);

LoggerMock.Verify(l => l.Log(LogLevel.WARN, expectedLogMessage), Times.Once());
}

[Test]
public void ConversionEventTest()
{
Expand Down
46 changes: 34 additions & 12 deletions OptimizelySDK/Event/EventFactory.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2019-2020, Optimizely
/*
* Copyright 2019-2020, 2023 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -22,7 +22,6 @@
using OptimizelySDK.Logger;
using OptimizelySDK.Utils;


namespace OptimizelySDK.Event
{
/// <summary>
Expand Down Expand Up @@ -75,7 +74,7 @@ public static LogEvent CreateLogEvent(UserEvent[] userEvents, ILogger logger)
}
else
{
//TODO: Need to log a message, invalid UserEvent added in a list.
logger.Log(LogLevel.WARN, "Invalid UserEvent added in a list.");
continue;
}

Expand Down Expand Up @@ -181,9 +180,10 @@ private static Visitor CreateVisitor(ConversionEvent conversionEvent, ILogger lo
/// </summary>
/// <param name="userAttributes">The user's attributes</param>
/// <param name="config">ProjectConfig instance</param>
/// <param name="logger">An optional ILogger implementation</param>
/// <returns>VisitorAttribute array if config is valid, null otherwise</returns>
public static VisitorAttribute[] BuildAttributeList(UserAttributes userAttributes,
ProjectConfig config
ProjectConfig config, ILogger logger = null
)
{
if (config == null)
Expand All @@ -192,18 +192,40 @@ ProjectConfig config
}

var attributesList = new List<VisitorAttribute>();
var invalidUserAttributeKeys = new List<string>();
var notRegisteredAttributeKeys = new List<string>();

if (userAttributes != null)
{
foreach (var validUserAttribute in userAttributes.Where(attribute =>
Validator.IsUserAttributeValid(attribute)))
foreach (var userAttribute in userAttributes)
{
var attributeId = config.GetAttributeId(validUserAttribute.Key);
if (!string.IsNullOrEmpty(attributeId))
var attributeId = config.GetAttributeId(userAttribute.Key);
if (string.IsNullOrEmpty(attributeId))
{
notRegisteredAttributeKeys.Add(userAttribute.Key);
continue;
}

if (!Validator.IsUserAttributeValid(userAttribute))
{
attributesList.Add(new VisitorAttribute(attributeId, validUserAttribute.Key,
CUSTOM_ATTRIBUTE_FEATURE_TYPE, validUserAttribute.Value));
invalidUserAttributeKeys.Add(userAttribute.Key);
continue;
}

attributesList.Add(new VisitorAttribute(attributeId, userAttribute.Key,
CUSTOM_ATTRIBUTE_FEATURE_TYPE, userAttribute.Value));
}

if (notRegisteredAttributeKeys.Count > 0 && !(logger is null))
{
logger.Log(LogLevel.WARN,
$"User attributes: {string.Join(", ", notRegisteredAttributeKeys.ToArray())} are not supported by the datafile and will not be used.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to log all these here. I see some clients have 100+ attributes not all registered to us. Each event will dump a bunch of these messages. Let's discuss.


if (invalidUserAttributeKeys.Count > 0 && !(logger is null))
{
logger.Log(LogLevel.WARN,
$"User attributes: {string.Join(", ", invalidUserAttributeKeys.ToArray())} were invalid and omitted.");
}
}

Expand Down
Loading