|
| 1 | +/* |
| 2 | +Copyright 2021 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package observers |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" |
| 24 | + "github.com/fluxcd/flagger/pkg/metrics/providers" |
| 25 | +) |
| 26 | + |
| 27 | +var osmQueries = map[string]string{ |
| 28 | + "request-success-rate": ` |
| 29 | + sum( |
| 30 | + rate( |
| 31 | + osm_request_total{ |
| 32 | + destination_namespace="{{ namespace }}", |
| 33 | + destination_kind="Deployment", |
| 34 | + destination_name="{{ target }}", |
| 35 | + response_code!~"5.*" |
| 36 | + }[{{ interval }}] |
| 37 | + ) |
| 38 | + ) |
| 39 | + / |
| 40 | + sum( |
| 41 | + rate( |
| 42 | + osm_request_total{ |
| 43 | + destination_namespace="{{ namespace }}", |
| 44 | + destination_kind="Deployment", |
| 45 | + destination_name="{{ target }}" |
| 46 | + }[{{ interval }}] |
| 47 | + ) |
| 48 | + ) |
| 49 | + * 100`, |
| 50 | + "request-duration": ` |
| 51 | + histogram_quantile( |
| 52 | + 0.99, |
| 53 | + sum( |
| 54 | + rate( |
| 55 | + osm_request_duration_ms_bucket{ |
| 56 | + destination_namespace="{{ namespace }}", |
| 57 | + destination_kind="Deployment", |
| 58 | + destination_name="{{ target }}" |
| 59 | + }[{{ interval }}] |
| 60 | + ) |
| 61 | + ) by (le) |
| 62 | + )`, |
| 63 | +} |
| 64 | + |
| 65 | +type OsmObserver struct { |
| 66 | + client providers.Interface |
| 67 | +} |
| 68 | + |
| 69 | +func (ob *OsmObserver) GetRequestSuccessRate(model flaggerv1.MetricTemplateModel) (float64, error) { |
| 70 | + query, err := RenderQuery(osmQueries["request-success-rate"], model) |
| 71 | + if err != nil { |
| 72 | + return 0, fmt.Errorf("rendering query failed: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + value, err := ob.client.RunQuery(query) |
| 76 | + if err != nil { |
| 77 | + return 0, fmt.Errorf("running query failed: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + return value, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (ob *OsmObserver) GetRequestDuration(model flaggerv1.MetricTemplateModel) (time.Duration, error) { |
| 84 | + query, err := RenderQuery(osmQueries["request-duration"], model) |
| 85 | + if err != nil { |
| 86 | + return 0, fmt.Errorf("rendering query failed: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + value, err := ob.client.RunQuery(query) |
| 90 | + if err != nil { |
| 91 | + return 0, fmt.Errorf("running query failed: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + ms := time.Duration(int64(value)) * time.Millisecond |
| 95 | + return ms, nil |
| 96 | +} |
0 commit comments