Skip to content

Commit

Permalink
Merge branch 'zp-prometheus-metrics' into '3.1.9-com'
Browse files Browse the repository at this point in the history
增加Prometheus指标接口

See merge request DataTech/dolphinscheduler!2
  • Loading branch information
zhaopink committed Jan 4, 2024
2 parents a13a51d + e634673 commit d90c003
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void addInterceptors(InterceptorRegistry registry) {
.addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN)
.excludePathPatterns(LOGIN_PATH_PATTERN, REGISTER_PATH_PATTERN,
"/swagger-resources/**", "/webjars/**", "/v3/api-docs/**", "/api-docs/**",
"/doc.html", "/swagger-ui/**", "*.html", "/ui/**", "/error");
"/doc.html", "/swagger-ui/**", "*.html", "/ui/**", "/error", "/service-node/**");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.SAVE_ERROR;

import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.ServiceNodeService;
import org.apache.dolphinscheduler.api.utils.Result;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* service node controller
* @author liyou
*/
@Api(tags = "SERVICE_NODE_TAG")
@RestController
@RequestMapping("service-node")
public class ServiceNodeController extends BaseController {

@Autowired
private ServiceNodeService serviceNodeService;

/**
* query all Service Node
*
* @return service node map
*/
@ApiOperation(value = "queryAllServiceNode", notes = "QUERY_ALL_SERVICE_NODE_NOTES")
@GetMapping("queryAllServiceNode")
@ResponseStatus(HttpStatus.OK)
public Result queryAllServiceNode() {
return serviceNodeService.queryAllServiceNode();
}

/**
* get Prometheus metrics address list
*
* @return {@code Object}
*/
@ApiOperation(value = "getPrometheusMetricsAddressList", notes = "GET_PROMETHEUS_METRICS_ADDRESS_LIST_NOTES")
@GetMapping("getPrometheusMetricsAddressList")
@ResponseStatus(HttpStatus.OK)
public Object getPrometheusMetricsAddressList() {
return serviceNodeService.getPrometheusMetricsAddressList();
}

/**
* upsert service node
*
* @param name worker group name
* @param addrList addr list
* @return result
*/
@ApiOperation(value = "upsertServiceNode", notes = "UPSERT_SERVICE_NODE_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "SERVICE_NAME", required = true, dataType = "String", allowableValues = "master,api,alert"),
@ApiImplicitParam(name = "addrList", value = "SERVICE_ADDR_LIST", required = true, dataType = "String", example = "192.168.1.1:123,192.168.1.2:123"),
})
@PostMapping()
@ResponseStatus(HttpStatus.OK)
@ApiException(SAVE_ERROR)
public Result upsertServiceNode(@RequestParam(value = "name") String name,
@RequestParam(value = "addrList") String addrList) {
Map<String, Object> result = serviceNodeService.upsertServiceNode(name, addrList);
return returnDataList(result);
}

@ApiOperation(value = "deleteServiceNode", notes = "DELETE_SERVICE_NODE_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "SERVICE_NAME", required = true, dataType = "String", allowableValues = "master,api,alert"),
})
@DeleteMapping()
@ResponseStatus(HttpStatus.OK)
public Result deleteServiceNode(@RequestParam(value = "name") String name) {
Map<String, Object> result = serviceNodeService.deleteServiceNode(name);
return returnDataList(result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.apache.dolphinscheduler.api.dto;

import java.util.Collection;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author liyou 2024-01-02
*/
public class ServiceNodeDto {

@Data
public static class PrometheusMetricsAddress {

private Collection<String> targets;
private Label labels;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Label {
private String group;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.api.service;

import org.apache.dolphinscheduler.api.utils.Result;

import java.util.List;
import java.util.Map;

/**
* service node service
*
* @author liyou 2023/12/29
*/
public interface ServiceNodeService {

/**
* queryAllServiceNode
*
* @return {@code Result<Object>}
*/
Result<Object> queryAllServiceNode();

/**
* getPrometheusMetricsAddressList
*
* @return {@code List<Object>}
*/
List<Object> getPrometheusMetricsAddressList();

/**
* upsertServiceNode
*
* @param serviceName serviceName
* @param addressList addressList
* @return {@code Map<String, Object>}
*/
Map<String, Object> upsertServiceNode(String serviceName, String addressList);

/**
* deleteServiceNode
*
* @param serviceName serviceName
* @return {@code Map<String, Object>}
*/
Map<String, Object> deleteServiceNode(String serviceName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.apache.dolphinscheduler.api.service.impl;

import org.apache.dolphinscheduler.api.dto.ServiceNodeDto;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.ServiceNodeService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.ServiceNode;
import org.apache.dolphinscheduler.dao.mapper.ServiceNodeMapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* @author liyou 2023-12-29
*/
@Service
public class ServiceNodeServiceImpl extends BaseServiceImpl implements ServiceNodeService {

@Autowired
private ServiceNodeMapper serviceNodeMapper;

@Override
public Result<Object> queryAllServiceNode() {
List<ServiceNode> serviceNodes = serviceNodeMapper.queryAllServiceNode();
serviceNodes.forEach(node -> node
.setAddrList(Arrays.stream(node.getAddrList().split(",")).distinct().collect(Collectors.joining(","))));
return Result.success(serviceNodes);
}

@Override
public List<Object> getPrometheusMetricsAddressList() {
List<Object> result = new ArrayList<>();
List<ServiceNode> serviceNodes = serviceNodeMapper.queryAllServiceNode();
for (ServiceNode node : serviceNodes) {
ServiceNodeDto.PrometheusMetricsAddress address = new ServiceNodeDto.PrometheusMetricsAddress();
address.setTargets(Arrays.stream(node.getAddrList().split(",")).distinct().collect(Collectors.toList()));
address.setLabels(new ServiceNodeDto.Label(node.getServiceName()));
result.add(address);
}
return result;
}

@Override
public Map<String, Object> upsertServiceNode(String serviceName, String addressList) {
Map<String, Object> result = new HashMap<>();
ServiceNode serviceNode = serviceNodeMapper
.selectOne(new QueryWrapper<ServiceNode>().lambda().eq(ServiceNode::getServiceName, serviceName));
if (serviceNode != null) {
serviceNode.setAddrList(addressList);
serviceNodeMapper.updateById(serviceNode);
} else {
ServiceNode node = new ServiceNode();
node.setServiceName(serviceName);
node.setAddrList(addressList);
serviceNodeMapper.insert(node);
}
putMsg(result, Status.SUCCESS);
return result;
}

@Override
public Map<String, Object> deleteServiceNode(String serviceName) {
Map<String, Object> result = new HashMap<>();
ServiceNode serviceNode = serviceNodeMapper.selectOne(new QueryWrapper<ServiceNode>().lambda().eq(ServiceNode::getServiceName, serviceName));
if (serviceNode != null) {
serviceNodeMapper.deleteById(serviceNode.getId());
}
putMsg(result, Status.SUCCESS);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.dao.entity;

import lombok.Data;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

/**
* service node
*
* @author liyou 2023/12/29
*/
@TableName("t_ds_service_node")
@Data
public class ServiceNode {

@TableId(value = "id", type = IdType.AUTO)
private Integer id;

private String serviceName;

private String addrList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.dao.mapper;

import org.apache.dolphinscheduler.dao.entity.ServiceNode;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* service node mapper interface
*
* @author liyou 2023/12/29
*/
public interface ServiceNodeMapper extends BaseMapper<ServiceNode> {

/**
* query all service node
*
* @return worker group list
*/
List<ServiceNode> queryAllServiceNode();
}
Loading

0 comments on commit d90c003

Please sign in to comment.