da_top_percentile.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include "da_top_percentile.h"
  20. #include "da_core.h"
  21. #include "da_server.h"
  22. #define ADDR_LEN 16
  23. #define MSG_SIZE 1024
  24. int8_t get_host_name_info(const char *addr, char *result)
  25. {
  26. struct addrinfo *answer;
  27. struct addrinfo *curr;
  28. struct addrinfo hint;
  29. struct sockaddr_in *sk_addr;
  30. char szIP[ADDR_LEN];
  31. bzero(&hint, sizeof(hint));
  32. hint.ai_family = AF_INET;
  33. hint.ai_socktype = SOCK_DGRAM;
  34. int8_t ret = getaddrinfo(addr, NULL, &hint, &answer);
  35. if(ret != 0)
  36. return -1;
  37. //正常情况应该是只有一个IP,所以只取第一个
  38. for(curr = answer; curr != NULL; curr = curr->ai_next)
  39. {
  40. sk_addr = (struct sockaddr_in *)(curr->ai_addr);
  41. inet_ntop(AF_INET, &(sk_addr->sin_addr), szIP, ADDR_LEN);
  42. break;
  43. }
  44. freeaddrinfo(answer);
  45. strncpy(result, szIP, ADDR_LEN);
  46. log_debug("remote ip is : [%s]", szIP);
  47. return 0;
  48. }
  49. int8_t set_remote_config(const char *addr, uint16_t port, struct sockaddr_in *remote_addr)
  50. {
  51. if(NULL == addr)
  52. return -1;
  53. if(0 == port)
  54. return -1;
  55. if(NULL == remote_addr)
  56. return -1;
  57. int8_t ret = 0;
  58. //因为不使用域名,addr 直接为IP,所以不再进行get_host_name_info转换
  59. /*
  60. char szIP[ADDR_LEN];
  61. memset(szIP, 0, ADDR_LEN);
  62. ret = get_host_name_info(addr, szIP);
  63. if(0 != ret)
  64. return -1;
  65. */
  66. bzero((void*)remote_addr, sizeof(*remote_addr));
  67. ret = inet_pton(AF_INET, addr, &(remote_addr->sin_addr));
  68. if(1 != ret)
  69. return -1;
  70. remote_addr->sin_family = AF_INET;
  71. remote_addr->sin_port = htons(port);
  72. return 0;
  73. }
  74. int8_t set_remote_param(uint64_t app_id, uint64_t interface_id, enum E_REPORT_TYPE type, struct remote_param *pParam)
  75. {
  76. if(NULL == pParam)
  77. return -1;
  78. if((int8_t)type <= (int8_t)RT_MIN || (int8_t)type >= (int8_t)RT_MAX)
  79. return -1;
  80. pParam[(int8_t)type - 1].app_id = app_id;
  81. pParam[(int8_t)type - 1].interface_id = interface_id;
  82. return 0;
  83. }
  84. static int build_remote_socket()
  85. {
  86. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  87. if(fd < 0)
  88. return -1;
  89. return fd;
  90. }
  91. int set_remote_fd()
  92. {
  93. int fd = build_remote_socket();
  94. if(fd < 0)
  95. return -1;
  96. return fd;
  97. }
  98. void top_percentile_report(struct context *ctx, struct server_pool *pool, int64_t elaspe, int32_t status, enum E_REPORT_TYPE type)
  99. {
  100. if(NULL == ctx || NULL == pool)
  101. {
  102. log_debug("ctx or pool is null!");
  103. return;
  104. }
  105. if((int8_t)type <= (int8_t)RT_MIN || (int8_t)type >= (int8_t)RT_MAX)
  106. {
  107. log_debug("the type <= RT_MIN or the type >= RT_MAX");
  108. return;
  109. }
  110. if(0 == pool->top_percentile_enable)
  111. {
  112. log_debug("the top_percentile_enable is 0");
  113. return;
  114. }
  115. if(pool->top_percentile_fd < 0)
  116. {
  117. log_debug("the top_percentile_fd < 0");
  118. return;
  119. }
  120. if(0 == pool->top_percentile_addr_len)
  121. {
  122. log_debug("the top_percentile_addr_len is 0");
  123. return;
  124. }
  125. if(NULL == pool->top_percentile_param)
  126. {
  127. log_debug("the top_percentile_param is null");
  128. return;
  129. }
  130. char szMsg[MSG_SIZE];
  131. memset(szMsg, 0, MSG_SIZE);
  132. int off = snprintf(szMsg, sizeof(szMsg), "{\"app_id\":%"PRIu64",\"interface_id\":%"PRIu64",\"call_time\":%"PRIu64",\"status\":%d}", pool->top_percentile_param[(int8_t)type - 1].app_id, pool->top_percentile_param[(int8_t)type - 1].interface_id, elaspe, status);
  133. sendto(pool->top_percentile_fd, szMsg, off, 0, (const struct sockaddr *)&(pool->top_percentile_addr), pool->top_percentile_addr_len);
  134. log_debug("send msg is : [%s]", szMsg);
  135. }