-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
998 lines (779 loc) · 30.8 KB
/
client.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
// Do not edit, this file was generated by github.com/apex/rpc.
package logs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// Alert represents configuration for performing alerting.
type Alert struct {
// CreatedAt is a timestamp indicating when the alert was created. This field is read-only.
CreatedAt time.Time `json:"created_at"`
// Description is the description of the alert.
Description string `json:"description"`
// ID is the alert id. This field is read-only.
ID string `json:"id"`
// Interval is the interval in minutes for performing the alert.
Interval int `json:"interval"`
// Limit is the maximum number of events in the alert notification.
Limit int `json:"limit"`
// Muted is a boolean used ignore trigger and resolve notifications.
Muted bool `json:"muted"`
// Name is the name of the alert. This field is required.
Name string `json:"name"`
// NotificationID is the notification id for reporting alerts, when omitted the alert will not run. This field is required.
NotificationID string `json:"notification_id"`
// Operator is the operator used when comparing against the threshold. This field is required. Must be one of: ">", ">=", "<", "<=".
Operator string `json:"operator"`
// ProjectID is the associated project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the query performed by the alert. This field is required.
Query string `json:"query"`
// Severity is the severity of the alert. This field is required. Must be one of: "info", "notice", "error", "critical".
Severity string `json:"severity"`
// Threshold is the threshold for comparison against the selected operator.
Threshold int `json:"threshold"`
// UpdatedAt is a timestamp indicating when the alert was last updated. This field is read-only.
UpdatedAt time.Time `json:"updated_at"`
}
// BooleanFieldStat represents a boolean field's stats.
type BooleanFieldStat struct {
// Count is the number of times this field occurred in the sampled events.
Count int `json:"count"`
// Percent is the percentage of occurrences in the sampled events.
Percent float64 `json:"percent"`
// Value is the boolean value.
Value bool `json:"value"`
}
// DiscoveredField represents a single discovered field.
type DiscoveredField struct {
// Count is the number of times this field occurred in the sampled events.
Count int `json:"count"`
// Name is the field name.
Name string `json:"name"`
// Percent is the percentage of occurrences in the sampled events.
Percent float64 `json:"percent"`
// Type is the type of discovered field. Must be one of: "string", "number", "boolean".
Type string `json:"type"`
}
// Event represents a single log event.
type Event struct {
// Fields is the log fields.
Fields map[string]interface{} `json:"fields"`
// ID is the event id.
ID string `json:"id"`
// Level is the severity level. This field is required. Must be one of: "debug", "info", "notice", "warning", "error", "critical", "alert", "emergency".
Level string `json:"level"`
// Message is the log message. This field is required.
Message string `json:"message"`
// Timestamp is the creation timestamp.
Timestamp time.Time `json:"timestamp"`
}
// InstanceConfig represents an instance's configuration.
type InstanceConfig struct {
// ProjectID is the Google Cloud project id.
ProjectID string `json:"project_id"`
// Region is the Google Cloud region id.
Region string `json:"region"`
// TeamID is the Apex team id.
TeamID string `json:"team_id"`
}
// Notification represents an alert notification.
type Notification struct {
// CreatedAt is a timestamp indicating when the notification was created. This field is read-only.
CreatedAt time.Time `json:"created_at"`
// EmailAddresses is the receipients of the alert notifications.
EmailAddresses []string `json:"email_addresses"`
// ID is the notification id. This field is read-only.
ID string `json:"id"`
// Name is the name of the notification. This field is required.
Name string `json:"name"`
// PagerdutyServiceKey is the PagerDuty service key.
PagerdutyServiceKey string `json:"pagerduty_service_key"`
// ProjectID is the associated project id. This field is required.
ProjectID string `json:"project_id"`
// SlackChannel is the Slack channel name, otherwise the default for the webhook is used.
SlackChannel string `json:"slack_channel"`
// SlackWebhookURL is the Slack webhook URL.
SlackWebhookURL string `json:"slack_webhook_url"`
// SmsNumbers is the receipients of the alert notifications.
SmsNumbers []string `json:"sms_numbers"`
// Type is the type of notification. This field is required. Must be one of: "slack", "pagerduty", "email", "sms", "webhook".
Type string `json:"type"`
// UpdatedAt is a timestamp indicating when the notification was last updated. This field is read-only.
UpdatedAt time.Time `json:"updated_at"`
// WebhookURL is the webhook URL which receives the alert payloads.
WebhookURL string `json:"webhook_url"`
}
// Project represents an isolated set of log events and alerts. A project can be created for each application, environment, or team within an organization depending on your preferences.
type Project struct {
// CreatedAt is a timestamp indicating when the project was created. This field is read-only.
CreatedAt time.Time `json:"created_at"`
// Description is the project description.
Description string `json:"description"`
// ID is the project id. This field is read-only.
ID string `json:"id"`
// Location is the geographical location where the log events are stored. This field is required. Must be one of: "us-west2", "northamerica-northeast1", "us-east4", "southamerica-east1", "europe-north1", "europe-west2", "europe-west6", "asia-east2", "asia-south1", "asia-northeast2", "asia-east1", "asia-northeast1", "asia-southeast1", "australia-southeast1".
Location string `json:"location"`
// Mode is the storage mode, optimized for plain-text or structured logs. Both options support plain-text and structured logging, however, the structured mode shards on the `message` value, restricting its length to 1024 bytes. This field is required. Must be one of: "plain_text", "structured".
Mode string `json:"mode"`
// Name is the human-friendly project name. This field is required.
Name string `json:"name"`
// Retention is the retention of log events in days. When zero the logs do not expire.
Retention int `json:"retention"`
// UpdatedAt is a timestamp indicating when the project was last updated. This field is read-only.
UpdatedAt time.Time `json:"updated_at"`
}
// QueryStats represents query statistics.
type QueryStats struct {
// CacheHit is a boolean indicating if the query was cached.
CacheHit bool `json:"cache_hit"`
// Duration is the query duration in milliseconds.
Duration int `json:"duration"`
// TotalBytesBilled is the total number of bytes billed by the query.
TotalBytesBilled int `json:"total_bytes_billed"`
// TotalBytesProcessed is the total number of bytes processed by the query.
TotalBytesProcessed int `json:"total_bytes_processed"`
}
// Search represents a saved search query.
type Search struct {
// CreatedAt is a timestamp indicating when the saved search was created. This field is read-only.
CreatedAt time.Time `json:"created_at"`
// ID is the saved search id. This field is read-only.
ID string `json:"id"`
// Name is the name of the saved search. This field is required.
Name string `json:"name"`
// ProjectID is the associated project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the saved search query. This field is required.
Query string `json:"query"`
// UpdatedAt is a timestamp indicating when the saved search was last updated. This field is read-only.
UpdatedAt time.Time `json:"updated_at"`
}
// StringFieldStat represents a string field's stats.
type StringFieldStat struct {
// Count is the number of times this field occurred in the sampled events.
Count int `json:"count"`
// Percent is the percentage of occurrences in the sampled events.
Percent float64 `json:"percent"`
// Value is the string value.
Value string `json:"value"`
}
// TimeseriesPoint represents a single point in a timeseries query.
type TimeseriesPoint struct {
// Count is the number of events for this bucket.
Count int `json:"count"`
// Timestamp is the bucket timestamp.
Timestamp time.Time `json:"timestamp"`
}
// Token represents an API token.
type Token struct {
// CreatedAt is a timestamp indicating when the token was created. This field is read-only.
CreatedAt time.Time `json:"created_at"`
// Description is the description of the token.
Description string `json:"description"`
// ID is the token. This field is read-only.
ID string `json:"id"`
// LastUsedAt is a timestamp indicating when the token was last used. This field is read-only.
LastUsedAt time.Time `json:"last_used_at"`
// Scopes is available to this token, permitting access to read and write data. This field is required. Must be one of: "events:read", "events:write", "alerts:read", "alerts:write", "notifications:read", "notifications:write", "projects:read", "projects:write", "tokens:read", "tokens:write", "searches:read", "searches:write".
Scopes []string `json:"scopes"`
}
// AddAlertInput params.
type AddAlertInput struct {
// Alert is the alert. This field is required.
Alert Alert `json:"alert"`
}
// AddAlertOutput params.
type AddAlertOutput struct {
// ID is the alert id. This field is required.
ID string `json:"id"`
}
// AddEventsInput params.
type AddEventsInput struct {
// Events is the batch of events. This field is required.
Events []Event `json:"events"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// AddNotificationInput params.
type AddNotificationInput struct {
// Notification is the notification. This field is required.
Notification Notification `json:"notification"`
}
// AddNotificationOutput params.
type AddNotificationOutput struct {
// ID is the notification id. This field is required.
ID string `json:"id"`
}
// AddProjectInput params.
type AddProjectInput struct {
// Project is the project. This field is required.
Project Project `json:"project"`
}
// AddProjectOutput params.
type AddProjectOutput struct {
// ID is the project id. This field is required.
ID string `json:"id"`
}
// AddSearchInput params.
type AddSearchInput struct {
// Search is the saved search. This field is required.
Search Search `json:"search"`
}
// AddSearchOutput params.
type AddSearchOutput struct {
// ID is the saved search id. This field is required.
ID string `json:"id"`
}
// AddTokenInput params.
type AddTokenInput struct {
// Token is the token. This field is required.
Token Token `json:"token"`
}
// AddTokenOutput params.
type AddTokenOutput struct {
// ID is the token id.
ID string `json:"id"`
}
// GetAlertInput params.
type GetAlertInput struct {
// AlertID is the alert id. This field is required.
AlertID string `json:"alert_id"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// GetAlertOutput params.
type GetAlertOutput struct {
// Alert is the alert. This field is required.
Alert Alert `json:"alert"`
}
// GetAlertsInput params.
type GetAlertsInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// GetAlertsOutput params.
type GetAlertsOutput struct {
// Alerts is the alerts. This field is required.
Alerts []Alert `json:"alerts"`
}
// GetBooleanFieldStatsInput params.
type GetBooleanFieldStatsInput struct {
// Field is the field name. This field is required.
Field string `json:"field"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// GetBooleanFieldStatsOutput params.
type GetBooleanFieldStatsOutput struct {
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
// Values is the boolean values. This field is required.
Values []BooleanFieldStat `json:"values"`
}
// GetCountInput params.
type GetCountInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// GetCountOutput params.
type GetCountOutput struct {
// Count is the query result count. This field is required.
Count int `json:"count"`
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
}
// GetDiscoveredFieldsInput params.
type GetDiscoveredFieldsInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// GetDiscoveredFieldsOutput params.
type GetDiscoveredFieldsOutput struct {
// Fields is the fields discovered. This field is required.
Fields []DiscoveredField `json:"fields"`
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
}
// GetInstanceConfigOutput params.
type GetInstanceConfigOutput struct {
// Config is the instance configuration.
Config InstanceConfig `json:"config"`
}
// GetNotificationInput params.
type GetNotificationInput struct {
// NotificationID is the notification id. This field is required.
NotificationID string `json:"notification_id"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// GetNotificationOutput params.
type GetNotificationOutput struct {
// Notification is the notification. This field is required.
Notification Notification `json:"notification"`
}
// GetNotificationsInput params.
type GetNotificationsInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// GetNotificationsOutput params.
type GetNotificationsOutput struct {
// Notifications is the notifications. This field is required.
Notifications []Notification `json:"notifications"`
}
// GetNumericFieldStatsInput params.
type GetNumericFieldStatsInput struct {
// Field is the field name. This field is required.
Field string `json:"field"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// GetNumericFieldStatsOutput params.
type GetNumericFieldStatsOutput struct {
// Avg is the avg value. This field is required.
Avg float64 `json:"avg"`
// Max is The max value. This field is required.
Max float64 `json:"max"`
// Min is the min value. This field is required.
Min float64 `json:"min"`
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
}
// GetProjectStatsInput params.
type GetProjectStatsInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// GetProjectStatsOutput params.
type GetProjectStatsOutput struct {
// BytesTotal is the total number of bytes stored. This field is required.
BytesTotal int `json:"bytes_total"`
// EventsTotal is the total number of events stored. This field is required.
EventsTotal int `json:"events_total"`
}
// GetProjectsOutput params.
type GetProjectsOutput struct {
// Projects is the projects. This field is required.
Projects []Project `json:"projects"`
}
// GetSearchesInput params.
type GetSearchesInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// GetSearchesOutput params.
type GetSearchesOutput struct {
// Searches is the saved searches.
Searches []Search `json:"searches"`
}
// GetStringFieldStatsInput params.
type GetStringFieldStatsInput struct {
// Field is the field name. This field is required.
Field string `json:"field"`
// Limit is the maximum number of values to return.
Limit int `json:"limit"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// GetStringFieldStatsOutput params.
type GetStringFieldStatsOutput struct {
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
// Values is the string values. This field is required.
Values []StringFieldStat `json:"values"`
}
// GetTimeseriesInput params.
type GetTimeseriesInput struct {
// MaxPoints is the maxmimum number of datapoints to return. This field is required.
MaxPoints int `json:"max_points"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// GetTimeseriesOutput params.
type GetTimeseriesOutput struct {
// Points is the series. This field is required.
Points []TimeseriesPoint `json:"points"`
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
}
// GetTokensOutput params.
type GetTokensOutput struct {
// Tokens is the tokens.
Tokens []Token `json:"tokens"`
}
// QueryInput params.
type QueryInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the SQL query string. This field is required.
Query string `json:"query"`
}
// QueryOutput params.
type QueryOutput struct {
// Results is the query results. This field is required.
Results []map[string]interface{} `json:"results"`
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
}
// RemoveAlertInput params.
type RemoveAlertInput struct {
// AlertID is the alert id. This field is required.
AlertID string `json:"alert_id"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// RemoveNotificationInput params.
type RemoveNotificationInput struct {
// NotificationID is the notification id. This field is required.
NotificationID string `json:"notification_id"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// RemoveProjectInput params.
type RemoveProjectInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
}
// RemoveSearchInput params.
type RemoveSearchInput struct {
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// SearchID is the saved search id. This field is required.
SearchID string `json:"search_id"`
}
// RemoveTokenInput params.
type RemoveTokenInput struct {
// TokenID is the token id. This field is required.
TokenID string `json:"token_id"`
}
// SearchInput params.
type SearchInput struct {
// Limit is the maxmimum number of events to return.
Limit int `json:"limit"`
// Order is the query timestamp sort order. Must be one of: "ascending", "descending".
Order string `json:"order"`
// ProjectID is the project id. This field is required.
ProjectID string `json:"project_id"`
// Query is the search query string.
Query string `json:"query"`
// Start is the start timestamp, events before this time are not included. This field is required.
Start time.Time `json:"start"`
// Stop is the stop timestamp, events after this time are not included. This field is required.
Stop time.Time `json:"stop"`
}
// SearchOutput params.
type SearchOutput struct {
// Events is the query search results. This field is required.
Events []Event `json:"events"`
// Stats is the query statistics. This field is required.
Stats QueryStats `json:"stats"`
}
// TestAlertInput params.
type TestAlertInput struct {
// Alert is the alert. This field is required.
Alert Alert `json:"alert"`
}
// UpdateAlertInput params.
type UpdateAlertInput struct {
// Alert is the alert. This field is required.
Alert Alert `json:"alert"`
}
// UpdateNotificationInput params.
type UpdateNotificationInput struct {
// Notification is the notification. This field is required.
Notification Notification `json:"notification"`
}
// UpdateProjectInput params.
type UpdateProjectInput struct {
// Project is the project. This field is required.
Project Project `json:"project"`
}
// UpdateSearchInput params.
type UpdateSearchInput struct {
// Search is the saved search. This field is required.
Search Search `json:"search"`
}
// Client is the API client.
type Client struct {
// URL is the required API endpoint address.
URL string
// AuthToken is an optional authentication token.
AuthToken string
// HTTPClient is the client used for making requests, defaulting to http.DefaultClient.
HTTPClient *http.Client
}
// AddAlert creates a new alert.
func (c *Client) AddAlert(in AddAlertInput) (*AddAlertOutput, error) {
var out AddAlertOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "add_alert", in, &out)
}
// AddEvents ingests a batch of events.
func (c *Client) AddEvents(in AddEventsInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "add_events", in, nil)
}
// AddNotification creates a new notification.
func (c *Client) AddNotification(in AddNotificationInput) (*AddNotificationOutput, error) {
var out AddNotificationOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "add_notification", in, &out)
}
// AddProject creates a new project.
func (c *Client) AddProject(in AddProjectInput) (*AddProjectOutput, error) {
var out AddProjectOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "add_project", in, &out)
}
// AddSearch creates a new saved search.
func (c *Client) AddSearch(in AddSearchInput) (*AddSearchOutput, error) {
var out AddSearchOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "add_search", in, &out)
}
// AddToken creates a new token.
func (c *Client) AddToken(in AddTokenInput) (*AddTokenOutput, error) {
var out AddTokenOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "add_token", in, &out)
}
// GetAlert returns an alert.
func (c *Client) GetAlert(in GetAlertInput) (*GetAlertOutput, error) {
var out GetAlertOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_alert", in, &out)
}
// GetAlerts returns all alerts in a project.
func (c *Client) GetAlerts(in GetAlertsInput) (*GetAlertsOutput, error) {
var out GetAlertsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_alerts", in, &out)
}
// GetBooleanFieldStats returns field statistics for a boolean field.
func (c *Client) GetBooleanFieldStats(in GetBooleanFieldStatsInput) (*GetBooleanFieldStatsOutput, error) {
var out GetBooleanFieldStatsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_boolean_field_stats", in, &out)
}
// GetCount performs a search query against the log events, returning the number of matches.
func (c *Client) GetCount(in GetCountInput) (*GetCountOutput, error) {
var out GetCountOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_count", in, &out)
}
// GetDiscoveredFields returns fields discovered in the provided time range.
func (c *Client) GetDiscoveredFields(in GetDiscoveredFieldsInput) (*GetDiscoveredFieldsOutput, error) {
var out GetDiscoveredFieldsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_discovered_fields", in, &out)
}
// GetInstanceConfig returns instance configuration.
func (c *Client) GetInstanceConfig() (*GetInstanceConfigOutput, error) {
var out GetInstanceConfigOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_instance_config", nil, &out)
}
// GetNotification returns a notification.
func (c *Client) GetNotification(in GetNotificationInput) (*GetNotificationOutput, error) {
var out GetNotificationOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_notification", in, &out)
}
// GetNotifications returns all notifications.
func (c *Client) GetNotifications(in GetNotificationsInput) (*GetNotificationsOutput, error) {
var out GetNotificationsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_notifications", in, &out)
}
// GetNumericFieldStats returns field statistics for a numeric field.
func (c *Client) GetNumericFieldStats(in GetNumericFieldStatsInput) (*GetNumericFieldStatsOutput, error) {
var out GetNumericFieldStatsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_numeric_field_stats", in, &out)
}
// GetProjectStats returns project statistics.
func (c *Client) GetProjectStats(in GetProjectStatsInput) (*GetProjectStatsOutput, error) {
var out GetProjectStatsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_project_stats", in, &out)
}
// GetProjects returns all projects.
func (c *Client) GetProjects() (*GetProjectsOutput, error) {
var out GetProjectsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_projects", nil, &out)
}
// GetSearches returns all saved searches in a project.
func (c *Client) GetSearches(in GetSearchesInput) (*GetSearchesOutput, error) {
var out GetSearchesOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_searches", in, &out)
}
// GetStringFieldStats returns field statistics for a string field.
func (c *Client) GetStringFieldStats(in GetStringFieldStatsInput) (*GetStringFieldStatsOutput, error) {
var out GetStringFieldStatsOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_string_field_stats", in, &out)
}
// GetTimeseries returns a timeseries of event counts in the provided time range.
func (c *Client) GetTimeseries(in GetTimeseriesInput) (*GetTimeseriesOutput, error) {
var out GetTimeseriesOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_timeseries", in, &out)
}
// GetTokens returns all tokens.
func (c *Client) GetTokens() (*GetTokensOutput, error) {
var out GetTokensOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "get_tokens", nil, &out)
}
// Query performs a SQL query against the log events.
func (c *Client) Query(in QueryInput) (*QueryOutput, error) {
var out QueryOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "query", in, &out)
}
// RemoveAlert removes an alert.
func (c *Client) RemoveAlert(in RemoveAlertInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "remove_alert", in, nil)
}
// RemoveNotification removes a notification.
func (c *Client) RemoveNotification(in RemoveNotificationInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "remove_notification", in, nil)
}
// RemoveProject removes a project.
func (c *Client) RemoveProject(in RemoveProjectInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "remove_project", in, nil)
}
// RemoveSearch removes a saved search.
func (c *Client) RemoveSearch(in RemoveSearchInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "remove_search", in, nil)
}
// RemoveToken removes a token.
func (c *Client) RemoveToken(in RemoveTokenInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "remove_token", in, nil)
}
// Search performs a search query against the log events.
func (c *Client) Search(in SearchInput) (*SearchOutput, error) {
var out SearchOutput
return &out, call(c.HTTPClient, c.AuthToken, c.URL, "search", in, &out)
}
// TestAlert test the alert configuration.
func (c *Client) TestAlert(in TestAlertInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "test_alert", in, nil)
}
// UpdateAlert updates an alert.
func (c *Client) UpdateAlert(in UpdateAlertInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "update_alert", in, nil)
}
// UpdateNotification updates a notification.
func (c *Client) UpdateNotification(in UpdateNotificationInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "update_notification", in, nil)
}
// UpdateProject updates a project.
func (c *Client) UpdateProject(in UpdateProjectInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "update_project", in, nil)
}
// UpdateSearch updates a saved search.
func (c *Client) UpdateSearch(in UpdateSearchInput) error {
return call(c.HTTPClient, c.AuthToken, c.URL, "update_search", in, nil)
}
// ErrInvalidScheme is returned when HTTP is used instead of HTTPS.
var ErrInvalidScheme = errors.New("Client.URL must be HTTPS, not HTTP")
// Error is an error returned by the client.
type Error struct {
Status string
StatusCode int
Type string
Message string
}
// Error implementation.
func (e Error) Error() string {
if e.Type == "" {
return fmt.Sprintf("%s: %d", e.Status, e.StatusCode)
}
return fmt.Sprintf("%s: %s", e.Type, e.Message)
}
// call implementation.
func call(client *http.Client, authToken, endpoint, method string, in, out interface{}) error {
var body io.Reader
// parse the URL
u, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("parsing url: %w", err)
}
// ensure HTTPS
if u.Scheme == "http" {
return ErrInvalidScheme
}
// default client
if client == nil {
client = http.DefaultClient
}
// input params
if in != nil {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(in)
if err != nil {
return fmt.Errorf("encoding: %w", err)
}
body = &buf
}
// POST request
req, err := http.NewRequest("POST", endpoint+"/"+method, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
// auth token
if authToken != "" {
req.Header.Set("Authorization", "Bearer "+authToken)
}
// response
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
// error
if res.StatusCode >= 300 {
var e Error
if res.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return err
}
}
e.Status = http.StatusText(res.StatusCode)
e.StatusCode = res.StatusCode
return e
}
// output params
if out != nil {
err = json.NewDecoder(res.Body).Decode(out)
if err != nil {
return err
}
}
return nil
}