Skip to content

Commit

Permalink
Pull request #255: RUM-9570 Events Spec 1.2
Browse files Browse the repository at this point in the history
Merge in OP/openkit-java from feature/RUM-9570-openkit-java-update-json-serialization-of-special-numeric-values-for-events to main

* commit 'd864e3bde1f733b565b2a7fe46b0713f4ff900e4':
  RUM-9570 Changing special pattern cases
  RUM-9570 Adding special pattern cases
  RUM-9570 Changing isFinite call
  RUM-9570 Removed unused import
  RUM-9570 Events Spec 1.2

GitOrigin-RevId: 65f5a02564a270fddc0c727d622e02e538b1af83
  • Loading branch information
TheHighriser authored and openkitdt committed Apr 4, 2023
1 parent ed2274a commit 34f68c7
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Disable Chunked Mode for sending data via HttpURLConnection
- Non-finite numeric values are serialized as JSON null in reported events, and a special field is added for supportability.

## 3.0.0 [Release date: 2022-12-06]
[GitHub Releases](https://github.com/Dynatrace/openkit-java/releases/tag/v3.0.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.util.*;

import static com.dynatrace.openkit.core.util.EventPayloadBuilderUtil.isItemContainingNonFiniteNumericValues;

public class EventPayloadBuilder {

/** {@link Logger} for tracing log message */
Expand Down Expand Up @@ -91,4 +93,18 @@ public EventPayloadBuilder cleanReservedInternalAttributes() {
public static boolean isReservedForInternalAttributes(String key) {
return (key.equals("dt") || key.startsWith("dt."));
}

/**
* Checks if the attributes contain a non-finite value
* @return True if non-finite values is within attributes
*/
public boolean isEventPayloadContainingNonFiniteValues() {
for (JSONValue value : attributes.values()) {
if(isItemContainingNonFiniteNumericValues(value)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2018-2021 Dynatrace LLC
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.dynatrace.openkit.core.util;

import com.dynatrace.openkit.core.objects.EventPayloadBuilder;
import com.dynatrace.openkit.util.json.objects.JSONArrayValue;
import com.dynatrace.openkit.util.json.objects.JSONNumberValue;
import com.dynatrace.openkit.util.json.objects.JSONObjectValue;
import com.dynatrace.openkit.util.json.objects.JSONValue;

import java.util.Iterator;

public class EventPayloadBuilderUtil {

private EventPayloadBuilderUtil() {
}

private static boolean isObjectContainingNonFiniteNumericValues(JSONObjectValue jsonObject) {
for(String key: jsonObject.keySet()) {
if(isItemContainingNonFiniteNumericValues(jsonObject.get(key))) {
return true;
}
}

return false;
}

private static boolean isArrayContainingNonFiniteNumericValues(JSONArrayValue jsonArrayValue) {
Iterator<JSONValue> it = jsonArrayValue.iterator();

while(it.hasNext()) {
if(isItemContainingNonFiniteNumericValues(it.next())) {
return true;
}
}

return false;
}

public static boolean isItemContainingNonFiniteNumericValues(JSONValue jsonValue) {
return (jsonValue.isObject() && isObjectContainingNonFiniteNumericValues((JSONObjectValue) jsonValue))
|| (jsonValue.isArray() && isArrayContainingNonFiniteNumericValues((JSONArrayValue) jsonValue))
|| (jsonValue.isNumber() && !((JSONNumberValue) jsonValue).isFinite());
}

}
11 changes: 10 additions & 1 deletion src/main/java/com/dynatrace/openkit/protocol/Beacon.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.dynatrace.openkit.providers.RandomNumberGenerator;
import com.dynatrace.openkit.providers.ThreadIDProvider;
import com.dynatrace.openkit.providers.TimingProvider;
import com.dynatrace.openkit.util.json.objects.JSONBooleanValue;
import com.dynatrace.openkit.util.json.objects.JSONNumberValue;
import com.dynatrace.openkit.util.json.objects.JSONStringValue;
import com.dynatrace.openkit.util.json.objects.JSONValue;
Expand Down Expand Up @@ -859,7 +860,7 @@ private void generateSendEventPayload(EventPayloadBuilder builder) {
.addNonOverridableAttribute(EVENT_PAYLOAD_APPLICATION_ID, JSONStringValue.fromString(configuration.getOpenKitConfiguration().getPercentEncodedApplicationID()))
.addNonOverridableAttribute(EVENT_PAYLOAD_INSTANCE_ID, JSONStringValue.fromString(String.valueOf(deviceID)))
.addNonOverridableAttribute(EVENT_PAYLOAD_SESSION_ID, JSONStringValue.fromString(String.valueOf(getSessionNumber())))
.addNonOverridableAttribute("dt.rum.schema_version", JSONStringValue.fromString("1.1"))
.addNonOverridableAttribute("dt.rum.schema_version", JSONStringValue.fromString("1.2"))
.addOverridableAttribute(EventPayloadAttributes.APP_VERSION, JSONStringValue.fromString(configuration.getOpenKitConfiguration().getApplicationVersion()))
.addOverridableAttribute(EventPayloadAttributes.OS_NAME, JSONStringValue.fromString(configuration.getOpenKitConfiguration().getOperatingSystem()))
.addOverridableAttribute(EventPayloadAttributes.DEVICE_MANUFACTURER, JSONStringValue.fromString(configuration.getOpenKitConfiguration().getManufacturer()))
Expand Down Expand Up @@ -909,6 +910,10 @@ public void sendBizEvent(String type, Map<String, JSONValue> attributes) {

builder.addNonOverridableAttribute(EventPayloadAttributes.EVENT_KIND, JSONStringValue.fromString(EVENT_KIND_BIZ));

if(builder.isEventPayloadContainingNonFiniteValues()) {
builder.addNonOverridableAttribute("dt.rum.has_nfn_values", JSONBooleanValue.fromValue(true));
}

sendEventPayload(builder);
}

Expand All @@ -932,6 +937,10 @@ public void sendEvent(String name, Map<String, JSONValue> attributes) {
builder.addNonOverridableAttribute("event.name", JSONStringValue.fromString(name))
.addOverridableAttribute(EventPayloadAttributes.EVENT_KIND, JSONStringValue.fromString(EVENT_KIND_RUM));

if(builder.isEventPayloadContainingNonFiniteValues()) {
builder.addNonOverridableAttribute("dt.rum.has_nfn_values", JSONBooleanValue.fromValue(true));
}

sendEventPayload(builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ public static JSONNumberValue fromNumberLiteral(String literalValue) {

@Override
void writeJSONString(JSONValueWriter writer, JSONOutputConfig config) {
if (isInteger()) {
writer.insertValue(String.valueOf(longValue));
if (!isFinite()) {
writer.insertValue("null");
} else {
writer.insertValue(String.valueOf(doubleValue));
if (isInteger()) {
writer.insertValue(String.valueOf(longValue));
} else {
writer.insertValue(String.valueOf(doubleValue));
}
}
}

Expand Down Expand Up @@ -199,4 +203,13 @@ public float getFloatValue() {
public double getDoubleValue() {
return doubleValue;
}

/**
* Checks if the number is finite.
*
* @return True if number is finite.
*/
public boolean isFinite() {
return isInteger || !Double.isNaN(doubleValue) && !Double.isInfinite(doubleValue);
}
}
Loading

0 comments on commit 34f68c7

Please sign in to comment.