Skip to content

feat: improve diff logging #2544

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

Merged
merged 9 commits into from
Oct 10, 2024

Conversation

bachmanity1
Copy link
Contributor

@bachmanity1 bachmanity1 commented Oct 2, 2024

Based on this comment.

Diff is calculated and printed only if there is a mismatch between actual and desired state and logger is enabled for the debug level. Here is the sample output:

2024-10-02 16:38:42,233 1 i.j.o.p.d.k.SSABasedGenericKubernetesResourceMatcher [DEBUG] Diff between actual and desired state for resource: ConfigMap with name: test1 in namespace: default is: 
--- 
+++ 
@@ -2,3 +2,3 @@
 data:
-  key1: "val1"
+  key1: "different value"
 metadata:

@openshift-ci openshift-ci bot requested review from adam-sandor and csviri October 2, 2024 07:43
@bachmanity1 bachmanity1 changed the title Improve diff logging feat: improve diff logging Oct 2, 2024
@bachmanity1 bachmanity1 force-pushed the improve-diff-logging branch from d1e477b to 03e5c5e Compare October 2, 2024 07:54
var matches = prunedActual.equals(desiredMap);

if (!matches && LoggingUtils.isNotSensitiveResource(desired)) {
var diff = getDiff(prunedActual, desiredMap, objectMapper);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The diff should be calculated only if debug enabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

The diff should be calculated only if debug enabled.

That's right, this costs a lot.

if (log.isDebugEnabled()) {
var actualYaml = serialization.asYaml(prunedActualMap);
var desiredYaml = serialization.asYaml(desiredMap);
log.debug("Pruned actual yaml: \n {} \n desired yaml: \n {} ", actualYaml, desiredYaml);
Copy link
Collaborator

@csviri csviri Oct 2, 2024

Choose a reason for hiding this comment

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

We still might want to log the actual resources. What do you think?

Copy link
Contributor Author

@bachmanity1 bachmanity1 Oct 2, 2024

Choose a reason for hiding this comment

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

when should we log actual resources?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean it is good that we log the diff, but some might be interested in the actual resources (I mean both "actual" and desired yaml). It might be useful to log that too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think?

how about logging actual resource in case when logger is enabled for the trace level? or should we introduce new configuration parameter?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Trace level is fine by me, we could have there both the resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I enabled printing actual and desired resources when trace is enabled.

@bachmanity1 bachmanity1 force-pushed the improve-diff-logging branch from b116919 to 611ecdb Compare October 2, 2024 08:44
@@ -58,7 +58,6 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if it is right to include assertj as a dependency when build time rather than in a test environment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, if this could be done nicer, would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if I should remove the last commit or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

possible alternative is to use https://github.com/java-diff-utils/java-diff-utils

This looks good to me, the library itself doesn't seem heavy.
It looks like there may be another opportunity to use this library for other purposes in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I was going to comment on using this lib instead of assertj. Thanks!

Signed-off-by: bachmanity1 <[email protected]>
Comment on lines 125 to 171
var actualYaml = serialization.asYaml(sortMap(prunedActualMap));
var desiredYaml = serialization.asYaml(sortMap(desiredMap));
if (log.isTraceEnabled()) {
log.trace("Pruned actual resource: \n {} \ndesired resource: \n {} ", actualYaml,
desiredYaml);
}

var patch = DiffUtils.diff(actualYaml.lines().toList(), desiredYaml.lines().toList());
List<String> unifiedDiff =
UnifiedDiffUtils.generateUnifiedDiff("", "", actualYaml.lines().toList(), patch, 1);
return String.join("\n", unifiedDiff);
}

@SuppressWarnings("unchecked")
private Map<String, Object> sortMap(Map<String, Object> map) {
List<String> sortedKeys = new ArrayList<>(map.keySet());
Collections.sort(sortedKeys);

Map<String, Object> sortedMap = new LinkedHashMap<>();
for (String key : sortedKeys) {
Object value = map.get(key);
if (value instanceof Map) {
Map<String, Object> nestedMap = (Map<String, Object>) value;
sortedMap.put(key, sortMap(nestedMap));
} else if (value instanceof List) {
sortedMap.put(key, sortList((List<?>) value));
} else {
sortedMap.put(key, value);
}
}
return sortedMap;
}

@SuppressWarnings("unchecked")
private List<?> sortList(List<?> list) {
List<Object> sortedList = new ArrayList<>();
for (Object item : list) {
if (item instanceof Map) {
Map<String, Object> mapItem = (Map<String, Object>) item;
sortedList.add(sortMap(mapItem));
} else if (item instanceof List) {
sortedList.add(sortList((List<?>) item));
} else {
sortedList.add(item);
}
}
return sortedList;
Copy link
Contributor

Choose a reason for hiding this comment

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

What if you could create a separate utility class like DiffGenerator and write unit tests for it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry, I was planning to separate this into util class but I got a little busy lately.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Np, feel free to create a separate PR. thx

@metacosm
Copy link
Collaborator

metacosm commented Oct 2, 2024

LGTM but would indeed like to see some tests.

}

@SuppressWarnings("unchecked")
private Map<String, Object> sortMap(Map<String, Object> map) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, for this pls add unit tests.

Copy link
Collaborator

@csviri csviri left a comment

Choose a reason for hiding this comment

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

LGTM thx!

@csviri csviri requested a review from metacosm October 10, 2024 06:37
Copy link
Collaborator

@metacosm metacosm left a comment

Choose a reason for hiding this comment

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

Thank you!

@csviri csviri merged commit 5585765 into operator-framework:next Oct 10, 2024
19 of 20 checks passed
metacosm pushed a commit that referenced this pull request Oct 10, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
@bachmanity1 bachmanity1 deleted the improve-diff-logging branch October 10, 2024 12:01
metacosm pushed a commit that referenced this pull request Nov 5, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
metacosm pushed a commit that referenced this pull request Nov 6, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
csviri pushed a commit that referenced this pull request Nov 13, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
metacosm pushed a commit that referenced this pull request Nov 19, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
metacosm pushed a commit that referenced this pull request Nov 20, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
metacosm pushed a commit that referenced this pull request Nov 27, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
csviri pushed a commit that referenced this pull request Dec 6, 2024
* imporve diff logging

Signed-off-by: bachmanity1 <[email protected]>

* compute diff only when actual doesn't match desired

Signed-off-by: bachmanity1 <[email protected]>

* slight improvements

Signed-off-by: bachmanity1 <[email protected]>

* increase context size

Signed-off-by: bachmanity1 <[email protected]>

* fix style

Signed-off-by: bachmanity1 <[email protected]>

* calculate diff only if debug is enabled

Signed-off-by: bachmanity1 <[email protected]>

* print actual resources when trace is enabled

Signed-off-by: bachmanity1 <[email protected]>

* use java-diff-utils

Signed-off-by: bachmanity1 <[email protected]>

* add unit tests

---------

Signed-off-by: bachmanity1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants