metric_dbinit.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 dbinit
  15. import (
  16. "yunion.io/x/log"
  17. "yunion.io/x/onecloud/pkg/apis"
  18. "yunion.io/x/onecloud/pkg/apis/monitor"
  19. "yunion.io/x/onecloud/pkg/monitor/dbinit/measurements"
  20. )
  21. var MetricNeedDeleteDescriptions = []string{}
  22. var metricInitInputMap map[string]monitor.MetricCreateInput
  23. func RegistryMetricCreateInput(name, displayName, resType, database string, score int,
  24. fields []monitor.MetricFieldCreateInput) {
  25. if metricInitInputMap == nil {
  26. metricInitInputMap = make(map[string]monitor.MetricCreateInput)
  27. }
  28. if _, ok := metricInitInputMap[name]; ok {
  29. log.Fatalf("inputMeasurementName: %q has already existed.", name)
  30. return
  31. }
  32. metricInitInputMap[name] = monitor.MetricCreateInput{
  33. Measurement: monitor.MetricMeasurementCreateInput{
  34. StandaloneResourceCreateInput: apis.StandaloneResourceCreateInput{Name: name},
  35. ResType: resType,
  36. DisplayName: displayName,
  37. Database: database,
  38. Score: score,
  39. },
  40. MetricFields: fields,
  41. }
  42. }
  43. func GetRegistryMetricInput() (metricInitInputs []monitor.MetricCreateInput) {
  44. if metricInitInputMap == nil {
  45. metricInitInputMap = make(map[string]monitor.MetricCreateInput)
  46. }
  47. for name := range metricInitInputMap {
  48. metricInitInputs = append(metricInitInputs, metricInitInputMap[name])
  49. }
  50. return
  51. }
  52. func newMetricFieldCreateInput(name, displayName, unit string, score int) monitor.MetricFieldCreateInput {
  53. return monitor.MetricFieldCreateInput{
  54. StandaloneResourceCreateInput: apis.StandaloneResourceCreateInput{Name: name},
  55. DisplayName: displayName,
  56. Unit: unit,
  57. ValueType: "",
  58. Score: score,
  59. }
  60. }
  61. // order by score asc
  62. // score default:99
  63. func init() {
  64. var measurements = measurements.All
  65. metricCount := 0
  66. scoreIdx := 0
  67. for mIdx := range measurements {
  68. measurement := measurements[mIdx]
  69. for ctxIdx := range measurement.Context {
  70. ctx := measurement.Context[ctxIdx]
  71. inputs := []monitor.MetricFieldCreateInput{}
  72. for metricIdx := range measurement.Metrics {
  73. metric := measurement.Metrics[metricIdx]
  74. inputs = append(inputs, newMetricFieldCreateInput(metric.Name, metric.DisplayName, metric.Unit, metricIdx+1))
  75. metricCount++
  76. }
  77. RegistryMetricCreateInput(ctx.Name, ctx.DisplayName, ctx.ResourceType, ctx.Database, scoreIdx+1, inputs)
  78. scoreIdx++
  79. }
  80. }
  81. log.Infof("[monitor.dbinit] Register %d metrics", metricCount)
  82. }