|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package milvus |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/milvus-io/milvus-sdk-go/v2/client" |
| 30 | + "hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/collect/strategy" |
| 31 | + "hertzbeat.apache.org/hertzbeat-collector-go/internal/collector/common/types/job" |
| 32 | + "hertzbeat.apache.org/hertzbeat-collector-go/internal/constants" |
| 33 | + "hertzbeat.apache.org/hertzbeat-collector-go/internal/util/logger" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + strategy.RegisterFactory(ProtocolMilvus, func(logger logger.Logger) strategy.Collector { |
| 38 | + return NewMilvusCollector(logger) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +const ( |
| 43 | + ProtocolMilvus = "milvus" |
| 44 | +) |
| 45 | + |
| 46 | +type MilvusCollector struct { |
| 47 | + logger logger.Logger |
| 48 | +} |
| 49 | + |
| 50 | +func NewMilvusCollector(log logger.Logger) *MilvusCollector { |
| 51 | + return &MilvusCollector{ |
| 52 | + logger: log.WithName("milvus-collector"), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (c *MilvusCollector) Protocol() string { |
| 57 | + return ProtocolMilvus |
| 58 | +} |
| 59 | + |
| 60 | +func (c *MilvusCollector) PreCheck(metrics *job.Metrics) error { |
| 61 | + if metrics == nil || metrics.Milvus == nil { |
| 62 | + return fmt.Errorf("milvus configuration is missing") |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (c *MilvusCollector) Collect(metrics *job.Metrics) *job.CollectRepMetricsData { |
| 68 | + start := time.Now() |
| 69 | + milvusConfig := metrics.Milvus |
| 70 | + |
| 71 | + // 1. Prepare connection |
| 72 | + addr := fmt.Sprintf("%s:%s", milvusConfig.Host, milvusConfig.Port) |
| 73 | + |
| 74 | + timeout := 10 * time.Second |
| 75 | + if milvusConfig.Timeout != "" { |
| 76 | + if t, err := strconv.Atoi(milvusConfig.Timeout); err == nil { |
| 77 | + timeout = time.Duration(t) * time.Millisecond |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 82 | + defer cancel() |
| 83 | + |
| 84 | + // 2. Connect |
| 85 | + var milvusClient client.Client |
| 86 | + var err error |
| 87 | + |
| 88 | + if milvusConfig.Username != "" && milvusConfig.Password != "" { |
| 89 | + milvusClient, err = client.NewDefaultGrpcClientWithAuth(ctx, addr, milvusConfig.Username, milvusConfig.Password) |
| 90 | + } else { |
| 91 | + milvusClient, err = client.NewDefaultGrpcClient(ctx, addr) |
| 92 | + } |
| 93 | + |
| 94 | + if err != nil { |
| 95 | + return c.createFailResponse(metrics, constants.CollectUnConnectable, fmt.Sprintf("failed to connect: %v", err)) |
| 96 | + } |
| 97 | + defer milvusClient.Close() |
| 98 | + |
| 99 | + // 3. Collect data |
| 100 | + // Currently only supports basic availability check and version retrieval |
| 101 | + version, err := milvusClient.GetVersion(ctx) |
| 102 | + if err != nil { |
| 103 | + return c.createFailResponse(metrics, constants.CollectFail, fmt.Sprintf("failed to get version: %v", err)) |
| 104 | + } |
| 105 | + |
| 106 | + responseTime := time.Since(start).Milliseconds() |
| 107 | + |
| 108 | + // 4. Parse/Format Response |
| 109 | + response := c.createSuccessResponse(metrics) |
| 110 | + row := job.ValueRow{Columns: make([]string, len(metrics.AliasFields))} |
| 111 | + |
| 112 | + for i, field := range metrics.AliasFields { |
| 113 | + switch strings.ToLower(field) { |
| 114 | + case strings.ToLower(constants.RESPONSE_TIME): |
| 115 | + row.Columns[i] = strconv.FormatInt(responseTime, 10) |
| 116 | + case "version": |
| 117 | + row.Columns[i] = version |
| 118 | + case "host": |
| 119 | + row.Columns[i] = milvusConfig.Host |
| 120 | + case "port": |
| 121 | + row.Columns[i] = milvusConfig.Port |
| 122 | + default: |
| 123 | + row.Columns[i] = constants.NULL_VALUE |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + response.Values = append(response.Values, row) |
| 128 | + return response |
| 129 | +} |
| 130 | + |
| 131 | +func (c *MilvusCollector) createSuccessResponse(metrics *job.Metrics) *job.CollectRepMetricsData { |
| 132 | + return &job.CollectRepMetricsData{ |
| 133 | + Metrics: metrics.Name, |
| 134 | + Time: time.Now().UnixMilli(), |
| 135 | + Code: constants.CollectSuccess, |
| 136 | + Msg: "success", |
| 137 | + Values: make([]job.ValueRow, 0), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func (c *MilvusCollector) createFailResponse(metrics *job.Metrics, code int, msg string) *job.CollectRepMetricsData { |
| 142 | + return &job.CollectRepMetricsData{ |
| 143 | + Metrics: metrics.Name, |
| 144 | + Time: time.Now().UnixMilli(), |
| 145 | + Code: code, |
| 146 | + Msg: msg, |
| 147 | + } |
| 148 | +} |
0 commit comments