AzureMonitor: Move Application Insights and Insight Analytics to a deprecated package (#45834)

This commit is contained in:
Andres Martinez Gotor
2022-03-02 15:41:07 +01:00
committed by GitHub
parent 3427ae463d
commit 700f6863f2
33 changed files with 523 additions and 387 deletions
@@ -0,0 +1,46 @@
package time
var (
// 1m, 5m, 15m, 30m, 1h, 6h, 12h, 1d in milliseconds
defaultAllowedIntervalsMS = []int64{60000, 300000, 900000, 1800000, 3600000, 21600000, 43200000, 86400000}
)
// SetAutoTimeGrain tries to find the closest interval to the query's intervalMs value
// if the metric has a limited set of possible intervals/time grains then use those
// instead of the default list of intervals
func SetAutoTimeGrain(intervalMs int64, timeGrains []int64) (string, error) {
autoInterval := FindClosestAllowedIntervalMS(intervalMs, timeGrains)
tg := &TimeGrain{}
autoTimeGrain, err := tg.createISO8601DurationFromIntervalMS(autoInterval)
if err != nil {
return "", err
}
return autoTimeGrain, nil
}
// FindClosestAllowedIntervalMS is used for the auto time grain setting.
// It finds the closest time grain from the list of allowed time grains for Azure Monitor
// using the Grafana interval in milliseconds
// Some metrics only allow a limited list of time grains. The allowedTimeGrains parameter
// allows overriding the default list of allowed time grains.
func FindClosestAllowedIntervalMS(intervalMs int64, allowedTimeGrains []int64) int64 {
allowedIntervals := defaultAllowedIntervalsMS
if len(allowedTimeGrains) > 0 {
allowedIntervals = allowedTimeGrains
}
closest := allowedIntervals[0]
for i, allowed := range allowedIntervals {
if intervalMs > allowed {
if i+1 < len(allowedIntervals) {
closest = allowedIntervals[i+1]
} else {
closest = allowed
}
}
}
return closest
}
+52
View File
@@ -0,0 +1,52 @@
package time
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
)
// TimeGrain handles conversions between
// the ISO 8601 Duration format (PT1H), Kbn units (1h) and Time Grains (1 hour)
// Also handles using the automatic Grafana interval to calculate a ISO 8601 Duration.
type TimeGrain struct{}
var (
smallTimeUnits = []string{"hour", "minute", "h", "m"}
)
func (tg *TimeGrain) createISO8601DurationFromIntervalMS(it int64) (string, error) {
formatted := intervalv2.FormatDuration(time.Duration(it) * time.Millisecond)
if strings.Contains(formatted, "ms") {
return "PT1M", nil
}
timeValueString := formatted[0 : len(formatted)-1]
timeValue, err := strconv.Atoi(timeValueString)
if err != nil {
return "", fmt.Errorf("could not parse interval %q to an ISO 8061 duration: %w", it, err)
}
unit := formatted[len(formatted)-1:]
if unit == "s" && timeValue < 60 {
// minimum interval is 1m for Azure Monitor
return "PT1M", nil
}
return tg.createISO8601Duration(timeValue, unit), nil
}
func (tg *TimeGrain) createISO8601Duration(timeValue int, timeUnit string) string {
for _, smallTimeUnit := range smallTimeUnits {
if timeUnit == smallTimeUnit {
return fmt.Sprintf("PT%v%v", timeValue, strings.ToUpper(timeUnit[0:1]))
}
}
return fmt.Sprintf("P%v%v", timeValue, strings.ToUpper(timeUnit[0:1]))
}
@@ -0,0 +1,56 @@
package time
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTimeGrain_createISO8601Duration(t *testing.T) {
tg := &TimeGrain{}
testCases := []struct {
name string
value int
unit string
expected string
}{
{"1m", 1, "m", "PT1M"},
{"1minute", 1, "minute", "PT1M"},
{"2h", 2, "h", "PT2H"},
{"2hour", 2, "hour", "PT2H"},
{"1d", 1, "d", "P1D"},
{"2day", 2, "day", "P2D"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
d := tg.createISO8601Duration(tc.value, tc.unit)
assert.Equal(t, tc.expected, d)
})
}
}
func TestTimeGrain_createISO8601DurationFromIntervalMS(t *testing.T) {
tg := &TimeGrain{}
testCases := []struct {
name string
interval int64
expected string
}{
{"100", 100, "PT1M"},
{"59999", 59999, "PT1M"},
{"600000", 600000, "PT10M"},
{"172800000", 172800000, "P2D"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
d, err := tg.createISO8601DurationFromIntervalMS(tc.interval)
require.NoError(t, err)
assert.Equal(t, tc.expected, d)
})
}
}