Skip to content

fix(manager): set instance to 'unknow' for dynamic monitors on modification#4101

Open
pentium100 wants to merge 18 commits intoapache:masterfrom
pentium100:fix-modify-monitor-instance-bug
Open

fix(manager): set instance to 'unknow' for dynamic monitors on modification#4101
pentium100 wants to merge 18 commits intoapache:masterfrom
pentium100:fix-modify-monitor-instance-bug

Conversation

@pentium100
Copy link
Copy Markdown
Contributor

What's changed?

fix(manager): set instance to 'unknow' for dynamic monitors on modification

In the modifyMonitor method, the instance field for dynamically discovered monitors (where isStatic is false) was not being explicitly set to "unknow". This could lead to incorrect instance values or unexpected behavior when updating service discovery monitors.

This commit aligns the behavior of modifyMonitor with addMonitor by explicitly setting monitor.setInstance("unknow") for non-static scrape types before processing the instance and port fields.

refer to #4100

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Add or update API

  • I have added the necessary e2e tests and all cases have passed.

…cation

In the `modifyMonitor` method, the `instance` field for dynamically discovered monitors (where `isStatic` is false) was not being explicitly set to "unknow". This could lead to incorrect instance values or unexpected behavior when updating service discovery monitors.

This commit aligns the behavior of `modifyMonitor` with `addMonitor` by explicitly setting `monitor.setInstance("unknow")` for non-static scrape types before processing the instance and port fields.
…cation

In the `modifyMonitor` method, the `instance` field for dynamically discovered monitors (where `isStatic` is false) was not being explicitly set to "unknow". This could lead to incorrect instance values or unexpected behavior when updating service discovery monitors.

This commit aligns the behavior of `modifyMonitor` with `addMonitor` by explicitly setting `monitor.setInstance("unknow")` for non-static scrape types before processing the instance and port fields.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an exception when modifying service-discovery (non-static scrape) monitors by ensuring monitor.instance is set to a non-null placeholder before building job metadata (aligning modifyMonitor behavior with addMonitor). This addresses issue #4100 where Map.of(...) would throw due to a null instance.

Changes:

  • In modifyMonitor, compute isStatic earlier and set monitor.instance to a placeholder value for non-static scrapes.
  • Prevent null instance from reaching Map.of(...) during job metadata construction for service discovery monitors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 391 to 403
@@ -396,8 +402,6 @@ public void modifyMonitor(Monitor monitor, List<Param> params, String collector,
}
monitor.setInstance(instance);
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

modifyMonitor appends portWithMark whenever instance is non-null, even if instance already includes a ":port" suffix. This can produce duplicated ports (e.g., host:9200:9200) for API clients/imports that send monitor.instance with port included. Align this logic with addMonitor by checking IpDomainUtil.isHasPortWithMark(instance) before concatenating the port.

Copilot uses AI. Check for mistakes.
@yuluo-yx
Copy link
Copy Markdown
Member

hi @pentium100 pls see the copilot review comment.

When the monitor type is service discovery and the instance is not empty,
the instance value should not be overwritten. This commit fixes this issue.
Additionally, it corrects the spelling of 'unknow' to 'unknown' and prevents
appending the port if the instance already contains it.
zqr10159
zqr10159 previously approved these changes Mar 30, 2026
zqr10159
zqr10159 previously approved these changes Mar 31, 2026
@zqr10159 zqr10159 requested a review from Duansg March 31, 2026 08:06
Copy link
Copy Markdown
Member

@Duansg Duansg left a comment

Choose a reason for hiding this comment

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

In addition, please update the regression test cases for this change.

@pentium100 pentium100 requested a review from Duansg April 6, 2026 12:19
: SignConstants.DOUBLE_MARK + portParam.getParamValue();
? ""
: SignConstants.DOUBLE_MARK + portParam.getParamValue();
if (IpDomainUtil.isHasPortWithMark(instance)){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I took a look at the code, and I have a question: if monitor.setInstance("unknown") is true, will this line of code execute?

Conversely, if that's not the case, I've noticed that the value of instance doesn't include port information during editing, so this line of code would never execute?

Please help confirm this issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The instance is not edited directly on the frontend; instead, the host is assigned to the instance during submission, and the port parameter is appended on the backend to form the final instance. There are two scenarios:

  1. When the scrape type is "static," the frontend host is mandatory. The frontend assigns the host to the instance, and the backend appends the port to form the host:port format, which is then assigned to the instance.
  2. When the scrape type is "sd," the frontend host is empty, resulting in an empty instance. This is passed to the backend, but the modification logic does not handle this case, leading to an error at the following line:
Map<String, String> metadata = Map.of(CommonConstants.LABEL_INSTANCE_NAME, monitor.getName(),
CommonConstants.LABEL_INSTANCE, monitor.getInstance());

Therefore, we need to add a check: if this is an "sd" type monitor, set the instance to "unknown." Given the current situation, it is true that the instance cannot contain a port. Are you suggesting that we should append the port directly without checking if the instance already contains one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please thoroughly test all page functions before determining whether there is an issue here. And don’t just rely on AI assistance.

@Duansg
Copy link
Copy Markdown
Member

Duansg commented Apr 11, 2026

pls check ci

Removed the unnecessary check for port existence in the `instance`
string, as `instance` will not contain port information in this context.
@pentium100 pentium100 requested a review from Duansg April 11, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants