-
Notifications
You must be signed in to change notification settings - Fork 17
/
telemetry.go
146 lines (134 loc) · 4.18 KB
/
telemetry.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
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
// The MIT License
//
// Copyright (c) 2020, Cloudflare, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"cloud.google.com/go/logging"
"context"
"encoding/json"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"log"
"net/http"
"strings"
"sync"
)
// This runningTime structure contains the epoch timestamps for the following operations
// 1. Start => Epoch time at which the request is received by the ObliviousDNSHandler
// 2. TargetQueryDecryptionTime => Epoch
type runningTime struct {
Start int64
TargetQueryDecryptionTime int64
TargetQueryResolutionTime int64
TargetAnswerEncryptionTime int64
EndTime int64
}
type experiment struct {
RequestID []byte
Resolver string
Timestamp runningTime
Status bool
IngestedFrom string
ExperimentID string
ProtocolType string
}
func (e *experiment) serialize() string {
exp := &e
response, err := json.Marshal(exp)
if err != nil {
log.Printf("Unable to log the information correctly.")
}
return string(response)
}
type telemetry struct {
sync.RWMutex
esClient *elasticsearch.Client
buffer []string
logClient *logging.Client
cloudlogger *logging.Logger
}
const (
INDEX = "server_telemetry"
TYPE = "client_localhost"
)
var telemetryInstance telemetry
func getTelemetryInstance(telemetryType string) *telemetry {
var err error
if telemetryType == "ELK" {
elasticsearchTransport := elasticsearch.Config{
Addresses: []string{
"http://localhost:9200",
},
Transport: &http.Transport{
MaxIdleConnsPerHost: 1024,
},
}
telemetryInstance.esClient, err = elasticsearch.NewClient(elasticsearchTransport)
if err != nil {
log.Fatalf("Unable to create an elasticsearch client connection.")
}
} else if telemetryType == "GCP" {
ctx := context.Background()
projectID := "odoh-target"
telemetryInstance.logClient, err = logging.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Unable to create a logging instance to Google Cloud %v", err)
}
logName := "odohserver-gcp"
telemetryInstance.cloudlogger = telemetryInstance.logClient.Logger(logName)
} else {
telemetryInstance.cloudlogger = nil
telemetryInstance.esClient = nil
}
return &telemetryInstance
}
func (t *telemetry) streamTelemetryToGCPLogging(dataItems []string) {
defer t.cloudlogger.Flush()
for _, item := range dataItems {
log.Printf("Logging %v to the GCP instance\n", item)
t.cloudlogger.Log(logging.Entry{Payload: item})
}
}
func (t *telemetry) streamDataToElastic(dataItems []string) {
var wg sync.WaitGroup
for index, item := range dataItems {
wg.Add(1)
go func(i int, message string) {
defer wg.Done()
req := esapi.IndexRequest{
Index: INDEX,
Body: strings.NewReader(message),
Refresh: "true",
}
res, err := req.Do(context.Background(), t.esClient)
if err != nil {
log.Printf("Unable to send the request to elastic.")
}
defer res.Body.Close()
if res.IsError() {
log.Printf("[%s] Error Indexing Value [%s]", res.Status(), message)
} else {
log.Printf("Successfully Inserted [%s]", message)
}
}(index, item)
}
wg.Wait()
}