traces.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package hcso
  15. import (
  16. "fmt"
  17. "strconv"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. )
  23. type SUser struct {
  24. Domain map[string]string
  25. Name string
  26. Id string
  27. }
  28. type SEvent struct {
  29. TraceId string
  30. Code string
  31. TraceName string
  32. ResourceType string
  33. ApiVersion string
  34. SourceIp string
  35. TraceType string
  36. ServiceType string
  37. EventType string
  38. ProjectId string
  39. Request string
  40. Response string
  41. TrackerName string
  42. TraceStatus string
  43. Time int64
  44. ResourceId string
  45. ResourceName string
  46. User SUser
  47. RecordTime int64
  48. }
  49. func (event *SEvent) GetName() string {
  50. if len(event.ResourceName) > 0 {
  51. return event.ResourceName
  52. }
  53. if len(event.ResourceId) > 0 {
  54. return event.ResourceId
  55. }
  56. return event.TraceName
  57. }
  58. func (event *SEvent) GetService() string {
  59. return event.ServiceType
  60. }
  61. func (event *SEvent) GetAction() string {
  62. return event.TraceName
  63. }
  64. func (event *SEvent) GetResourceType() string {
  65. return event.ResourceType
  66. }
  67. func (event *SEvent) GetRequestId() string {
  68. return event.TraceId
  69. }
  70. func (event *SEvent) GetRequest() jsonutils.JSONObject {
  71. return jsonutils.Marshal(event)
  72. }
  73. func (event *SEvent) GetAccount() string {
  74. return event.User.Name
  75. }
  76. func (event *SEvent) IsSuccess() bool {
  77. code, _ := strconv.Atoi(event.Code)
  78. return code < 400
  79. }
  80. func (event *SEvent) GetCreatedAt() time.Time {
  81. return time.Unix(event.Time/1000, event.Time%1000)
  82. }
  83. func (self *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) {
  84. if !self.client.isMainProject {
  85. return nil, cloudprovider.ErrNotSupported
  86. }
  87. events, err := self.GetEvents(start, end)
  88. if err != nil {
  89. return nil, err
  90. }
  91. iEvents := []cloudprovider.ICloudEvent{}
  92. for i := range events {
  93. iEvents = append(iEvents, &events[i])
  94. }
  95. return iEvents, nil
  96. }
  97. func (self *SRegion) GetEvents(start time.Time, end time.Time) ([]SEvent, error) {
  98. events := []SEvent{}
  99. params := map[string]string{}
  100. if start.IsZero() {
  101. start = time.Now().AddDate(0, 0, -7)
  102. }
  103. if end.IsZero() {
  104. end = time.Now()
  105. }
  106. params["from"] = fmt.Sprintf("%d000", start.Unix())
  107. params["to"] = fmt.Sprintf("%d000", end.Unix())
  108. err := doListAllWithMarker(self.ecsClient.Traces.List, params, &events)
  109. if err != nil {
  110. return nil, errors.Wrap(err, "doListAllWithMarker")
  111. }
  112. return events, nil
  113. }