-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (110 loc) · 3.04 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
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
package main
import (
"encoding/json"
"net/http"
"os"
"time"
"github.com/salsadigitalauorg/lagoon-solr-metrics/pkg/lagoon"
log "github.com/sirupsen/logrus"
)
var (
solrHost string
solrPort string
solrCore string
insightsEndpoint string
)
// Init http client with default timeout.
var httpClient = &http.Client{Timeout: 10 * time.Second}
type Solr struct {
Metrics map[string]any `json:"metrics"`
}
// Struct that only holds selected metrics.
type Metrics struct {
Version string `json:"CONTAINER.version.specification,omitempty"`
Name string `json:"CORE.coreName,omitempty"`
StartTime string `json:"CORE.startTime,omitempty"`
NumDocs int `json:"SEARCHER.searcher.numDocs,omitempty"`
IndexSize string `json:"INDEX.size,omitempty"`
}
// A copy of Metrics struct but with flattened json keys.
type Info struct {
Version string `json:"Version"`
Name string `json:"Core name"`
StartTime string `json:"Start time"`
NumDocs int `json:"Documents,string"`
IndexSize string `json:"Index size"`
}
func main() {
// Extract environment variables.
parseEnvVars()
// Request data from Solr.
resp, err := httpClient.Get("http://" + solrHost + ":" + solrPort + "/solr/admin/metrics")
if err != nil {
log.Fatal(err)
}
// Ensure response body is always closed.
defer resp.Body.Close()
// Extract selected data from Solr.
metrics := parseSolrData(resp)
// Convert metrics to a flattened structure.
info := Info(metrics)
if err := transcode(metrics, &info); err != nil {
log.Fatal(err)
}
// Push data to Remote Insights.
var dataMap map[string]string
if err := transcode(info, &dataMap); err != nil {
log.Fatal(err)
}
if err := lagoon.ProcessFacts(dataMap, insightsEndpoint); err != nil {
log.Fatal(err)
}
}
func parseSolrData(resp *http.Response) Metrics {
// Parse raw Solr API json.
solr := Solr{}
json.NewDecoder(resp.Body).Decode(&solr)
// Initialise the metrics container.
metrics := Metrics{}
// Solr Core metrics.
if err := transcode(solr.Metrics["solr.core."+solrCore], &metrics); err != nil {
log.Fatal(err)
}
// Solr Node metrics.
if err := transcode(solr.Metrics["solr.node"], &metrics); err != nil {
log.Fatal(err)
}
return metrics
}
// Parse data to a struct.
func transcode(in any, out interface{}) error {
bytes, err := json.Marshal(in)
if err != nil {
return err
}
json.Unmarshal(bytes, &out)
return nil
}
// Read and apply supported environment variables.
func parseEnvVars() {
solrHostEnv := os.Getenv("SOLR_HOST")
solrHost = "solr" // default.
if solrHostEnv != "" {
solrHost = solrHostEnv
}
solrPortEnv := os.Getenv("SOLR_PORT")
solrPort = "8983" // default.
if solrPortEnv != "" {
solrPort = solrPortEnv
}
solrCoreEnv := os.Getenv("SOLR_CORE")
solrCore = "drupal" // default.
if solrCoreEnv != "" {
solrCore = solrCoreEnv
}
insightsEndpointEnv := os.Getenv("LAGOON_INSIGHTS_ENDPOINT")
insightsEndpoint = "http://lagoon-remote-insights-remote.lagoon.svc" // default.
if insightsEndpointEnv != "" {
insightsEndpoint = insightsEndpointEnv
}
}