support prometheus sd (#9518)
This commit is contained in:
parent
8292d2ccb1
commit
4bd5b7c85a
@ -50,6 +50,10 @@
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-istio</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-prometheus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- log -->
|
||||
<!-- log4j通过slf4j来代理 -->
|
||||
|
@ -189,4 +189,5 @@ nacos.istio.mcp.server.enabled=false
|
||||
# nacos.core.protocol.raft.data.read_index_type=ReadOnlySafe
|
||||
### rpc request timeout, default 5 seconds
|
||||
# nacos.core.protocol.raft.data.rpc_request_timeout_ms=5000
|
||||
|
||||
### enable to support prometheus service discovery
|
||||
#nacos.prometheus.metrics.enabled=true
|
||||
|
@ -225,3 +225,6 @@ nacos.istio.mcp.server.enabled=false
|
||||
|
||||
### Distro data load retry delay when load snapshot data failed, default 30 seconds.
|
||||
# nacos.core.protocol.distro.data.load.retryDelayMs=30000
|
||||
|
||||
### enable to support prometheus service discovery
|
||||
#nacos.prometheus.metrics.enabled=true
|
@ -230,3 +230,6 @@ nacos.istio.mcp.server.enabled=false
|
||||
|
||||
### Distro data load retry delay when load snapshot data failed, default 30 seconds.
|
||||
# nacos.core.protocol.distro.data.load.retryDelayMs=30000
|
||||
|
||||
### enable to support prometheus service discovery
|
||||
#nacos.prometheus.metrics.enabled=true
|
6
pom.xml
6
pom.xml
@ -596,6 +596,7 @@
|
||||
<module>sys</module>
|
||||
<module>plugin</module>
|
||||
<module>plugin-default-impl</module>
|
||||
<module>prometheus</module>
|
||||
</modules>
|
||||
|
||||
<!-- Default dependencies in all subprojects -->
|
||||
@ -699,6 +700,11 @@
|
||||
<artifactId>nacos-istio</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-prometheus</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-consistency</artifactId>
|
||||
|
79
prometheus/pom.xml
Normal file
79
prometheus/pom.xml
Normal file
@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright 1999-2021 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.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nacos-all</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>nacos-prometheus</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>nacos-prometheus ${project.version}</name>
|
||||
<url>http://nacos.io</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-naming</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>nacos-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.alibaba.nacos.prometheus.PrometheusApp</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.prometheus;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* PrometheusApp starter.
|
||||
*
|
||||
* @author karsonto
|
||||
*/
|
||||
@EnableScheduling
|
||||
@SpringBootApplication(scanBasePackages = {"com.alibaba.nacos"})
|
||||
public class PrometheusApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PrometheusApp.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.prometheus.api;
|
||||
|
||||
/**
|
||||
* Api Constants.
|
||||
*
|
||||
* @author karsonto
|
||||
*/
|
||||
public class ApiConstants {
|
||||
|
||||
public static final String PROMETHEUS_CONTROLLER_PATH = "/prometheus";
|
||||
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.prometheus.controller;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.alibaba.nacos.naming.core.InstanceOperatorClientImpl;
|
||||
import com.alibaba.nacos.naming.core.v2.ServiceManager;
|
||||
import com.alibaba.nacos.naming.core.v2.pojo.Service;
|
||||
import com.alibaba.nacos.prometheus.api.ApiConstants;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
|
||||
/**
|
||||
* Support Prometheus SD Controller.
|
||||
*
|
||||
* @author karsonto
|
||||
*/
|
||||
@RestController
|
||||
@ConditionalOnProperty(name = "nacos.prometheus.metrics.enabled", havingValue = "true")
|
||||
public class PrometheusController {
|
||||
|
||||
@Autowired
|
||||
private InstanceOperatorClientImpl instanceServiceV2;
|
||||
|
||||
private final ServiceManager serviceManager;
|
||||
|
||||
public PrometheusController() {
|
||||
this.serviceManager = ServiceManager.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all service instances.
|
||||
*
|
||||
* @throws NacosException NacosException.
|
||||
*/
|
||||
@GetMapping(value = ApiConstants.PROMETHEUS_CONTROLLER_PATH, produces = "application/json; charset=UTF-8")
|
||||
public ResponseEntity metric() throws NacosException {
|
||||
ArrayNode arrayNode = JacksonUtils.createEmptyArrayNode();
|
||||
Set<Instance> targetSet = new HashSet<>();
|
||||
Set<String> allNamespaces = serviceManager.getAllNamespaces();
|
||||
for (String namespace : allNamespaces) {
|
||||
Set<Service> singletons = serviceManager.getSingletons(namespace);
|
||||
for (Service service : singletons) {
|
||||
|
||||
List<? extends Instance> instances = instanceServiceV2.listAllInstances(namespace, service.getName());
|
||||
|
||||
for (Instance instance : instances) {
|
||||
targetSet.add(instance);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Map<String, List<Instance>> groupingInsMap = targetSet.stream().collect(groupingBy(Instance::getClusterName));
|
||||
groupingInsMap.forEach((key, value) -> {
|
||||
ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode();
|
||||
ArrayNode targetsNode = JacksonUtils.createEmptyArrayNode();
|
||||
ObjectNode labelNode = JacksonUtils.createEmptyJsonNode();
|
||||
value.forEach(e -> {
|
||||
targetsNode.add(e.getIp() + ":" + e.getPort());
|
||||
});
|
||||
labelNode.put("__meta_clusterName", key);
|
||||
jsonNode.replace("targets", targetsNode);
|
||||
jsonNode.replace("labels", labelNode);
|
||||
arrayNode.add(jsonNode);
|
||||
|
||||
});
|
||||
return ResponseEntity.ok().body(arrayNode.toString());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user