Skip to content

Commit 08f988b

Browse files
mfahadahmedmikeproeng37
authored andcommitted
Fix log levels in GetNumericValue. (#88)
1 parent c98a46c commit 08f988b

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

OptimizelySDK.Tests/UtilsTests/EventTagUtilsTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void TestGetRevenueValue()
7676

7777
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Event tags is undefined."), Times.Once);
7878
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The revenue key is not defined in the event tags."), Times.Exactly(2));
79-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "The revenue key value is not defined in event tags."), Times.Once);
79+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The revenue key value is not defined in event tags."), Times.Once);
8080
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Revenue value is not an integer or couldn't be parsed as an integer."), Times.Once);
8181

8282
// Valid data.
@@ -126,9 +126,9 @@ public void TestGetEventValue()
126126
Assert.Null(EventTagUtils.GetNumericValue(invalidTag, Logger));
127127
Assert.Null(EventTagUtils.GetNumericValue(nullValue, Logger));
128128

129-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Event tags is undefined."), Times.Once);
130-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "The numeric metric key is not in event tags."), Times.Once);
131-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "The numeric metric key value is not defined in event tags."), Times.Once);
129+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Event tags is undefined."), Times.Once);
130+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The numeric metric key is not in event tags."), Times.Once);
131+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The numeric metric key value is not defined in event tags."), Times.Once);
132132

133133
// Valid data.
134134
Assert.AreEqual(42, EventTagUtils.GetNumericValue(validTagStr, Logger));
@@ -147,7 +147,7 @@ public void TestGetEventValue()
147147
public void TestGetNumericMetricInvalidArgs()
148148
{
149149
Assert.IsNull(EventTagUtils.GetNumericValue(null, Logger));
150-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Event tags is undefined."), Times.Once);
150+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Event tags is undefined."), Times.Once);
151151

152152
//Errors for all, because it accepts only dictionary//
153153
// Not valid test cases in C#
@@ -167,7 +167,7 @@ public void TestGetNumericMetricNoValueTag()
167167
Assert.IsNull(EventTagUtils.GetNumericValue(new Dictionary<string, object> { }, Logger));
168168
Assert.IsNull(EventTagUtils.GetNumericValue(new Dictionary<string, object> { { "non-value", 42 } }, Logger));
169169

170-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "The numeric metric key is not in event tags."), Times.Exactly(2));
170+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The numeric metric key is not in event tags."), Times.Exactly(2));
171171

172172
//Errors for all, because it accepts only dictionary//
173173
//Assert.IsNull(EventTagUtils.GetEventValue(new object[] { }));
@@ -188,7 +188,7 @@ public void TestGetNumericMetricInvalidValueTag()
188188
Assert.IsNull(EventTagUtils.GetNumericValue(new Dictionary<string, object> { { "non-value", new object[] { 1, 2, 3 } } }, Logger));
189189
Assert.IsNull(EventTagUtils.GetNumericValue(new Dictionary<string, object> { { "non-value", new object[] { 'a', 'b', 'c' } } }, Logger));
190190

191-
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "The numeric metric key is not in event tags."), Times.Exactly(8));
191+
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The numeric metric key is not in event tags."), Times.Exactly(8));
192192
}
193193

194194
[Test]

OptimizelySDK/Utils/EventTagUtils.cs

