|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/argoproj/argo-rollouts/metricproviders/plugin" |
| 13 | + "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" |
| 14 | + "github.com/argoproj/argo-rollouts/utils/evaluate" |
| 15 | + metricutil "github.com/argoproj/argo-rollouts/utils/metric" |
| 16 | + timeutil "github.com/argoproj/argo-rollouts/utils/time" |
| 17 | + "github.com/prometheus/client_golang/api" |
| 18 | + v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 19 | + "github.com/prometheus/common/model" |
| 20 | + log "github.com/sirupsen/logrus" |
| 21 | +) |
| 22 | + |
| 23 | +const EnvVarArgoRolloutsPrometheusAddress string = "ARGO_ROLLOUTS_PROMETHEUS_ADDRESS" |
| 24 | + |
| 25 | +// Here is a real implementation of MetricsPlugin |
| 26 | +type RpcPlugin struct { |
| 27 | + LogCtx log.Entry |
| 28 | + api v1.API |
| 29 | +} |
| 30 | + |
| 31 | +type Config struct { |
| 32 | + // Address is the HTTP address and port of the prometheus server |
| 33 | + Address string `json:"address,omitempty" protobuf:"bytes,1,opt,name=address"` |
| 34 | + // Query is a raw prometheus query to perform |
| 35 | + Query string `json:"query,omitempty" protobuf:"bytes,2,opt,name=query"` |
| 36 | +} |
| 37 | + |
| 38 | +func (g *RpcPlugin) NewMetricsPlugin(metric v1alpha1.Metric) error { |
| 39 | + config := Config{} |
| 40 | + err := json.Unmarshal(metric.Provider.Plugin.Config, &config) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + api, err := newPrometheusAPI(config.Address) |
| 46 | + g.api = api |
| 47 | + |
| 48 | + return err |
| 49 | +} |
| 50 | + |
| 51 | +func (g *RpcPlugin) Run(anaysisRun *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alpha1.Measurement { |
| 52 | + startTime := timeutil.MetaNow() |
| 53 | + newMeasurement := v1alpha1.Measurement{ |
| 54 | + StartedAt: &startTime, |
| 55 | + } |
| 56 | + |
| 57 | + config := Config{} |
| 58 | + json.Unmarshal(metric.Provider.Plugin.Config, &config) |
| 59 | + |
| 60 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 61 | + defer cancel() |
| 62 | + |
| 63 | + response, warnings, err := g.api.Query(ctx, config.Query, time.Now()) |
| 64 | + if err != nil { |
| 65 | + return metricutil.MarkMeasurementError(newMeasurement, err) |
| 66 | + } |
| 67 | + |
| 68 | + newValue, newStatus, err := g.processResponse(metric, response) |
| 69 | + if err != nil { |
| 70 | + return metricutil.MarkMeasurementError(newMeasurement, err) |
| 71 | + |
| 72 | + } |
| 73 | + newMeasurement.Value = newValue |
| 74 | + if len(warnings) > 0 { |
| 75 | + warningMetadata := "" |
| 76 | + for _, warning := range warnings { |
| 77 | + warningMetadata = fmt.Sprintf(`%s"%s", `, warningMetadata, warning) |
| 78 | + } |
| 79 | + warningMetadata = warningMetadata[:len(warningMetadata)-2] |
| 80 | + if warningMetadata != "" { |
| 81 | + newMeasurement.Metadata = map[string]string{"warnings": warningMetadata} |
| 82 | + g.LogCtx.Warnf("Prometheus returned the following warnings: %s", warningMetadata) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + newMeasurement.Phase = newStatus |
| 87 | + finishedTime := timeutil.MetaNow() |
| 88 | + newMeasurement.FinishedAt = &finishedTime |
| 89 | + return newMeasurement |
| 90 | +} |
| 91 | + |
| 92 | +func (g *RpcPlugin) Resume(analysisRun *v1alpha1.AnalysisRun, metric v1alpha1.Metric, measurement v1alpha1.Measurement) v1alpha1.Measurement { |
| 93 | + return measurement |
| 94 | +} |
| 95 | + |
| 96 | +func (g *RpcPlugin) Terminate(analysisRun *v1alpha1.AnalysisRun, metric v1alpha1.Metric, measurement v1alpha1.Measurement) v1alpha1.Measurement { |
| 97 | + return measurement |
| 98 | +} |
| 99 | + |
| 100 | +func (g *RpcPlugin) GarbageCollect(*v1alpha1.AnalysisRun, v1alpha1.Metric, int) error { |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func (g *RpcPlugin) Type() string { |
| 105 | + return plugin.ProviderType |
| 106 | +} |
| 107 | + |
| 108 | +func (g *RpcPlugin) GetMetadata(metric v1alpha1.Metric) map[string]string { |
| 109 | + metricsMetadata := make(map[string]string) |
| 110 | + |
| 111 | + config := Config{} |
| 112 | + json.Unmarshal(metric.Provider.Plugin.Config, &config) |
| 113 | + if config.Query != "" { |
| 114 | + metricsMetadata["ResolvedPrometheusQuery"] = config.Query |
| 115 | + } |
| 116 | + return metricsMetadata |
| 117 | +} |
| 118 | + |
| 119 | +func (g *RpcPlugin) processResponse(metric v1alpha1.Metric, response model.Value) (string, v1alpha1.AnalysisPhase, error) { |
| 120 | + switch value := response.(type) { |
| 121 | + case *model.Scalar: |
| 122 | + valueStr := value.Value.String() |
| 123 | + result := float64(value.Value) |
| 124 | + newStatus, err := evaluate.EvaluateResult(result, metric, g.LogCtx) |
| 125 | + return valueStr, newStatus, err |
| 126 | + case model.Vector: |
| 127 | + results := make([]float64, 0, len(value)) |
| 128 | + valueStr := "[" |
| 129 | + for _, s := range value { |
| 130 | + if s != nil { |
| 131 | + valueStr = valueStr + s.Value.String() + "," |
| 132 | + results = append(results, float64(s.Value)) |
| 133 | + } |
| 134 | + } |
| 135 | + // if we appended to the string, we should remove the last comma on the string |
| 136 | + if len(valueStr) > 1 { |
| 137 | + valueStr = valueStr[:len(valueStr)-1] |
| 138 | + } |
| 139 | + valueStr = valueStr + "]" |
| 140 | + newStatus, err := evaluate.EvaluateResult(results, metric, g.LogCtx) |
| 141 | + return valueStr, newStatus, err |
| 142 | + default: |
| 143 | + return "", v1alpha1.AnalysisPhaseError, fmt.Errorf("Prometheus metric type not supported") |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func newPrometheusAPI(address string) (v1.API, error) { |
| 148 | + envValuesByKey := make(map[string]string) |
| 149 | + if value, ok := os.LookupEnv(fmt.Sprintf("%s", EnvVarArgoRolloutsPrometheusAddress)); ok { |
| 150 | + envValuesByKey[EnvVarArgoRolloutsPrometheusAddress] = value |
| 151 | + log.Debugf("ARGO_ROLLOUTS_PROMETHEUS_ADDRESS: %v", envValuesByKey[EnvVarArgoRolloutsPrometheusAddress]) |
| 152 | + } |
| 153 | + if len(address) != 0 { |
| 154 | + if !isUrl(address) { |
| 155 | + return nil, errors.New("prometheus address is not is url format") |
| 156 | + } |
| 157 | + } else if envValuesByKey[EnvVarArgoRolloutsPrometheusAddress] != "" { |
| 158 | + if isUrl(envValuesByKey[EnvVarArgoRolloutsPrometheusAddress]) { |
| 159 | + address = envValuesByKey[EnvVarArgoRolloutsPrometheusAddress] |
| 160 | + } else { |
| 161 | + return nil, errors.New("prometheus address is not is url format") |
| 162 | + } |
| 163 | + } else { |
| 164 | + return nil, errors.New("prometheus address is not configured") |
| 165 | + } |
| 166 | + client, err := api.NewClient(api.Config{ |
| 167 | + Address: address, |
| 168 | + }) |
| 169 | + if err != nil { |
| 170 | + log.Errorf("Error in getting prometheus client: %v", err) |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + return v1.NewAPI(client), nil |
| 174 | +} |
| 175 | + |
| 176 | +func isUrl(str string) bool { |
| 177 | + u, err := url.Parse(str) |
| 178 | + if err != nil { |
| 179 | + log.Errorf("Error in parsing url: %v", err) |
| 180 | + } |
| 181 | + log.Debugf("Parsed url: %v", u) |
| 182 | + return err == nil && u.Scheme != "" && u.Host != "" |
| 183 | +} |
0 commit comments