-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin version and user agent fields to PluginContext (#736)
* add plugin version * add user agent * tidy * remove unused fields for now * fix imports * tidy * return nil * add comment
- Loading branch information
Showing
8 changed files
with
416 additions
and
221 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package useragent | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
userAgentRegex = regexp.MustCompile(`^Grafana/([0-9]+\.[0-9]+\.[0-9]+(?:-[a-zA-Z0-9]+)?) \(([a-zA-Z0-9]+); ([a-zA-Z0-9]+)\)$`) | ||
errInvalidFormat = errors.New("invalid user agent format") | ||
) | ||
|
||
// UserAgent represents a Grafana user agent. | ||
// Its format is "Grafana/<version> (<os>; <arch>)" | ||
// Example: "Grafana/7.0.0-beta1 (darwin; amd64)", "Grafana/10.0.0 (windows; x86)" | ||
type UserAgent struct { | ||
grafanaVersion string | ||
arch string | ||
os string | ||
} | ||
|
||
// New creates a new UserAgent. | ||
// The version must be a valid semver string, and the os and arch must be valid strings. | ||
func New(grafanaVersion, os, arch string) (*UserAgent, error) { | ||
ua := &UserAgent{ | ||
grafanaVersion: grafanaVersion, | ||
os: os, | ||
arch: arch, | ||
} | ||
|
||
return Parse(ua.String()) | ||
} | ||
|
||
// Parse creates a new UserAgent from a string. | ||
func Parse(s string) (*UserAgent, error) { | ||
matches := userAgentRegex.FindStringSubmatch(s) | ||
if len(matches) != 4 { | ||
return nil, errInvalidFormat | ||
} | ||
|
||
return &UserAgent{ | ||
grafanaVersion: matches[1], | ||
os: matches[2], | ||
arch: matches[3], | ||
}, nil | ||
} | ||
|
||
func (ua *UserAgent) GrafanaVersion() string { | ||
return ua.grafanaVersion | ||
} | ||
|
||
func (ua *UserAgent) String() string { | ||
return "Grafana/" + ua.grafanaVersion + " (" + ua.os + "; " + ua.arch + ")" | ||
} |
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,71 @@ | ||
package useragent | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFromString(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
userAgent string | ||
expected *UserAgent | ||
err error | ||
}{ | ||
{ | ||
name: "valid", | ||
userAgent: "Grafana/10.2.0 (darwin; amd64)", | ||
expected: &UserAgent{ | ||
grafanaVersion: "10.2.0", | ||
os: "darwin", | ||
arch: "amd64", | ||
}, | ||
}, { | ||
name: "valid (with semver suffix)", | ||
userAgent: "Grafana/7.0.0-beta1 (darwin; amd64)", | ||
expected: &UserAgent{ | ||
grafanaVersion: "7.0.0-beta1", | ||
os: "darwin", | ||
arch: "amd64", | ||
}, | ||
}, | ||
{ | ||
name: "invalid (missing os + arch)", | ||
userAgent: "Grafana/7.0.0-beta1", | ||
err: errInvalidFormat, | ||
}, | ||
{ | ||
name: "invalid (missing arch)", | ||
userAgent: "Grafana/7.0.0-beta1 (darwin)", | ||
err: errInvalidFormat, | ||
}, | ||
{ | ||
name: "invalid (missing os)", | ||
userAgent: "Grafana/7.0.0-beta1 (; amd64)", | ||
err: errInvalidFormat, | ||
}, | ||
{ | ||
name: "invalid (missing semicolon)", | ||
userAgent: "Grafana/7.0.0-beta1 (darwin amd64)", | ||
err: errInvalidFormat, | ||
}, | ||
{ | ||
name: "invalid (not semver)", | ||
userAgent: "Grafana/10.0 (darwin; amd64)", | ||
err: errInvalidFormat, | ||
}, | ||
{ | ||
name: "invalid (extra param)", | ||
userAgent: "Grafana/7.0.0-beta1 (darwin; amd64; linux)", | ||
err: errInvalidFormat, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
res, err := Parse(tc.userAgent) | ||
require.ErrorIs(t, err, tc.err) | ||
require.Equalf(t, tc.expected, res, "Parse(%v)", tc.userAgent) | ||
}) | ||
} | ||
} |
Oops, something went wrong.