forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream_logs.go
96 lines (77 loc) · 2.41 KB
/
stream_logs.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package app
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/StackVista/stackstate-agent/cmd/agent/common"
"github.com/StackVista/stackstate-agent/pkg/api/util"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/logs/diagnostic"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var (
filters diagnostic.Filters
)
func init() {
AgentCmd.AddCommand(troubleshootLogsCmd)
troubleshootLogsCmd.Flags().StringVar(&filters.Name, "name", "", "Filter by name")
troubleshootLogsCmd.Flags().StringVar(&filters.Type, "type", "", "Filter by type")
troubleshootLogsCmd.Flags().StringVar(&filters.Source, "source", "", "Filter by source")
}
var troubleshootLogsCmd = &cobra.Command{
Use: "stream-logs",
Short: "Stream the logs being processed by a running agent",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if flagNoColor {
color.NoColor = true
}
err := common.SetupConfigWithoutSecrets(confFilePath, "")
if err != nil {
return fmt.Errorf("unable to set up global agent configuration: %v", err)
}
err = config.SetupLogger(loggerName, config.GetEnvDefault("DD_LOG_LEVEL", "off"), "", "", false, true, false)
if err != nil {
fmt.Printf("Cannot setup logger, exiting: %v\n", err)
return err
}
return connectAndStream()
},
}
func connectAndStream() error {
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
body, err := json.Marshal(&filters)
if err != nil {
return err
}
urlstr := fmt.Sprintf("https://%v:%v/agent/stream-logs", ipcAddress, config.Datadog.GetInt("cmd_port"))
return streamRequest(urlstr, body, func(chunk []byte) {
fmt.Print(string(chunk))
})
}
func streamRequest(url string, body []byte, onChunk func([]byte)) error {
var e error
c := util.GetClient(false)
// Set session token
e = util.SetAuthToken()
if e != nil {
return e
}
e = util.DoPostChunked(c, url, "application/json", bytes.NewBuffer(body), onChunk)
if e == io.EOF {
return nil
}
if e != nil {
fmt.Printf("Could not reach agent: %v \nMake sure the agent is running before requesting the logs and contact support if you continue having issues. \n", e)
}
return e
}