mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Use interfaces where possible (#26392)
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/client"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
|
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
|
"github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
|
||||||
@@ -99,7 +100,7 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string,
|
|||||||
return result.Location, nil
|
return result.Location, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func webIdentityProvider(sess *session.Session) credentials.Provider {
|
func webIdentityProvider(sess client.ConfigProvider) credentials.Provider {
|
||||||
svc := sts.New(sess)
|
svc := sts.New(sess)
|
||||||
|
|
||||||
roleARN := os.Getenv("AWS_ROLE_ARN")
|
roleARN := os.Getenv("AWS_ROLE_ARN")
|
||||||
@@ -128,6 +129,6 @@ func ecsCredProvider(sess *session.Session, uri string) credentials.Provider {
|
|||||||
func(p *endpointcreds.Provider) { p.ExpiryWindow = 5 * time.Minute })
|
func(p *endpointcreds.Provider) { p.ExpiryWindow = 5 * time.Minute })
|
||||||
}
|
}
|
||||||
|
|
||||||
func ec2RoleProvider(sess *session.Session) credentials.Provider {
|
func ec2RoleProvider(sess client.ConfigProvider) credentials.Provider {
|
||||||
return &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess), ExpiryWindow: 5 * time.Minute}
|
return &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess), ExpiryWindow: 5 * time.Minute}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ func writeMetric(buf *bufio.Writer, m model.Metric, mf *dto.MetricFamily) error
|
|||||||
return addExtensionConventionForRollups(buf, mf, m)
|
return addExtensionConventionForRollups(buf, mf, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addExtensionConventionForRollups(buf *bufio.Writer, mf *dto.MetricFamily, m model.Metric) error {
|
func addExtensionConventionForRollups(buf io.Writer, mf *dto.MetricFamily, m model.Metric) error {
|
||||||
// Adding `.count` `.sum` suffix makes it possible to configure
|
// Adding `.count` `.sum` suffix makes it possible to configure
|
||||||
// different rollup strategies based on metric type
|
// different rollup strategies based on metric type
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package alerting
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -75,7 +74,7 @@ func (ae *AlertEngine) mapRulesToUsageStats(rules []*models.Alert) (DatasourceAl
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ae *AlertEngine) parseAlertRuleModel(settings *simplejson.Json) ([]int64, error) {
|
func (ae *AlertEngine) parseAlertRuleModel(settings json.Marshaler) ([]int64, error) {
|
||||||
datasourceIDs := []int64{}
|
datasourceIDs := []int64{}
|
||||||
model := alertJSONModel{}
|
model := alertJSONModel{}
|
||||||
|
|
||||||
@@ -85,7 +84,7 @@ func (ae *AlertEngine) parseAlertRuleModel(settings *simplejson.Json) ([]int64,
|
|||||||
|
|
||||||
bytes, err := settings.MarshalJSON()
|
bytes, err := settings.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return datasourceIDs, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(bytes, &model)
|
err = json.Unmarshal(bytes, &model)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package alerting
|
package alerting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -95,8 +96,7 @@ func TestParsingAlertRuleSettings(t *testing.T) {
|
|||||||
shouldErr: require.NoError,
|
shouldErr: require.NoError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "can parse blank content",
|
name: "can handle nil content",
|
||||||
file: "testdata/settings/invalid_format.json",
|
|
||||||
expected: []int64{},
|
expected: []int64{},
|
||||||
shouldErr: require.NoError,
|
shouldErr: require.NoError,
|
||||||
},
|
},
|
||||||
@@ -108,12 +108,16 @@ func TestParsingAlertRuleSettings(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range tcs {
|
for _, tc := range tcs {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
content, err := ioutil.ReadFile(tc.file)
|
var settings json.Marshaler
|
||||||
require.NoError(t, err, "expected to be able to read file")
|
if tc.file != "" {
|
||||||
|
content, err := ioutil.ReadFile(tc.file)
|
||||||
|
require.NoError(t, err, "expected to be able to read file")
|
||||||
|
|
||||||
j, _ := simplejson.NewJson(content)
|
settings, err = simplejson.NewJson(content)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
result, err := ae.parseAlertRuleModel(j)
|
result, err := ae.parseAlertRuleModel(settings)
|
||||||
|
|
||||||
tc.shouldErr(t, err)
|
tc.shouldErr(t, err)
|
||||||
diff := cmp.Diff(tc.expected, result)
|
diff := cmp.Diff(tc.expected, result)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package alerting
|
package alerting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
@@ -64,7 +65,7 @@ func findPanelQueryByRefID(panel *simplejson.Json, refID string) *simplejson.Jso
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyJSON(in *simplejson.Json) (*simplejson.Json, error) {
|
func copyJSON(in json.Marshaler) (*simplejson.Json, error) {
|
||||||
rawJSON, err := in.MarshalJSON()
|
rawJSON, err := in.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/aws/aws-sdk-go/aws/request"
|
"github.com/aws/aws-sdk-go/aws/request"
|
||||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
||||||
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
||||||
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
|
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
@@ -115,7 +116,8 @@ func (e *cloudWatchExecutor) getCWLogsClient(region string) (*cloudwatchlogs.Clo
|
|||||||
return newLogsClient, nil
|
return newLogsClient, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient *cloudwatchlogs.CloudWatchLogs, queryContext *tsdb.TsdbQuery) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient cloudwatchlogsiface.CloudWatchLogsAPI,
|
||||||
|
queryContext *tsdb.TsdbQuery) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||||
const maxAttempts = 8
|
const maxAttempts = 8
|
||||||
const pollPeriod = 1000 * time.Millisecond
|
const pollPeriod = 1000 * time.Millisecond
|
||||||
|
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ func getCredentials(dsInfo *datasourceInfo) (*credentials.Credentials, error) {
|
|||||||
return creds, nil
|
return creds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func webIdentityProvider(sess *session.Session) credentials.Provider {
|
func webIdentityProvider(sess client.ConfigProvider) credentials.Provider {
|
||||||
svc := newSTSService(sess)
|
svc := newSTSService(sess)
|
||||||
|
|
||||||
roleARN := os.Getenv("AWS_ROLE_ARN")
|
roleARN := os.Getenv("AWS_ROLE_ARN")
|
||||||
@@ -171,7 +171,7 @@ func ecsCredProvider(sess *session.Session, uri string) credentials.Provider {
|
|||||||
func(p *endpointcreds.Provider) { p.ExpiryWindow = 5 * time.Minute })
|
func(p *endpointcreds.Provider) { p.ExpiryWindow = 5 * time.Minute })
|
||||||
}
|
}
|
||||||
|
|
||||||
func ec2RoleProvider(sess *session.Session) credentials.Provider {
|
func ec2RoleProvider(sess client.ConfigProvider) credentials.Provider {
|
||||||
return &ec2rolecreds.EC2RoleProvider{Client: newEC2Metadata(sess), ExpiryWindow: 5 * time.Minute}
|
return &ec2rolecreds.EC2RoleProvider{Client: newEC2Metadata(sess), ExpiryWindow: 5 * time.Minute}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ enable = [
|
|||||||
"gosimple",
|
"gosimple",
|
||||||
"govet",
|
"govet",
|
||||||
"ineffassign",
|
"ineffassign",
|
||||||
# "interfacer",
|
|
||||||
"misspell",
|
"misspell",
|
||||||
"nakedret",
|
"nakedret",
|
||||||
"rowserrcheck",
|
"rowserrcheck",
|
||||||
|
|||||||
Reference in New Issue
Block a user