apsara.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package providerdriver
  15. import (
  16. "context"
  17. "strconv"
  18. "sync"
  19. "time"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/util/influxdb"
  25. )
  26. type ApsaraCollect struct {
  27. SCollectByMetricTypeDriver
  28. }
  29. func (self *ApsaraCollect) GetProvider() string {
  30. return api.CLOUD_PROVIDER_APSARA
  31. }
  32. func (self *ApsaraCollect) IsSupportMetrics() bool {
  33. return true
  34. }
  35. func init() {
  36. Register(&ApsaraCollect{})
  37. }
  38. func (self *ApsaraCollect) CollectEipMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ElasticipDetails, start, end time.Time) error {
  39. metrics := []influxdb.SMetricData{}
  40. var wg sync.WaitGroup
  41. var mu sync.Mutex
  42. for _, _metricType := range cloudprovider.ALL_EIP_TYPES {
  43. wg.Add(1)
  44. go func(metricType cloudprovider.TMetricType) {
  45. defer func() {
  46. wg.Done()
  47. }()
  48. opts := &cloudprovider.MetricListOptions{
  49. ResourceType: cloudprovider.METRIC_RESOURCE_TYPE_EIP,
  50. MetricType: metricType,
  51. StartTime: start,
  52. EndTime: end,
  53. }
  54. data, err := provider.GetMetrics(opts)
  55. if err != nil {
  56. if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
  57. log.Errorf("get eip %s(%s) %s error: %v", manager.Name, manager.Id, metricType, err)
  58. return
  59. }
  60. return
  61. }
  62. for _, value := range data {
  63. eip, ok := res[value.Id]
  64. if !ok {
  65. continue
  66. }
  67. tags := []influxdb.SKeyValue{}
  68. for k, v := range eip.GetMetricTags() {
  69. tags = append(tags, influxdb.SKeyValue{
  70. Key: k,
  71. Value: v,
  72. })
  73. }
  74. for _, v := range value.Values {
  75. metric := influxdb.SMetricData{
  76. Name: value.MetricType.Name(),
  77. Timestamp: v.Timestamp,
  78. Tags: []influxdb.SKeyValue{},
  79. Metrics: []influxdb.SKeyValue{
  80. {
  81. Key: value.MetricType.Key(),
  82. Value: strconv.FormatFloat(v.Value, 'E', -1, 64),
  83. },
  84. },
  85. }
  86. for k, v := range v.Tags {
  87. metric.Tags = append(metric.Tags, influxdb.SKeyValue{
  88. Key: k,
  89. Value: v,
  90. })
  91. }
  92. metric.Tags = append(metric.Tags, tags...)
  93. mu.Lock()
  94. metrics = append(metrics, metric)
  95. mu.Unlock()
  96. }
  97. }
  98. }(_metricType)
  99. }
  100. wg.Wait()
  101. return self.sendMetrics(ctx, manager, "elasticip", len(res), metrics)
  102. }