Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add prometheus timeout #2893

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/analysis/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ spec:
provider:
prometheus:
address: http://prometheus.example.com:9090
# timeout is expressed in seconds
timeout: 40
zachaller marked this conversation as resolved.
Show resolved Hide resolved
query: |
sum(irate(
istio_requests_total{reporter="source",destination_service=~"{{args.service-name}}",response_code!~"5.*"}[5m]
Expand Down
12 changes: 12 additions & 0 deletions docs/features/kustomize/rollout_cr_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,10 @@
},
"query": {
"type": "string"
},
"timeout": {
"format": "int64",
"type": "integer"
}
},
"type": "object"
Expand Down Expand Up @@ -8670,6 +8674,10 @@
},
"query": {
"type": "string"
},
"timeout": {
"format": "int64",
"type": "integer"
}
},
"type": "object"
Expand Down Expand Up @@ -13060,6 +13068,10 @@
},
"query": {
"type": "string"
},
"timeout": {
"format": "int64",
"type": "integer"
}
},
"type": "object"
Expand Down
3 changes: 3 additions & 0 deletions manifests/crds/analysis-run-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,9 @@ spec:
type: object
query:
type: string
timeout:
format: int64
type: integer
type: object
skywalking:
properties:
Expand Down
3 changes: 3 additions & 0 deletions manifests/crds/analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2775,6 +2775,9 @@ spec:
type: object
query:
type: string
timeout:
format: int64
type: integer
type: object
skywalking:
properties:
Expand Down
3 changes: 3 additions & 0 deletions manifests/crds/cluster-analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2775,6 +2775,9 @@ spec:
type: object
query:
type: string
timeout:
format: int64
type: integer
type: object
skywalking:
properties:
Expand Down
9 changes: 9 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,9 @@ spec:
type: object
query:
type: string
timeout:
format: int64
type: integer
type: object
skywalking:
properties:
Expand Down Expand Up @@ -5739,6 +5742,9 @@ spec:
type: object
query:
type: string
timeout:
format: int64
type: integer
type: object
skywalking:
properties:
Expand Down Expand Up @@ -8584,6 +8590,9 @@ spec:
type: object
query:
type: string
timeout:
format: int64
type: integer
type: object
skywalking:
properties:
Expand Down
2 changes: 1 addition & 1 deletion metricproviders/metricproviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (f *ProviderFactory) NewProvider(logCtx log.Entry, metric v1alpha1.Metric)
if err != nil {
return nil, err
}
return prometheus.NewPrometheusProvider(api, logCtx), nil
return prometheus.NewPrometheusProvider(api, logCtx, metric)
case job.ProviderType:
return job.NewJobProvider(logCtx, f.KubeClient, f.JobLister), nil
case kayenta.ProviderType:
Expand Down
25 changes: 20 additions & 5 deletions metricproviders/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const (

// Provider contains all the required components to run a prometheus query
type Provider struct {
api v1.API
logCtx log.Entry
api v1.API
logCtx log.Entry
timeout time.Duration
}

// Type indicates provider is a prometheus provider
Expand All @@ -59,7 +60,7 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
}

//TODO(dthomson) make timeout configurable
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()

response, warnings, err := p.api.Query(ctx, metric.Provider.Prometheus.Query, time.Now())
Expand Down Expand Up @@ -138,11 +139,25 @@ func (p *Provider) processResponse(metric v1alpha1.Metric, response model.Value)
}

// NewPrometheusProvider Creates a new Prometheus client
func NewPrometheusProvider(api v1.API, logCtx log.Entry) *Provider {
return &Provider{
func NewPrometheusProvider(api v1.API, logCtx log.Entry, metric v1alpha1.Metric) (*Provider, error) {
provider := &Provider{
logCtx: logCtx,
api: api,
}

if metric.Provider.Prometheus == nil || metric.Provider.Prometheus.Timeout == nil {
zachaller marked this conversation as resolved.
Show resolved Hide resolved
provider.timeout = 30 * time.Second
return provider, nil
}

metricTimeout := metric.Provider.Prometheus.Timeout

if *metricTimeout < 0 {
return nil, errors.New("prometheus timeout should not be negative")
}

provider.timeout = time.Duration(*metricTimeout * int64(time.Second))
return provider, nil
}

// NewPrometheusAPI generates a prometheus API from the metric configuration
Expand Down
Loading
Loading