/
metron.go
78 lines (65 loc) · 1.94 KB
/
metron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package testservers
import (
"fmt"
"net"
"os"
"os/exec"
"time"
metronConf "metron/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
func BuildMetronConfig(dopplerURI string, grpcPort, udpPort int) metronConf.Config {
metronPort := getPort(metronPortOffset)
return metronConf.Config{
Index: jobIndex,
Job: jobName,
Zone: availabilityZone,
SharedSecret: sharedSecret,
IncomingUDPPort: metronPort,
PPROFPort: uint32(getPort(metronPPROFPortOffset)),
Deployment: "deployment",
DopplerAddr: fmt.Sprintf("%s:%d", dopplerURI, grpcPort),
DopplerAddrUDP: fmt.Sprintf("%s:%d", dopplerURI, udpPort),
GRPC: metronConf.GRPC{
CertFile: ClientCertFilePath(),
KeyFile: ClientKeyFilePath(),
CAFile: CAFilePath(),
},
MetricBatchIntervalMilliseconds: 10,
RuntimeStatsIntervalMilliseconds: 10,
}
}
func StartMetron(conf metronConf.Config) (func(), int, func()) {
By("making sure metron was build")
metronPath := os.Getenv("METRON_BUILD_PATH")
Expect(metronPath).ToNot(BeEmpty())
filename, err := writeConfigToFile("metron-config", conf)
Expect(err).ToNot(HaveOccurred())
By("starting metron")
metronCommand := exec.Command(metronPath, "--config", filename)
metronSession, err := gexec.Start(
metronCommand,
gexec.NewPrefixedWriter(color("o", "metron", green, magenta), GinkgoWriter),
gexec.NewPrefixedWriter(color("e", "metron", red, magenta), GinkgoWriter),
)
Expect(err).ToNot(HaveOccurred())
By("waiting for metron to listen")
Eventually(func() bool {
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", conf.PPROFPort))
if err != nil {
return false
}
conn.Close()
return true
}).Should(BeTrue())
return func() {
os.Remove(filename)
metronSession.Kill().Wait()
}, conf.IncomingUDPPort, func() {
// TODO When we switch to gRPC we should wait until
// we can connect to it
time.Sleep(10 * time.Second)
}
}