Skip to content

Commit

Permalink
add parameter typing, change attributes names
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-suhorukov committed Jul 22, 2024
1 parent 7500d3e commit 1b1f307
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,11 @@ void captureMdcAttributes(AttributesBuilder attributes, Map<String, String> mdcP
}

void captureArguments(AttributesBuilder attributes, String message, Object[] arguments) {
attributes.put("src_msg_", message);
String bodyKey = "log.body.original";
attributes.put(bodyKey, message);
for (int idx = 0; idx < arguments.length; idx++) {
Object argument = arguments[idx];
attributes.put("log_arg_" + idx, String.valueOf(argument));
propagateAttribute(attributes, String.format("%s.param.%d", bodyKey, idx), argument);
}
}

Expand Down Expand Up @@ -247,21 +248,26 @@ private static void captureKeyValuePairAttributes(
if (keyValuePairs != null) {
for (KeyValuePair keyValuePair : keyValuePairs) {
Object value = keyValuePair.value;
if (keyValuePair.value != null) {
// preserve type for boolean and numeric values, everything else is converted to String
if (value instanceof Boolean) {
attributes.put(keyValuePair.key, (Boolean) keyValuePair.value);
} else if (value instanceof Byte
|| value instanceof Integer
|| value instanceof Long
|| value instanceof Short) {
attributes.put(keyValuePair.key, ((Number) keyValuePair.value).longValue());
} else if (value instanceof Double || value instanceof Float) {
attributes.put(keyValuePair.key, ((Number) keyValuePair.value).doubleValue());
} else {
attributes.put(getAttributeKey(keyValuePair.key), keyValuePair.value.toString());
}
}
propagateAttribute(attributes, keyValuePair.key, value);
}
}
}

@NoMuzzle
private static void propagateAttribute(AttributesBuilder attributes, String key, Object value) {
if (value != null) {
// preserve type for boolean and numeric values, everything else is converted to String
if (value instanceof Boolean) {
attributes.put(key, (Boolean) value);
} else if (value instanceof Byte
|| value instanceof Integer
|| value instanceof Long
|| value instanceof Short) {
attributes.put(key, ((Number) value).longValue());
} else if (value instanceof Double || value instanceof Float) {
attributes.put(key, ((Number) value).doubleValue());
} else {
attributes.put(getAttributeKey(key), value.toString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ void multipleMarkers() {
void arguments() {
logger
.atInfo()
.setMessage("log message {} and {}")
.setMessage("log message {} and {}, bool {}, long {}")
.addArgument("'world'")
.addArgument(Math.PI)
.addArgument(true)
.addArgument(Long.MAX_VALUE)
.log();

List<LogRecordData> logDataList = logRecordExporter.getFinishedLogRecordItems();
Expand All @@ -128,12 +130,18 @@ void arguments() {

assertThat(logData.getResource()).isEqualTo(resource);
assertThat(logData.getInstrumentationScopeInfo()).isEqualTo(instrumentationScopeInfo);
assertThat(logData.getBody().asString()).isEqualTo("log message 'world' and 3.141592653589793");
assertThat(logData.getAttributes().size()).isEqualTo(7);
assertThat(logData.getBody().asString())
.isEqualTo(
"log message 'world' and 3.141592653589793, bool true, long 9223372036854775807");
assertThat(logData.getAttributes().size()).isEqualTo(9);
assertThat(logData)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("log_arg_0"), "'world'"),
equalTo(AttributeKey.stringKey("log_arg_1"), "3.141592653589793"),
equalTo(AttributeKey.stringKey("src_msg_"), "log message {} and {}"));
equalTo(AttributeKey.stringKey("log.body.original.param.0"), "'world'"),
equalTo(AttributeKey.doubleKey("log.body.original.param.1"), Math.PI),
equalTo(AttributeKey.booleanKey("log.body.original.param.2"), true),
equalTo(AttributeKey.longKey("log.body.original.param.3"), Long.MAX_VALUE),
equalTo(
AttributeKey.stringKey("log.body.original"),
"log message {} and {}, bool {}, long {}"));
}
}

0 comments on commit 1b1f307

Please sign in to comment.