Skip to content

Commit 5ffe36d

Browse files
authored
Use the m-lab/go/host package to parse -hostname flag (#50)
* Use the m-lab/go/host package to parse -hostname flag This provides a consistent interface for processing a hostname, and allows us to handle special cases in the `host` package instead of in this code. For example, the `host` package knows about managed instance group random suffixes on instance/node names, and can handle that gracefully for uuid-annotator. * Uses builtin String() func instead of fmt The m-lab/go/host package has a built-in String() function which returns the hostname, minus any suffix. Use this instead of some long fmt.Sprintf() call.
1 parent 89c25ed commit 5ffe36d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/go-test/deep v1.0.6
77
github.com/m-lab/annotation-service v0.0.0-20210504151333-138bdf572368
8-
github.com/m-lab/go v0.1.47
8+
github.com/m-lab/go v0.1.57
99
github.com/m-lab/tcp-info v1.5.3
1010
github.com/oschwald/geoip2-golang v1.7.0
1111
github.com/prometheus/client_golang v1.12.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ github.com/m-lab/annotation-service v0.0.0-20210504151333-138bdf572368 h1:cRzgLE
248248
github.com/m-lab/annotation-service v0.0.0-20210504151333-138bdf572368/go.mod h1:bW5A2AmUqyh6kGbmu4X8fYK2pRcfTvTjAXW/+4VQZUA=
249249
github.com/m-lab/go v0.1.47 h1:yV6RgVpiWm2BnpJcjfy4pbUkB9cz0BvBbVG64UGLiC0=
250250
github.com/m-lab/go v0.1.47/go.mod h1:woT26L9Hf07juZGHe7Z4WveV7MM6NS6vQaaWzRQnab4=
251+
github.com/m-lab/go v0.1.57 h1:C6IjtM/DY0FEiA3FB2HS7av4ybtwQOa8Y3vys6XEXaA=
252+
github.com/m-lab/go v0.1.57/go.mod h1:O1D/EoVarJ8lZt9foANcqcKtwxHatBzUxXFFyC87aQQ=
251253
github.com/m-lab/tcp-info v1.5.3 h1:4IspTPcNc8D8LNRvuFnID8gDiz+hxPAtYvpKZaiGGe8=
252254
github.com/m-lab/tcp-info v1.5.3/go.mod h1:bkvI4qbjB6QVC2tsLSHqf5OnIYcmuLEVjo7+8YA56Kg=
253255
github.com/m-lab/uuid-annotator v0.4.1/go.mod h1:f/zvgcc5A3HQ1Y63HWpbBVXNcsJwQ4uRIOqsF/nyto8=

main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/m-lab/go/content"
1212
"github.com/m-lab/go/flagx"
13+
"github.com/m-lab/go/host"
1314
"github.com/m-lab/go/memoryless"
1415
"github.com/m-lab/go/prometheusx"
1516
"github.com/m-lab/go/rtx"
@@ -71,6 +72,19 @@ func main() {
7172
flag.Parse()
7273
rtx.Must(flagx.ArgsFromEnv(flag.CommandLine), "Could not get args from environment variables")
7374

75+
// Parse the node's name into its constituent parts. This ensures that the
76+
// value of the -hostname flag is actually valid. Additionally, virtual
77+
// nodes which are part of a managed instance group may have a random
78+
// suffix, which uuid-annotator cannot use, so we explicitly only include
79+
// the parts of the node name that uuid-annotator actually cares about. The
80+
// resultant variable mlabHostname should match a machine name in siteinfo's
81+
// annotations.json:
82+
//
83+
// https://siteinfo.mlab-oti.measurementlab.net/v2/sites/annotations.json
84+
h, err := host.Parse(*hostname)
85+
rtx.Must(err, "Failed to parse -hostname flag value")
86+
mlabHostname := h.String()
87+
7488
defer mainCancel()
7589
// A waitgroup that waits for every component goroutine to complete before main exits.
7690
wg := sync.WaitGroup{}
@@ -121,7 +135,7 @@ func main() {
121135
// Load the siteinfo annotations for "site" specific metadata.
122136
js, err := content.FromURL(mainCtx, siteinfo.URL)
123137
rtx.Must(err, "Could not load siteinfo URL")
124-
site := siteannotator.New(mainCtx, *hostname, js, localIPs)
138+
site := siteannotator.New(mainCtx, mlabHostname, js, localIPs)
125139

126140
// Generate .json files for every UUID discovered.
127141
h := handler.New(*datadir, *eventbuffersize, []annotator.Annotator{geo, asn, site})

0 commit comments

Comments
 (0)