|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/def" |
| 9 | + "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ces/v1/model" |
| 10 | + |
| 11 | + "github.com/huaweicloud/cloudeye-exporter/logs" |
| 12 | +) |
| 13 | + |
| 14 | +var dayuInfo serversInfo |
| 15 | + |
| 16 | +type DayuInfo struct{} |
| 17 | + |
| 18 | +func (getter DayuInfo) GetResourceInfo() (map[string]labelInfo, []model.MetricInfoList) { |
| 19 | + resourceInfos := map[string]labelInfo{} |
| 20 | + filterMetrics := make([]model.MetricInfoList, 0) |
| 21 | + dayuInfo.Lock() |
| 22 | + defer dayuInfo.Unlock() |
| 23 | + if dayuInfo.LabelInfo == nil || time.Now().Unix() > dayuInfo.TTL { |
| 24 | + streams, err := getAllStreams() |
| 25 | + if err != nil { |
| 26 | + logs.Logger.Error("Get all dis Streams error:", err.Error()) |
| 27 | + return dayuInfo.LabelInfo, dayuInfo.FilterMetrics |
| 28 | + } |
| 29 | + |
| 30 | + sysConfigMap := getMetricConfigMap("SYS.DAYU") |
| 31 | + for _, stream := range streams { |
| 32 | + if metricNames, ok := sysConfigMap["stream_id"]; ok { |
| 33 | + metrics := buildSingleDimensionMetrics(metricNames, "SYS.DAYU", "stream_id", stream.ID) |
| 34 | + filterMetrics = append(filterMetrics, metrics...) |
| 35 | + info := labelInfo{ |
| 36 | + Name: []string{"name", "epId"}, |
| 37 | + Value: []string{stream.Name, stream.EpId}, |
| 38 | + } |
| 39 | + keys, values := getTags(stream.Tags) |
| 40 | + info.Name = append(info.Name, keys...) |
| 41 | + info.Value = append(info.Value, values...) |
| 42 | + resourceInfos[GetResourceKeyFromMetricInfo(metrics[0])] = info |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + dayuInfo.LabelInfo = resourceInfos |
| 47 | + dayuInfo.FilterMetrics = filterMetrics |
| 48 | + dayuInfo.TTL = time.Now().Add(TTL).Unix() |
| 49 | + } |
| 50 | + return dayuInfo.LabelInfo, dayuInfo.FilterMetrics |
| 51 | +} |
| 52 | + |
| 53 | +type StreamInfo struct { |
| 54 | + Private bool `json:"private"` |
| 55 | + StreamID string `json:"stream_id"` |
| 56 | + StreamName string `json:"stream_name"` |
| 57 | + CreateTime int64 `json:"create_time"` |
| 58 | + RetentionPeriod int `json:"retention_period"` |
| 59 | + Status string `json:"status"` |
| 60 | + StreamType string `json:"stream_type"` |
| 61 | + DataType string `json:"data_type"` |
| 62 | + PartitionCount int `json:"partition_count"` |
| 63 | + Tags []Tag `json:"tags"` |
| 64 | + SysTags []Tag `json:"sys_tags"` |
| 65 | + AutoScaleEnabled bool `json:"auto_scale_enabled"` |
| 66 | + AutoScaleMinPartitionCount int `json:"auto_scale_min_partition_count"` |
| 67 | + AutoScaleMaxPartitionCount int `json:"auto_scale_max_partition_count"` |
| 68 | +} |
| 69 | + |
| 70 | +type ListStreamsResp struct { |
| 71 | + TotalNumber int `json:"total_number"` |
| 72 | + StreamNames []string `json:"stream_names"` |
| 73 | + StreamInfoList []StreamInfo `json:"stream_info_list"` |
| 74 | + HasMoreStreams bool `json:"has_more_streams"` |
| 75 | + HttpStatusCode int `json:"-"` |
| 76 | +} |
| 77 | + |
| 78 | +type ListStreamsRep struct { |
| 79 | + Limit string `json:"limit"` |
| 80 | + StartStreamName string `json:"start_stream_name"` |
| 81 | +} |
| 82 | + |
| 83 | +func genReqDefForListStreams() *def.HttpRequestDef { |
| 84 | + reqDefBuilder := def.NewHttpRequestDefBuilder().WithMethod(http.MethodGet).WithPath("/v2/{project_id}/streams"). |
| 85 | + WithResponse(new(ListStreamsResp)).WithContentType("application/json") |
| 86 | + |
| 87 | + reqDefBuilder.WithRequestField(def.NewFieldDef().WithName("Limit").WithJsonTag("limit").WithLocationType(def.Query)) |
| 88 | + reqDefBuilder.WithRequestField(def.NewFieldDef().WithName("StartStreamName").WithJsonTag("start_stream_name").WithLocationType(def.Query)) |
| 89 | + return reqDefBuilder.Build() |
| 90 | +} |
| 91 | + |
| 92 | +func listStreams() ([]StreamInfo, error) { |
| 93 | + disClient := getHcClient(getEndpoint("dis", "v2")) |
| 94 | + request := &ListStreamsRep{Limit: "100"} |
| 95 | + var streams []StreamInfo |
| 96 | + for { |
| 97 | + resp, err := disClient.Sync(request, genReqDefForListStreams()) |
| 98 | + if err != nil { |
| 99 | + logs.Logger.Errorf("Failed to get list streams : %s", err.Error()) |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + response, ok := resp.(*ListStreamsResp) |
| 103 | + if !ok { |
| 104 | + err := errors.New("resp type is not ServiceDetail") |
| 105 | + logs.Logger.Errorf("Failed to get list streams : %s", err.Error()) |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + streams = append(streams, response.StreamInfoList...) |
| 109 | + if !response.HasMoreStreams { |
| 110 | + break |
| 111 | + } |
| 112 | + request.StartStreamName = response.StreamNames[len(response.StreamNames)-1] |
| 113 | + } |
| 114 | + return streams, nil |
| 115 | +} |
| 116 | + |
| 117 | +func getAllStreams() ([]ResourceBaseInfo, error) { |
| 118 | + streams, err := listStreams() |
| 119 | + if err != nil { |
| 120 | + logs.Logger.Errorf("Failed to get list streams : %s", err.Error()) |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + resources := make([]ResourceBaseInfo, len(streams)) |
| 125 | + for i, stream := range streams { |
| 126 | + resources[i].ID = stream.StreamID |
| 127 | + resources[i].Name = stream.StreamName |
| 128 | + resources[i].EpId = fmtTags(stream.SysTags)["_sys_enterprise_project_id"] |
| 129 | + resources[i].Tags = fmtTags(stream.Tags) |
| 130 | + } |
| 131 | + return resources, nil |
| 132 | +} |
0 commit comments