+42-30
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static object GetRevenueValue(Dictionary<string, object> eventTags, ILogg
4444
else if (eventTags[REVENUE_EVENT_METRIC_NAME] == null)
4545
{
4646
logMessage = "The revenue key value is not defined in event tags.";
47-
logLevel = LogLevel.ERROR;
47+
logLevel = LogLevel.DEBUG;
4848
}
4949
else if (!int.TryParse(eventTags[REVENUE_EVENT_METRIC_NAME].ToString(), out result))
5050
{
@@ -68,63 +68,75 @@ public static object GetRevenueValue(Dictionary<string, object> eventTags, ILogg
6868

6969
public static object GetNumericValue(Dictionary<string, object> eventTags, ILogger logger = null)
7070
{
71-
string debugMessage = string.Empty;
72-
bool isCasted = false;
73-
7471
float refVar = 0;
72+
bool isCasted = false;
73+
string logMessage = string.Empty;
74+
LogLevel logLevel = LogLevel.INFO;
7575

7676
if (eventTags == null)
77-
debugMessage = "Event tags is undefined.";
78-
77+
{
78+
logMessage = "Event tags is undefined.";
79+
logLevel = LogLevel.DEBUG;
80+
}
7981
else if (!eventTags.ContainsKey(VALUE_EVENT_METRIC_NAME))
80-
debugMessage = "The numeric metric key is not in event tags.";
81-
82+
{
83+
logMessage = "The numeric metric key is not in event tags.";
84+
logLevel = LogLevel.DEBUG;
85+
}
8286
else if (eventTags[VALUE_EVENT_METRIC_NAME] == null)
83-
debugMessage = "The numeric metric key value is not defined in event tags.";
84-
87+
{
88+
logMessage = "The numeric metric key value is not defined in event tags.";
89+
logLevel = LogLevel.DEBUG;
90+
}
8591
else if (eventTags[VALUE_EVENT_METRIC_NAME] is bool)
86-
debugMessage = "Provided numeric value is boolean which is an invalid format.";
87-
92+
{
93+
logMessage = "Provided numeric value is boolean which is an invalid format.";
94+
logLevel = LogLevel.ERROR;
95+
}
8896
else if (!(eventTags[VALUE_EVENT_METRIC_NAME] is int) && !(eventTags[VALUE_EVENT_METRIC_NAME] is string) && !(eventTags[VALUE_EVENT_METRIC_NAME] is float)
89-
&& !(eventTags[VALUE_EVENT_METRIC_NAME] is decimal)
90-
&& !(eventTags[VALUE_EVENT_METRIC_NAME] is double)
91-
&& !(eventTags[VALUE_EVENT_METRIC_NAME] is float))
92-
debugMessage = "Numeric metric value is not in integer, float, or string form.";
93-
97+
&& !(eventTags[VALUE_EVENT_METRIC_NAME] is decimal) && !(eventTags[VALUE_EVENT_METRIC_NAME] is double) && !(eventTags[VALUE_EVENT_METRIC_NAME] is float))
98+
{
99+
logMessage = "Numeric metric value is not in integer, float, or string form.";
100+
logLevel = LogLevel.ERROR;
101+
}
102+
else if (!float.TryParse(eventTags[VALUE_EVENT_METRIC_NAME].ToString(), out refVar))
103+
{
104+
logMessage = "Provided numeric value is boolean which is an invalid format.";
105+
logLevel = LogLevel.ERROR;
106+
}
94107
else
95108
{
96109
if (!float.TryParse(eventTags[VALUE_EVENT_METRIC_NAME].ToString(), out refVar))
97-
debugMessage = string.Format("Provided numeric value {0} is in an invalid format.", eventTags[VALUE_EVENT_METRIC_NAME]);
110+
{
111+
logMessage = string.Format("Provided numeric value {0} is in an invalid format.", eventTags[VALUE_EVENT_METRIC_NAME]);
112+
logLevel = LogLevel.ERROR;
113+
}
98114
else
99115
{
100116
if (float.IsInfinity(refVar))
101-
debugMessage = string.Format("Provided numeric value {0} is in an invalid format.", eventTags[VALUE_EVENT_METRIC_NAME]);
117+
{
118+
logMessage = string.Format("Provided numeric value {0} is in an invalid format.", eventTags[VALUE_EVENT_METRIC_NAME]);
119+
logLevel = LogLevel.ERROR;
120+
}
102121
else
122+
{
103123
isCasted = true;
124+
logMessage = string.Format("The numeric metric value {0} will be sent to results.", refVar);
125+
}
104126
}
105127
}
106128

107129
if (logger != null)
108-
{
109-
if (isCasted)
110-
logger.Log(LogLevel.INFO, string.Format("The numeric metric value {0} will be sent to results.", refVar));
111-
else
112-
if (string.IsNullOrEmpty(debugMessage))
113-
logger.Log(LogLevel.ERROR, string.Format("The provided numeric metric value {0} is in an invalid format and will not be sent to results.", eventTags[VALUE_EVENT_METRIC_NAME]));
114-
else
115-
logger.Log(LogLevel.ERROR, debugMessage);
116-
}
130+
logger.Log(logLevel, logMessage);
117131

118132
object o = refVar;
119-
120133
if(isCasted && eventTags[VALUE_EVENT_METRIC_NAME] is float)
121134
{
122135
// Special case, maximum value when passed and gone through tryparse, it loses precision.
123136
o = eventTags[VALUE_EVENT_METRIC_NAME];
124137
}
125138

126139
return isCasted ? o : null;
127-
128140
}
129141
}
130142
}

0 commit comments

Comments
 (0)