forked from HuaweiTech/lattice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (67 loc) · 1.99 KB
/
main.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
79
80
81
package main
import (
"flag"
"github.com/cloudfoundry/dropsonde"
"github.com/cloudfoundry/dropsonde/logs"
"io"
"os"
"os/exec"
"fmt"
)
var dropsondeDestination, sourceInstance string
const latticeDebugStreamId = "lattice-debug"
func init() {
flag.StringVar(
&dropsondeDestination,
"dropsondeDestination",
"",
`where to stream logs to in the form of hostname:port
eg. -dropsondeDestination=127.0.0.1:3457
`)
flag.StringVar(
&sourceInstance,
"sourceInstance",
"",
"The label for the log source instance that shows up when consuming the stream",
)
flag.Parse()
}
func main() {
if dropsondeDestination == ""{
fmt.Println("dropsondeDestination flag is required")
os.Exit(1)
}
if sourceInstance == ""{
fmt.Println("sourceInstance flag is required")
os.Exit(1)
}
args := flag.Args()
if len(args) == 0 {
fmt.Println("Command not specified!")
fmt.Println("Usage: tee2metron -dropsondeDestionation=127.0.0.1:3457 -sourceInstance=lattice-cell-21 COMMAND")
os.Exit(3)
}
err := dropsonde.Initialize(dropsondeDestination, sourceInstance, args[0])
if err != nil {
panic("error initializing dropsonde" + err.Error())
}
dropsondeStdoutReader, dropsondeStdoutWriter := io.Pipe()
dropsondeStderrReader, dropsondeStderrWriter := io.Pipe()
stdoutTeeWriter := io.MultiWriter(dropsondeStdoutWriter, os.Stdout)
stderrTeeWriter := io.MultiWriter(dropsondeStderrWriter, os.Stderr)
defer dropsondeStdoutReader.Close()
defer dropsondeStderrReader.Close()
defer dropsondeStdoutWriter.Close()
defer dropsondeStderrWriter.Close()
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = stdoutTeeWriter
cmd.Stderr = stderrTeeWriter
go logs.ScanLogStream(latticeDebugStreamId, args[0], sourceInstance, dropsondeStdoutReader)
go logs.ScanErrorLogStream(latticeDebugStreamId, args[0], sourceInstance, dropsondeStderrReader)
err = cmd.Start()
if err != nil {
fmt.Println(err)
os.Exit(3)
}
cmd.Wait()
}