hcsop.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/cloudmon/options"
  25. "yunion.io/x/onecloud/pkg/util/influxdb"
  26. )
  27. type HcsOpCollect struct {
  28. SCollectByResourceIdDriver
  29. }
  30. func (self *HcsOpCollect) GetProvider() string {
  31. return api.CLOUD_PROVIDER_HCSOP
  32. }
  33. func init() {
  34. Register(&HcsOpCollect{})
  35. }
  36. func (self *HcsOpCollect) IsSupportMetrics() bool {
  37. return true
  38. }
  39. func (self *HcsOpCollect) CollectStorageMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.StorageDetails, start, end time.Time) error {
  40. metrics := []influxdb.SMetricData{}
  41. for _, storage := range res {
  42. metric := influxdb.SMetricData{
  43. Name: string(cloudprovider.METRIC_RESOURCE_TYPE_STORAGE),
  44. Timestamp: time.Now(),
  45. Tags: []influxdb.SKeyValue{},
  46. Metrics: []influxdb.SKeyValue{},
  47. }
  48. for k, v := range storage.GetMetricTags() {
  49. metric.Tags = append(metric.Tags, influxdb.SKeyValue{
  50. Key: k,
  51. Value: v,
  52. })
  53. }
  54. for k, v := range storage.GetMetricPairs() {
  55. metric.Metrics = append(metric.Metrics, influxdb.SKeyValue{
  56. Key: k,
  57. Value: v,
  58. })
  59. }
  60. metrics = append(metrics, metric)
  61. }
  62. return self.sendMetrics(ctx, manager, "storage", len(res), metrics)
  63. }
  64. func (self *HcsOpCollect) CollectWireMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.WireDetails, start, end time.Time) error {
  65. ch := make(chan struct{}, options.Options.CloudResourceCollectMetricsBatchCount)
  66. defer close(ch)
  67. metrics := []influxdb.SMetricData{}
  68. var wg sync.WaitGroup
  69. var mu sync.Mutex
  70. for i := range res {
  71. ch <- struct{}{}
  72. wg.Add(1)
  73. go func(wire api.WireDetails) {
  74. defer func() {
  75. wg.Done()
  76. <-ch
  77. }()
  78. opts := &cloudprovider.MetricListOptions{
  79. ResourceType: cloudprovider.METRIC_RESOURCE_TYPE_WIRE,
  80. StartTime: start,
  81. EndTime: end,
  82. }
  83. opts.ResourceId = wire.ExternalId
  84. tags := []influxdb.SKeyValue{}
  85. for k, v := range wire.GetMetricTags() {
  86. tags = append(tags, influxdb.SKeyValue{
  87. Key: k,
  88. Value: v,
  89. })
  90. }
  91. data, err := provider.GetMetrics(opts)
  92. if err != nil {
  93. if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
  94. log.Errorf("get rds %s(%s) error: %v", wire.Name, wire.Id, err)
  95. return
  96. }
  97. return
  98. }
  99. for _, values := range data {
  100. for _, value := range values.Values {
  101. metric := influxdb.SMetricData{
  102. Name: values.MetricType.Name(),
  103. Timestamp: value.Timestamp,
  104. Tags: tags,
  105. Metrics: []influxdb.SKeyValue{
  106. {
  107. Key: values.MetricType.Key(),
  108. Value: strconv.FormatFloat(value.Value, 'E', -1, 64),
  109. },
  110. },
  111. }
  112. for k, v := range value.Tags {
  113. metric.Tags = append(metric.Tags, influxdb.SKeyValue{
  114. Key: k,
  115. Value: v,
  116. })
  117. }
  118. mu.Lock()
  119. metrics = append(metrics, metric)
  120. mu.Unlock()
  121. }
  122. }
  123. }(res[i])
  124. }
  125. wg.Wait()
  126. return self.sendMetrics(ctx, manager, "wire", len(res), metrics)
  127. }