-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add collection-interval to metrics-generator user-configurable overri…
…des (#2899) * Add collection-interval to metrics-generator user-configurable overrides Signed-off-by: Robbie Lankford <robert.lankford@grafana.com> * update CHANGELOG.md * Update modules/overrides/userconfigurable/client/limits.go Co-authored-by: Koenraad Verheyden <koenraad.verheyden@posteo.net> * add collection interval validation * implement json.Unmarshaler for duration type * implement json.Marshaler for duration type * fix tests/lint * move duration code --------- Signed-off-by: Robbie Lankford <robert.lankford@grafana.com> Co-authored-by: Koenraad Verheyden <koenraad.verheyden@posteo.net>
- Loading branch information
Showing
8 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Duration struct { | ||
time.Duration | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler. | ||
func (d *Duration) MarshalJSON() ([]byte, error) { | ||
return []byte(fmt.Sprintf(`"%s"`, d.String())), nil | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler. | ||
func (d *Duration) UnmarshalJSON(input []byte) error { | ||
var unmarshalledJSON interface{} | ||
|
||
err := json.Unmarshal(input, &unmarshalledJSON) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch value := unmarshalledJSON.(type) { | ||
case float64: | ||
d.Duration = time.Duration(value) | ||
case string: | ||
d.Duration, err = time.ParseDuration(value) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("invalid duration: %#v", unmarshalledJSON) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var ( | ||
_ json.Marshaler = (*Duration)(nil) | ||
_ json.Unmarshaler = (*Duration)(nil) | ||
) |
86 changes: 86 additions & 0 deletions
86
modules/overrides/userconfigurable/client/duration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockStruct struct { | ||
Duration *Duration `json:"duration"` | ||
} | ||
|
||
func TestDuration_MarshalJSON(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input mockStruct | ||
expected []byte | ||
expErr string | ||
}{ | ||
{ | ||
name: "marshal duration", | ||
input: mockStruct{&Duration{60 * time.Second}}, | ||
expected: []byte("{\"duration\":\"1m0s\"}"), | ||
expErr: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := json.Marshal(tc.input) | ||
if tc.expErr != "" { | ||
assert.EqualError(t, err, tc.expErr) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDuration_UnmarshalJSON(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input []byte | ||
expected mockStruct | ||
expErr string | ||
}{ | ||
{ | ||
name: "unmarshal duration nanoseconds", | ||
input: []byte(`{"duration":60000000000}`), | ||
expected: mockStruct{ | ||
Duration: &Duration{60 * time.Second}, | ||
}, | ||
expErr: "", | ||
}, | ||
{ | ||
name: "unmarshal duration string", | ||
input: []byte(`{"duration":"60s"}`), | ||
expected: mockStruct{ | ||
Duration: &Duration{60 * time.Second}, | ||
}, | ||
expErr: "", | ||
}, | ||
{ | ||
name: "unmarshal duration error", | ||
input: []byte(`{"duration":"foo"}`), | ||
expected: mockStruct{}, | ||
expErr: "time: invalid duration \"foo\"", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var result mockStruct | ||
err := json.Unmarshal(tc.input, &result) | ||
if tc.expErr != "" { | ||
assert.EqualError(t, err, tc.expErr) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, *tc.expected.Duration, *result.Duration) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters