@Test public void testStripPayloadMetadataToAttributes() throws Exception { ObjectNode metadata = mapToObjectNode(ImmutableMap.<String, Object>builder() .put("uri", ImmutableMap.of("app_name", "Firefox")) .put("header", ImmutableMap.of("x_debug_id", "mysession")) .put("geo", ImmutableMap.of("country", "CA")).put("user_agent", ImmutableMap.of()).build()); ObjectNode payload = Json.createObjectNode(); payload.set("metadata", metadata); payload.put("field1", 99); payload.put("normalized_channel", "release"); payload.put("sample_id", 18); Map<String, String> expected = ImmutableMap.<String, String>builder().put("app_name", "Firefox") .put("sample_id", "18").put("geo_country", "CA").put("x_debug_id", "mysession") .put("normalized_channel", "release").build(); Map<String, String> attributes = new HashMap<>(); NestedMetadata.stripPayloadMetadataToAttributes(attributes, payload); assertEquals(expected, attributes); assertEquals(mapToObjectNode(ImmutableMap.of("field1", 99)), payload); }
public static void stripPayloadMetadataToAttributes(Map<String, String> attributes, ObjectNode payload) { Optional .ofNullable(payload) .map(p -> p.remove(FieldName.METADATA)) .filter(JsonNode::isObject) .map(ObjectNode.class::cast) .ifPresent(metadata -> { putGeoAttributes(attributes, metadata); putIspAttributes(attributes, metadata); putUserAgentAttributes(attributes, metadata); putHeaderAttributes(attributes, metadata); putUriAttributes(attributes, metadata); IDENTIFYING_FIELDS.forEach(name -> Optional.of(metadata) .map(p -> metadata.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); }); TOP_LEVEL_STRING_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); TOP_LEVEL_INT_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .ifPresent(value -> attributes.put(name, value.asText()))); }
NestedMetadata { public static void stripPayloadMetadataToAttributes(Map<String, String> attributes, ObjectNode payload) { Optional .ofNullable(payload) .map(p -> p.remove(FieldName.METADATA)) .filter(JsonNode::isObject) .map(ObjectNode.class::cast) .ifPresent(metadata -> { putGeoAttributes(attributes, metadata); putIspAttributes(attributes, metadata); putUserAgentAttributes(attributes, metadata); putHeaderAttributes(attributes, metadata); putUriAttributes(attributes, metadata); IDENTIFYING_FIELDS.forEach(name -> Optional.of(metadata) .map(p -> metadata.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); }); TOP_LEVEL_STRING_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); TOP_LEVEL_INT_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .ifPresent(value -> attributes.put(name, value.asText()))); } }
NestedMetadata { public static void stripPayloadMetadataToAttributes(Map<String, String> attributes, ObjectNode payload) { Optional .ofNullable(payload) .map(p -> p.remove(FieldName.METADATA)) .filter(JsonNode::isObject) .map(ObjectNode.class::cast) .ifPresent(metadata -> { putGeoAttributes(attributes, metadata); putIspAttributes(attributes, metadata); putUserAgentAttributes(attributes, metadata); putHeaderAttributes(attributes, metadata); putUriAttributes(attributes, metadata); IDENTIFYING_FIELDS.forEach(name -> Optional.of(metadata) .map(p -> metadata.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); }); TOP_LEVEL_STRING_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); TOP_LEVEL_INT_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .ifPresent(value -> attributes.put(name, value.asText()))); } private  NestedMetadata(); }
NestedMetadata { public static void stripPayloadMetadataToAttributes(Map<String, String> attributes, ObjectNode payload) { Optional .ofNullable(payload) .map(p -> p.remove(FieldName.METADATA)) .filter(JsonNode::isObject) .map(ObjectNode.class::cast) .ifPresent(metadata -> { putGeoAttributes(attributes, metadata); putIspAttributes(attributes, metadata); putUserAgentAttributes(attributes, metadata); putHeaderAttributes(attributes, metadata); putUriAttributes(attributes, metadata); IDENTIFYING_FIELDS.forEach(name -> Optional.of(metadata) .map(p -> metadata.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); }); TOP_LEVEL_STRING_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); TOP_LEVEL_INT_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .ifPresent(value -> attributes.put(name, value.asText()))); } private  NestedMetadata(); static byte[] mergedPayload(byte[] payload, byte[] metadata); static ObjectNode attributesToMetadataPayload(Map<String, String> attributes); static void stripPayloadMetadataToAttributes(Map<String, String> attributes,
      ObjectNode payload); }
NestedMetadata { public static void stripPayloadMetadataToAttributes(Map<String, String> attributes, ObjectNode payload) { Optional .ofNullable(payload) .map(p -> p.remove(FieldName.METADATA)) .filter(JsonNode::isObject) .map(ObjectNode.class::cast) .ifPresent(metadata -> { putGeoAttributes(attributes, metadata); putIspAttributes(attributes, metadata); putUserAgentAttributes(attributes, metadata); putHeaderAttributes(attributes, metadata); putUriAttributes(attributes, metadata); IDENTIFYING_FIELDS.forEach(name -> Optional.of(metadata) .map(p -> metadata.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); }); TOP_LEVEL_STRING_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .filter(JsonNode::isTextual) .ifPresent(value -> attributes.put(name, value.textValue()))); TOP_LEVEL_INT_FIELDS.forEach(name -> Optional .ofNullable(payload) .map(p -> p.remove(name)) .ifPresent(value -> attributes.put(name, value.asText()))); } private  NestedMetadata(); static byte[] mergedPayload(byte[] payload, byte[] metadata); static ObjectNode attributesToMetadataPayload(Map<String, String> attributes); static void stripPayloadMetadataToAttributes(Map<String, String> attributes,
      ObjectNode payload);  }