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

Implement reading of simple key-value Logstash JSON Marker attributes #12513

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

oldium
Copy link

@oldium oldium commented Oct 24, 2024

Supported are MapEntriesAppendingMarker and SingleFieldAppendingMarker (i.e. ObjectAppendingMarker and RawJsonAppendingMarker) only. The attribute value is always a string retrieved by a call to toString() method.

Typically the Logstash markers are added to logs via Markers.append() and Markers.appendEntries() methods.

Closes #12573

@oldium oldium requested a review from a team as a code owner October 24, 2024 13:00
Copy link

linux-foundation-easycla bot commented Oct 24, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot added the test native This label can be applied to PRs to trigger them to run native tests label Oct 24, 2024
@oldium oldium force-pushed the feature/logstash-markers branch 2 times, most recently from 087cdd9 to 6f0d31f Compare October 24, 2024 13:03
@oldium
Copy link
Author

oldium commented Oct 24, 2024

Ah, Muzzle, I was afraid of that. I have no idea on what is wrong.

@steverao
Copy link
Contributor

Ah, Muzzle, I was afraid of that. I have no idea on what is wrong.

About CI failing, you can refer to https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/CONTRIBUTING.md#troubleshooting-pr-build-failures

@oldium
Copy link
Author

oldium commented Oct 24, 2024

Thanks, hopefully fixed Muzzle.

@oldium
Copy link
Author

oldium commented Oct 25, 2024

Marking as draft:

  • The new config needs to be added to the documentation (README.md in corresponding folders)
  • I will also reuse the key-value method to set numbers and I will extend it with string arrays.

I will finish this later next week.

@oldium oldium changed the title Implement reading of simple key-value Logstash JSON Marker attributes Draft: Implement reading of simple key-value Logstash JSON Marker attributes Oct 25, 2024
@trask trask marked this pull request as draft October 25, 2024 14:13
@trask
Copy link
Member

trask commented Oct 25, 2024

Marking as draft:

click the "Ready for review" button when you are ready for it to be reviewed, thanks!

@oldium oldium changed the title Draft: Implement reading of simple key-value Logstash JSON Marker attributes Implement reading of simple key-value Logstash JSON Marker attributes Oct 31, 2024
@oldium oldium marked this pull request as ready for review October 31, 2024 22:45
Supported are MapEntriesAppendingMarker and SingleFieldAppendingMarker
(i.e. ObjectAppendingMarker and RawJsonAppendingMarker) only. The attribute
value is sent either as boolean, long, double or String or typed-array
with respective values. The generic types (Object[], Collection) is
converted to String array with values converted with String.valueOf()
method.

Typically the Logstash markers are added to logs via Markers.append(),
Markers.appendEntries(), Markers.appendArray() and Markers.appendRaw()
methods.

Signed-off-by: Oldřich Jedlička <[email protected]>
@oldium
Copy link
Author

oldium commented Nov 1, 2024

Updated, rebased, ready for review. It looks like the build errors are unrelated (Lettuce).

Comment on lines 606 to 622
try {
Class.forName("net.logstash.logback.marker.LogstashMarker");
} catch (ClassNotFoundException e) {
return false;
}

try {
Class.forName("net.logstash.logback.marker.SingleFieldAppendingMarker");
} catch (ClassNotFoundException e) {
return false;
}

try {
Class.forName("net.logstash.logback.marker.MapEntriesAppendingMarker");
} catch (ClassNotFoundException e) {
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

you could have all Class.forName calls inside the same try-catch block

@@ -60,6 +70,8 @@ public final class LoggingEventMapper {
private static final AttributeKey<List<String>> LOG_BODY_PARAMETERS =
AttributeKey.stringArrayKey("log.body.parameters");

private static final Cache<Class<?>, Field> valueField = Cache.bounded(20);
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps ClassValue would serve better for caching the fields?

}
}

private static void captureKeyValueAttribute(
Copy link
Contributor

Choose a reason for hiding this comment

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

since this method isn't only used for key value attributes any more maybe it should be renamed

}
}

private static <T> void captureKeyArrayValueAttribute(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be easier to write the converted values to a list.

  private static <T> void captureKeyArrayValueAttribute(
      AttributesBuilder attributes,
      AttributeKey<List<T>> key,
      Object array,
      Function<Object, T> extractor) {
    List<T> list = new ArrayList<>();
    int length = java.lang.reflect.Array.getLength(array);
    for (int i = 0; i < length; i++) {
      Object value = (T) java.lang.reflect.Array.get(array, i);
      if (value != null) {
        list.add(extractor.apply(value));
      }
    }
    // empty lists are not serialized
    if (!list.isEmpty()) {
      attributes.put(key, list);
    }
  }

} else if (value instanceof Double || value instanceof Float) {
attributes.put(keyStr, ((Number) value).doubleValue());
} else if (value.getClass().isArray()) {
if (value instanceof boolean[] || value instanceof Boolean[]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need a unit test for this method to ensure that all the array variations are tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test native This label can be applied to PRs to trigger them to run native tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

otel sdk unexpectedly extract log data
4 participants