This repository was archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathloganalytics.go
More file actions
182 lines (153 loc) · 5.16 KB
/
loganalytics.go
File metadata and controls
182 lines (153 loc) · 5.16 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package loganalytics
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"time"
"github.com/gliderlabs/logspout/router"
)
const (
envWorkspaceID = "LOGANALYTICS_WORKSPACE_ID"
envWorkspaceSecret = "LOGANALYTICS_WORKSPACE_SECRET"
envIoTHubName = "IOTEDGE_IOTHUBHOSTNAME"
envIoTHubDeviceID = "IOTEDGE_DEVICEID"
envGatewayHostName = "IOTEDGE_GATEWAYHOSTNAME"
envEdgeHubConnString = "EdgeHubConnectionString"
)
var (
locationGMT = time.FixedZone("GMT", 0)
)
// LogClient is the client for log analytics
type LogClient struct {
workspaceID string
workspaceSecret string
httpClient *http.Client
hostname string
iotHubName string
iotHubDeviceID string
signingKey []byte
apiLogsURL string
}
// ModuleMessage defines a log message from an IoT Edge module.
type ModuleMessage struct {
V int `json:"version"`
Time time.Time `json:"timeEmitted"`
Level string `json:"level"`
Data string `json:"msg"`
ModuleName string `json:"moduleName"`
ContainerID string `json:"containerID"`
ContainerImage string `json:"containerImage"`
Hostname string `json:"hostname"`
IoTHubName string `json:"iothubname"`
IoTHubDeviceID string `json:"iothubdeviceid"`
}
// NewLogClient creates a log client
func NewLogClient(workspaceID, workspaceSecret string) LogClient {
client := LogClient{
workspaceID: workspaceID,
workspaceSecret: workspaceSecret,
}
client.httpClient = &http.Client{Timeout: time.Second * 30}
client.signingKey, _ = base64.StdEncoding.DecodeString(workspaceSecret)
client.apiLogsURL = fmt.Sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01", workspaceID)
client.iotHubName = os.Getenv(envIoTHubName)
if len(client.iotHubName) == 0 {
connStr := os.Getenv(envEdgeHubConnString)
reg := regexp.MustCompile("HostName=(.*?);GatewayHostName=(.*?);DeviceId=(.*?);(.*?)")
matches := reg.FindStringSubmatch(connStr)
if len(matches) == 5 {
client.iotHubName = matches[1]
client.hostname = matches[2]
client.iotHubDeviceID = matches[3]
}
}
if len(client.iotHubDeviceID) == 0 {
client.iotHubDeviceID = os.Getenv(envIoTHubDeviceID)
client.hostname = os.Getenv(envGatewayHostName)
}
return client
}
// PostMessage logs an array of messages to log analytics service
func (c *LogClient) PostMessage(message *router.Message, timestamp time.Time) error {
if timestamp.IsZero() {
timestamp = time.Now().UTC()
}
msg := ModuleMessage{
V: 0,
Time: timestamp,
Level: message.Source,
Data: message.Data,
ModuleName: message.Container.Name,
ContainerID: message.Container.ID,
ContainerImage: message.Container.Config.Image,
Hostname: c.hostname,
IoTHubDeviceID: c.iotHubDeviceID,
IoTHubName: c.iotHubName,
}
body, _ := json.Marshal(msg)
req, _ := http.NewRequest(http.MethodPost, c.apiLogsURL, bytes.NewReader(body))
date := time.Now().In(locationGMT).Format(time.RFC1123)
stringToSign := "POST\n" + strconv.FormatInt(req.ContentLength, 10) + "\napplication/json\n" + "x-ms-date:" + date + "\n/api/logs"
signature := computeHmac256(stringToSign, c.signingKey)
req.Header.Set("Authorization", fmt.Sprintf("SharedKey %s:%s", c.workspaceID, signature))
req.Header.Add("Log-Type", "container_logs")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-ms-date", date)
req.Header.Set("time-generated-field", "Timestamp")
response, err := c.httpClient.Do(req)
if err != nil {
return err
}
if response.StatusCode != 200 {
defer response.Body.Close()
buf, _ := ioutil.ReadAll(response.Body)
time.AfterFunc(
time.Second*15,
func() {
c.PostMessage(message, timestamp)
})
return fmt.Errorf("[loganalytics][%s] Post log request failed with status: %d %s", time.Now().UTC().Format(time.RFC3339), response.StatusCode, string(buf))
}
return nil
}
func init() {
router.AdapterFactories.Register(NewLogAnalyticsAdapter, "loganalytics")
}
// ComputeHmac256 computes HMAC with given secret
func computeHmac256(message string, secret []byte) string {
h := hmac.New(sha256.New, secret)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func NewLogAnalyticsAdapter(route *router.Route) (router.LogAdapter, error) {
workspaceID, workspaceSecret := os.Getenv(envWorkspaceID), os.Getenv(envWorkspaceSecret)
if workspaceID == "" || workspaceSecret == "" {
return nil,
fmt.Errorf("Workspace Id and secret not defined in environment variable '%s' and '%s'.\n", envWorkspaceID, envWorkspaceSecret)
}
client := NewLogClient(workspaceID, workspaceSecret)
return &Adapter{
route: route,
client: &client,
}, nil
}
// Adapter defines a logspout adapter for azure log analytics.
type Adapter struct {
route *router.Route
client *LogClient
}
// Stream waits on a logspout message channel. Upon receiving on it POSTs it to
// Log Analytics endpoint.
func (adapter *Adapter) Stream(logstream chan *router.Message) {
for message := range logstream {
adapter.client.PostMessage(message, time.Now().UTC())
}
}