Debian Codegraph @

fastly/stats.go

raw symbols SCIP
1 package fastly
2
3 import "fmt"
4
5 // Stats represent metrics of a Fastly service
6 type Stats struct {
7 Requests uint64 `mapstructure:"requests"` // Number of requests processed.
8 Hits uint64 `mapstructure:"hits"` // Number of cache hits.
9 HitsTime float64 `mapstructure:"hits_time"` // Total amount of time spent processing cache hits (in seconds).
10 Miss uint64 `mapstructure:"miss"` // Number of cache misses.
11 MissTime float64 `mapstructure:"miss_time"` // Amount of time spent processing cache misses (in seconds).
12 Pass uint64 `mapstructure:"pass"` // Number of requests that passed through the CDN without being cached.
13 PassTime float64 `mapstructure:"pass_time"` // Amount of time spent processing cache passes (in seconds).
14 Synth uint64 `mapstructure:"synth"` // Number of requests that returned synth response.
15 Errors uint64 `mapstructure:"errors"` // Number of cache errors.
16 Restarts uint64 `mapstructure:"restarts"` // Number of restarts performed.
17 HitRatio float64 `mapstructure:"hit_ratio"` // Ratio of cache hits to cache misses (between 0 and 1).
18 Bandwidth uint64 `mapstructure:"bandwidth"` // Total bytes delivered (body_size + header_size).
19 RequestBodyBytes uint64 `mapstructure:"req_body_bytes"` // Total body bytes received.
20 RequestHeaderBytes uint64 `mapstructure:"req_header_bytes"` // Total header bytes received.
21 ResponseBodyBytes uint64 `mapstructure:"resp_body_bytes"` // Total body bytes delivered.
22 ResponseHeaderBytes uint64 `mapstructure:"resp_header_bytes"` // Total header bytes delivered.
23 BERequestBodyBytes uint64 `mapstructure:"bereq_body_bytes"` // Total body bytes sent to origin.
24 BERequestHeaderbytes uint64 `mapstructure:"bereq_header_bytes"` // Total header bytes sent to origin.
25 Uncachable uint64 `mapstructure:"uncachable"` // Number of requests that were designated uncachable.
26 Pipe uint64 `mapstructure:"pipe"` // Optional. Pipe operations performed (legacy feature).
27 TLS uint64 `mapstructure:"tls"` // Number of requests that were received over TLS.
28 TLSv10 uint64 `mapstructure:"tls_v10"` // Number of requests received over TLS 1.0.
29 TLSv11 uint64 `mapstructure:"tls_v11"` // Number of requests received over TLS 1.`.
30 TLSv12 uint64 `mapstructure:"tls_v12"` // Number of requests received over TLS 1.2.
31 TLSv13 uint64 `mapstructure:"tls_v13"` // Number of requests received over TLS 1.3.
32 Shield uint64 `mapstructure:"shield"` // Number of requests from shield to origin.
33 ShieldResponseBodyBytes uint64 `mapstructure:"shield_resp_body_bytes"` // Total body bytes delivered via a shield.
34 ShieldResponseHeaderBytes uint64 `mapstructure:"shield_resp_header_bytes"` // Total header bytes delivered via a shield.
35 IPv6 uint64 `mapstructure:"ipv6"` // Number of requests that were received over IPv6.
36 OTFP uint64 `mapstructure:"otfp"` // Number of responses that came from the Fastly On-the-Fly Packager for On Demand Streaming service for video-on-demand.
37 Video uint64 `mapstructure:"video"` // Number of responses with the video segment or video manifest MIME type (i.e., application/x-mpegurl, application/vnd.apple.mpegurl, application/f4m, application/dash+xml, application/vnd.ms-sstr+xml, ideo/mp2t, audio/aac, video/f4f, video/x-flv, video/mp4, audio/mp4).
38 PCI uint64 `mapstructure:"pci"` // Number of responses with the PCI flag turned on.
39 Log uint64 `mapstructure:"log"` // Number of log lines sent.
40 HTTP2 uint64 `mapstructure:"http2"` // Number of requests received over HTTP2.
41 WAFLogged uint64 `mapstructure:"waf_logged"` // Number of requests that triggered a WAF rule and were logged.
42 WAFBlocked uint64 `mapstructure:"waf_blocked"` // Number of requests that triggered a WAF rule and were blocked.
43 WAFPassed uint64 `mapstructure:"waf_passed"` // Number of requests that triggered a WAF rule and were passed.
44 AttackRequestBodyBytes uint64 `mapstructure:"attack_req_body_bytes"` // Total body bytes received from requests that triggered a WAF rule.
45 AttachRequestHeaderBytes uint64 `mapstructure:"attack_req_header_bytes"` // Total header bytes received from requests that triggered a WAF rule.
46 AttackResponseSynthBytes uint64 `mapstructure:"attack_resp_synth_bytes"` // Total bytes delivered for requests that triggered a WAF rule and returned a synthetic response.
47 ImageOptimizer uint64 `mapstructure:"imgopto"` // Number of responses that came from the Fastly Image Optimizer service.
48 Status200 uint64 `mapstructure:"status_200"` // Number of responses sent with status code 200 (Success).
49 Status204 uint64 `mapstructure:"status_204"` // Number of responses sent with status code 204 (No Content).
50 Status301 uint64 `mapstructure:"status_301"` // Number of responses sent with status code 301 (Moved Permanently).
51 Status302 uint64 `mapstructure:"status_302"` // Number of responses sent with status code 302 (Found).
52 Status304 uint64 `mapstructure:"status_304"` // Number of responses sent with status code 304 (Not Modified).
53 Status400 uint64 `mapstructure:"status_400"` // Number of responses sent with status code 400 (Bad Request).
54 Status401 uint64 `mapstructure:"status_401"` // Number of responses sent with status code 401 (Unauthorized).
55 Status403 uint64 `mapstructure:"status_403"` // Number of responses sent with status code 403 (Forbidden).
56 Status404 uint64 `mapstructure:"status_404"` // Number of responses sent with status code 404 (Not Found).
57 Status416 uint64 `mapstructure:"status_416"` // Number of responses sent with status code 416 (Range Not Satisfiable).
58 Status500 uint64 `mapstructure:"status_500"` // Number of responses sent with status code 500 (Internal Server Error).
59 Status501 uint64 `mapstructure:"status_501"` // Number of responses sent with status code 501 (Not Implemented).
60 Status502 uint64 `mapstructure:"status_502"` // Number of responses sent with status code 502 (Bad Gateway).
61 Status503 uint64 `mapstructure:"status_503"` // Number of responses sent with status code 503 (Service Unavailable).
62 Status504 uint64 `mapstructure:"status_504"` // Number of responses sent with status code 504 (Gateway Timeout).
63 Status505 uint64 `mapstructure:"status_505"` // Number of responses sent with status code 505 (HTTP Version Not Supported).
64 Status1xx uint64 `mapstructure:"status_1xx"` // Number of "Informational" category status codes delivered.
65 Status2xx uint64 `mapstructure:"status_2xx"` // Number of "Success" status codes delivered.
66 Status3xx uint64 `mapstructure:"status_3xx"` // Number of "Redirection" codes delivered.
67 Status4xx uint64 `mapstructure:"status_4xx"` // Number of "Client Error" codes delivered.
68 Status5xx uint64 `mapstructure:"status_5xx"` // Number of "Server Error" codes delivered.
69 ObjectSize1k uint64 `mapstructure:"object_size_1k"` // Number of objects served that were under 1KB in size.
70 ObjectSize10k uint64 `mapstructure:"object_size_10k"` // Number of objects served that were between 1KB and 10KB in size.
71 ObjectSize100k uint64 `mapstructure:"object_size_100k"` // Number of objects served that were between 10KB and 100KB in size.
72 ObjectSize1m uint64 `mapstructure:"object_size_1m"` // Number of objects served that were between 100KB and 1MB in size.
73 ObjectSize10m uint64 `mapstructure:"object_size_10m"` // Number of objects served that were between 1MB and 10MB in size.
74 ObjectSize100m uint64 `mapstructure:"object_size_100m"` // Number of objects served that were between 10MB and 100MB in size.
75 ObjectSize1g uint64 `mapstructure:"object_size_1g"` // Number of objects served that were between 100MB and 1GB in size.
76 MissHistogram map[int]int `mapstructure:"miss_histogram"` // Number of requests to origin in time buckets of 10s of milliseconds
77 BilledHeaderBytes uint64 `mapstructure:"billed_header_bytes"`
78 BilledBodyBytes uint64 `mapstructure:"billed_body_bytes"`
79 }
80
81 // GetStatsInput is an input to the GetStats function.
82 // Stats can be filtered by a Service ID, an individual stats field,
83 // time range (From and To), sampling rate (By) and/or Fastly region (Region)
84 // Allowed values for the fields are described at https://docs.fastly.com/api/stats
85 type GetStatsInput struct {
86 Service string
87 Field string
88 From string
89 To string
90 By string
91 Region string
92 }
93
94 // StatsResponse is a response from the service stats API endpoint
95 type StatsResponse struct {
96 Status string `mapstructure:"status"`
97 Meta map[string]string `mapstructure:"meta"`
98 Message string `mapstructure:"msg"`
99 Data []*Stats `mapstructure:"data"`
100 }
101
102 // GetStats returns stats data based on GetStatsInput
103 func (c *Client) GetStats(i *GetStatsInput) (*StatsResponse, error) {
104
105 p := "/stats"
106
107 if i.Service != "" {
108 p = fmt.Sprintf("%s/service/%s", p, i.Service)
109 }
110
111 if i.Field != "" {
112 p = fmt.Sprintf("%s/field/%s", p, i.Field)
113 }
114
115 r, err := c.Get(p, &RequestOptions{
116 Params: map[string]string{
117 "from": i.From,
118 "to": i.To,
119 "by": i.By,
120 "region": i.Region,
121 },
122 })
123 if err != nil {
124 return nil, err
125 }
126
127 var sr *StatsResponse
128 if err := decodeJSON(&sr, r.Body); err != nil {
129 return nil, err
130 }
131
132 return sr, nil
133 }
134
135 // UsageStatsResponse is a response from the account usage API endpoint
136 type UsageStatsResponse struct {
137 Status string `mapstructure:"status"`
138 Meta map[string]string `mapstructure:"meta"`
139 Message string `mapstructure:"msg"`
140 Data map[string]*Usage `mapstructure:"data"`
141 }
142
143 // Usage represents usage data of a single service or region
144 type Usage struct {
145 Requests uint64 `mapstructure:"requests"`
146 Bandwidth uint64 `mapstructure:"bandwidth"`
147 }
148
149 // RegionsUsage is a list of aggregated usage data by Fastly's region
150 type RegionsUsage map[string]*Usage
151
152 // UsageStatsResponse is a response from the account usage API endpoint
153 type UsageResponse struct {
154 Status string `mapstructure:"status"`
155 Meta map[string]string `mapstructure:"meta"`
156 Message string `mapstructure:"msg"`
157 Data *RegionsUsage `mapstructure:"data"`
158 }
159
160 // GetUsageInput is used as an input to the GetUsage function
161 // Value for the input are described at https://docs.fastly.com/api/stats
162 type GetUsageInput struct {
163 From string
164 To string
165 By string
166 Region string
167 }
168
169 // GetUsage returns usage information aggregated across all Fastly services and grouped by region.
170 func (c *Client) GetUsage(i *GetUsageInput) (*UsageResponse, error) {
171 r, err := c.Get("/stats/usage", &RequestOptions{
172 Params: map[string]string{
173 "from": i.From,
174 "to": i.To,
175 "by": i.By,
176 "region": i.Region,
177 },
178 })
179 if err != nil {
180 return nil, err
181 }
182 var sr *UsageResponse
183 if err := decodeJSON(&sr, r.Body); err != nil {
184 return nil, err
185 }
186
187 return sr, nil
188 }
189
190 // UsageStatsResponse is a response from the account usage API endpoint
191 type UsageByServiceResponse struct {
192 Status string `mapstructure:"status"`
193 Meta map[string]string `mapstructure:"meta"`
194 Message string `mapstructure:"msg"`
195 Data *ServicesByRegionsUsage `mapstructure:"data"`
196 }
197
198 // ServicesUsage is a list of usage data by a service
199 type ServicesUsage map[string]*Usage
200
201 // ServicesByRegionsUsage is a list of ServicesUsage by Fastly's region
202 type ServicesByRegionsUsage map[string]*ServicesUsage
203
204 // GetUsageByService returns usage information aggregated by service and
205 // grouped by service and region.
206 func (c *Client) GetUsageByService(i *GetUsageInput) (*UsageByServiceResponse, error) {
207 r, err := c.Get("/stats/usage_by_service", &RequestOptions{
208 Params: map[string]string{
209 "from": i.From,
210 "to": i.To,
211 "by": i.By,
212 "region": i.Region,
213 },
214 })
215 if err != nil {
216 return nil, err
217 }
218 var sr *UsageByServiceResponse
219 if err := decodeJSON(&sr, r.Body); err != nil {
220 return nil, err
221 }
222
223 return sr, nil
224 }
225
226 // RegionsResponse is a response from Fastly regions API endpoint
227 type RegionsResponse struct {
228 Status string `mapstructure:"status"`
229 Meta map[string]string `mapstructure:"meta"`
230 Message string `mapstructure:"msg"`
231 Data []string `mapstructure:"data"`
232 }
233
234 // GetRegions returns a list of Fastly regions
235 func (c *Client) GetRegions() (*RegionsResponse, error) {
236 r, err := c.Get("stats/regions", nil)
237 if err != nil {
238 return nil, err
239 }
240
241 var rr *RegionsResponse
242 if err := decodeJSON(&rr, r.Body); err != nil {
243 return nil, err
244 }
245
246 return rr, nil
247 }