-
Notifications
You must be signed in to change notification settings - Fork 0
/
typesense.go
146 lines (128 loc) · 4.61 KB
/
typesense.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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
const TypesenseHeaderApiKey = "X-TYPESENSE-API-KEY"
type Client struct {
http *http.Client
apiKey string
host string
}
type ResponseMetrics struct {
SystemCPUxActivePercentage map[string]json.Number `json:"-"`
SystemCPUActivePercentage float64 `json:"system_cpu_active_percentage,string"`
SystemDiskTotalBytes float64 `json:"system_disk_total_bytes,string"`
SystemDiskUsedBytes float64 `json:"system_disk_used_bytes,string"`
SystemMemoryTotalBytes float64 `json:"system_memory_total_bytes,string"`
SystemMemoryUsedBytes float64 `json:"system_memory_used_bytes,string"`
SystemNetworkReceivedBytes float64 `json:"system_network_received_bytes,string"`
SystemNetworkSentBytes float64 `json:"system_network_sent_bytes,string"`
TypesenseMemoryActiveBytes float64 `json:"typesense_memory_active_bytes,string"`
TypesenseMemoryAllocated float64 `json:"typesense_memory_allocated_bytes,string"`
TypesenseMemoryFragment float64 `json:"typesense_memory_fragmentation_ratio,string"`
TypesenseMemoryMapped float64 `json:"typesense_memory_mapped_bytes,string"`
TypesenseMemoryMetadata float64 `json:"typesense_memory_metadata_bytes,string"`
TypesenseMemoryResident float64 `json:"typesense_memory_resident_bytes,string"`
TypesenseMemoryRetained float64 `json:"typesense_memory_retained_bytes,string"`
}
type ResponseApiStats struct {
DeleteLatency float64 `json:"delete_latency_ms"`
DeleteRequests float64 `json:"delete_requests_per_second"`
ImportLatency float64 `json:"import_latency_ms"`
ImportRequests float64 `json:"import_requests_per_second"`
Latency map[string]json.Number `json:"latency_ms"`
PendingWrite float64 `json:"pending_write_batches"`
Requests map[string]json.Number `json:"requests_per_second"`
SearchLatency float64 `json:"search_latency_ms"`
SearchRequests float64 `json:"search_requests_per_second"`
TotalRequests float64 `json:"total_requests_per_second"`
WriteLatency float64 `json:"write_latency_ms"`
WriteRequests float64 `json:"write_requests_per_second"`
}
type ResponseHealth struct {
Ok bool `json:"ok"`
}
func NewClient(apiKey string, host string, insecure bool, timeout time.Duration) *Client {
return &Client{
http: &http.Client{
Timeout: timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
},
},
},
apiKey: apiKey,
host: host,
}
}
func (c *Client) get(path string) ([]byte, error) {
endpoint := fmt.Sprintf("%s/%s", c.host, path)
log.Printf("Fetching: %s\n", endpoint)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
log.Printf("Error while fetching: %s\n", endpoint)
log.Printf(err.Error())
return nil, err
}
req.Header.Set(TypesenseHeaderApiKey, c.apiKey)
resp, err := c.http.Do(req)
if err != nil {
log.Printf("Error while fetching: %s\n", endpoint)
log.Printf(err.Error())
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return body, err
}
func (c *Client) GetMetrics() (*ResponseMetrics, error) {
body, err := c.get("metrics.json")
response := &ResponseMetrics{}
err = json.Unmarshal(body, response)
if err != nil {
log.Printf("Error while unmarshalling input: %s", string(body))
return nil, err
}
err = json.Unmarshal(body, &response.SystemCPUxActivePercentage)
if err != nil {
log.Printf("Error while unmarshalling input: %s", string(body))
return nil, err
}
for k := range response.SystemCPUxActivePercentage {
if !strings.HasPrefix(k, "system_cpu") {
delete(response.SystemCPUxActivePercentage, k)
}
}
return response, nil
}
func (c *Client) GetStats() (*ResponseApiStats, error) {
body, err := c.get("stats.json")
response := &ResponseApiStats{}
err = json.Unmarshal(body, response)
if err != nil {
log.Printf("Error while unmarshalling input: %s", string(body))
return nil, err
}
return response, nil
}
func (c *Client) GetHealth() (bool, error) {
body, err := c.get("health")
if err != nil {
return false, err
}
response := &ResponseHealth{}
err = json.Unmarshal(body, response)
if err != nil {
log.Printf("Error while unmarshalling input: %s", string(body))
return false, err
}
return response.Ok, nil
}