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

Allow to specify custom time range for metrics query #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ type batchRequest struct {
Method string `json:"httpMethod"`
}

func resourceURLFrom(resource string, metricNamespace string, metricNames string, aggregations []string) string {
func resourceURLFrom(resource string, metricNamespace string, metricNames string, aggregations []string, rangeSeconds, offsetSeconds *int) string {
apiVersion := "2018-01-01"

path := fmt.Sprintf(
Expand All @@ -593,7 +593,7 @@ func resourceURLFrom(resource string, metricNamespace string, metricNames string
resource,
)

endTime, startTime := GetTimes()
endTime, startTime := GetTimes(rangeSeconds, offsetSeconds)

values := url.Values{}
if metricNames != "" {
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ type Target struct {
MetricNamespace string `yaml:"metric_namespace"`
Metrics []Metric `yaml:"metrics"`
Aggregations []string `yaml:"aggregations"`
RangeSeconds *int `yaml:"range_seconds"`
OffsetSeconds *int `yaml:"offset_seconds"`

XXX map[string]interface{} `yaml:",inline"`
}
Expand All @@ -162,6 +164,8 @@ type ResourceGroup struct {
ResourceNameExcludeRe []Regexp `yaml:"resource_name_exclude_re"`
Metrics []Metric `yaml:"metrics"`
Aggregations []string `yaml:"aggregations"`
RangeSeconds *int `yaml:"range_seconds"`
OffsetSeconds *int `yaml:"offset_seconds"`

XXX map[string]interface{} `yaml:",inline"`
}
Expand All @@ -174,6 +178,8 @@ type ResourceTag struct {
ResourceTypes []string `yaml:"resource_types"`
Metrics []Metric `yaml:"metrics"`
Aggregations []string `yaml:"aggregations"`
RangeSeconds *int `yaml:"range_seconds"`
OffsetSeconds *int `yaml:"offset_seconds"`

XXX map[string]interface{} `yaml:",inline"`
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
rm.metricNamespace = target.MetricNamespace
rm.metrics = strings.Join(metrics, ",")
rm.aggregations = filterAggregations(target.Aggregations)
rm.resourceURL = resourceURLFrom(target.Resource, rm.metricNamespace, rm.metrics, rm.aggregations)
rm.resourceURL = resourceURLFrom(target.Resource, rm.metricNamespace, rm.metrics, rm.aggregations, target.RangeSeconds, target.OffsetSeconds)
incompleteResources = append(incompleteResources, rm)
}

Expand All @@ -259,7 +259,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
rm.metricNamespace = resourceGroup.MetricNamespace
rm.metrics = metricsStr
rm.aggregations = filterAggregations(resourceGroup.Aggregations)
rm.resourceURL = resourceURLFrom(f.ID, rm.metricNamespace, rm.metrics, rm.aggregations)
rm.resourceURL = resourceURLFrom(f.ID, rm.metricNamespace, rm.metrics, rm.aggregations, resourceGroup.RangeSeconds, resourceGroup.OffsetSeconds)
rm.resource = f
resources = append(resources, rm)
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
rm.metricNamespace = resourceTag.MetricNamespace
rm.metrics = metricsStr
rm.aggregations = filterAggregations(resourceTag.Aggregations)
rm.resourceURL = resourceURLFrom(f.ID, rm.metricNamespace, rm.metrics, rm.aggregations)
rm.resourceURL = resourceURLFrom(f.ID, rm.metricNamespace, rm.metrics, rm.aggregations, resourceTag.RangeSeconds, resourceTag.OffsetSeconds)
incompleteResources = append(incompleteResources, rm)
}
}
Expand Down
15 changes: 12 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ func PrintPrettyJSON(input map[string]interface{}) {
}

// GetTimes - Returns the endTime and startTime used for querying Azure Metrics API
func GetTimes() (string, string) {
func GetTimes(rangeSeconds, offsetSeconds *int) (string, string) {
// Make sure we are using UTC
now := time.Now().UTC()

if offsetSeconds == nil {
offsetSecondsDefault := 3 * 60
offsetSeconds = &offsetSecondsDefault
}
if rangeSeconds == nil {
rangeSecondsDefault := 60
rangeSeconds = &rangeSecondsDefault
}

// Use query delay of 3 minutes when querying for latest metric data
endTime := now.Add(time.Minute * time.Duration(-3)).Format(time.RFC3339)
startTime := now.Add(time.Minute * time.Duration(-4)).Format(time.RFC3339)
endTime := now.Add(time.Duration(-*offsetSeconds) * time.Second).Format(time.RFC3339)
startTime := now.Add(time.Duration(-*rangeSeconds-*offsetSeconds) * time.Second).Format(time.RFC3339)
return endTime, startTime
}

Expand Down