-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add client labels collect and attach labels to conn (#11844)
* add client labels collect and attach labels to conn * remove some constants * fixup test case
- Loading branch information
Showing
25 changed files
with
1,256 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
common/src/main/java/com/alibaba/nacos/common/labels/LabelsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.common.labels; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* LabelsCollector. | ||
* | ||
* @author rong | ||
* @date 2024/2/4 | ||
*/ | ||
public interface LabelsCollector { | ||
|
||
/** | ||
* init labels. | ||
* | ||
* @param properties Properties | ||
* @date 2024/2/4 | ||
* @description init labels | ||
*/ | ||
void init(Properties properties); | ||
|
||
/** | ||
* getLabels. | ||
* | ||
* @return Map | ||
* @date 2024/2/4 | ||
* @description get all labels | ||
*/ | ||
Map<String, String> getLabels(); | ||
|
||
/** | ||
* getOrder. | ||
* | ||
* @return the order value | ||
* @date 2024/2/4 | ||
* @description get order value of labels in case of multiple labels | ||
*/ | ||
int getOrder(); | ||
|
||
/** | ||
* get collector name. | ||
* | ||
* @return name of collector | ||
* @date 2024/2/4 | ||
* @description name of collector | ||
*/ | ||
String getName(); | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
common/src/main/java/com/alibaba/nacos/common/labels/LabelsCollectorManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.common.labels; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* LabelsCollectorManager. | ||
* | ||
* @author rong | ||
* @date 2024/2/4 | ||
*/ | ||
public interface LabelsCollectorManager { | ||
|
||
/** | ||
* get all labels collected. | ||
* | ||
* @date 2024/2/4 | ||
* @description | ||
* @return all labels | ||
*/ | ||
Map<String, String> getAllLabels(); | ||
|
||
/** | ||
* refresh all labels. | ||
* | ||
* @date 2024/3/7 | ||
* @param properties Properties. | ||
* @return all labels. | ||
*/ | ||
Map<String, String> refreshAllLabels(Properties properties); | ||
} |
44 changes: 44 additions & 0 deletions
44
common/src/main/java/com/alibaba/nacos/common/labels/impl/AbstractLabelsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.common.labels.impl; | ||
|
||
import com.alibaba.nacos.common.labels.LabelsCollector; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* AbstractLabelsCollector. | ||
* | ||
* @author rong | ||
*/ | ||
public abstract class AbstractLabelsCollector implements LabelsCollector { | ||
|
||
protected Map<String, String> labels = new HashMap<>(2); | ||
|
||
private static final int DEFAULT_INITIAL_ORDER = 100; | ||
|
||
@Override | ||
public Map<String, String> getLabels() { | ||
return labels; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return DEFAULT_INITIAL_ORDER; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
common/src/main/java/com/alibaba/nacos/common/labels/impl/DefaultLabelsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.common.labels.impl; | ||
|
||
import com.alibaba.nacos.api.common.Constants; | ||
import com.alibaba.nacos.common.labels.LabelsCollector; | ||
import com.alibaba.nacos.common.labels.impl.utils.ConfigGetterManager; | ||
import com.alibaba.nacos.common.utils.ConnLabelsUtils; | ||
import com.alibaba.nacos.common.utils.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* DefaultLabelsCollector. | ||
* | ||
* @author rong | ||
*/ | ||
public class DefaultLabelsCollector extends AbstractLabelsCollector implements LabelsCollector { | ||
|
||
protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultLabelsCollector.class); | ||
|
||
private final String customName = "defaultLabelsCollector"; | ||
|
||
/** | ||
* init labels. | ||
* | ||
* @date 2024/2/4 | ||
*@description will init from properties, JVM OPTIONS, ENV by order of <tt>properties > JVM OPTIONS > ENV</tt> by default. | ||
* which will use the next level value when the current level value isn't setup (you also can set the level by env). | ||
* <p>eg: if the value of "nacos.app.conn.labels"(properties' key) is "k1=v1,k2=v2"(properties' value), the result will be | ||
* a Map with value{k1=v1,k2=v2}.</p> | ||
* @param properties Properties | ||
*/ | ||
@Override | ||
public void init(Properties properties) { | ||
ConfigGetterManager configGetterManager = new ConfigGetterManager(properties); | ||
labels.putAll(ConnLabelsUtils.parseRawLabels(configGetterManager.getConfig(Constants.APP_CONN_LABELS_PREFIX))); | ||
|
||
String grayLabelValue = configGetterManager.getConfig(Constants.CONFIG_GRAY); | ||
if (StringUtils.isNotBlank(grayLabelValue)) { | ||
labels.put(Constants.GRAY, grayLabelValue); | ||
} | ||
for (Map.Entry<String, String> entry : labels.entrySet()) { | ||
LOGGER.info("init labels: {}={}", entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return customName; | ||
} | ||
} |
Oops, something went wrong.