|
| 1 | +package pumps |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "github.com/Sirupsen/logrus" |
| 7 | + "github.com/TykTechnologies/tyk-pump/analytics" |
| 8 | + "github.com/mitchellh/mapstructure" |
| 9 | + "github.com/robertkowalski/graylog-golang" |
| 10 | + "strconv" |
| 11 | +) |
| 12 | + |
| 13 | +type GraylogPump struct { |
| 14 | + client *gelf.Gelf |
| 15 | + conf *GraylogConf |
| 16 | +} |
| 17 | + |
| 18 | +type GraylogConf struct { |
| 19 | + GraylogHost string `mapstructure:"host"` |
| 20 | + GraylogPort int `mapstructure:"port"` |
| 21 | + Tags []string `mapstructure: "tags"` |
| 22 | +} |
| 23 | + |
| 24 | +var graylogPrefix string = "graylog-pump" |
| 25 | + |
| 26 | +func (p *GraylogPump) New() Pump { |
| 27 | + newPump := GraylogPump{} |
| 28 | + return &newPump |
| 29 | +} |
| 30 | + |
| 31 | +func (p *GraylogPump) GetName() string { |
| 32 | + return "Graylog Pump" |
| 33 | +} |
| 34 | + |
| 35 | +func (p *GraylogPump) Init(conf interface{}) error { |
| 36 | + p.conf = &GraylogConf{} |
| 37 | + err := mapstructure.Decode(conf, &p.conf) |
| 38 | + if err != nil { |
| 39 | + log.WithFields(logrus.Fields{ |
| 40 | + "prefix": graylogPrefix, |
| 41 | + }).Fatal("Failed to decode configuration: ", err) |
| 42 | + } |
| 43 | + |
| 44 | + if p.conf.GraylogHost == "" { |
| 45 | + p.conf.GraylogHost = "localhost" |
| 46 | + } |
| 47 | + |
| 48 | + if p.conf.GraylogPort == 0 { |
| 49 | + p.conf.GraylogPort = 1000 |
| 50 | + } |
| 51 | + log.WithFields(logrus.Fields{ |
| 52 | + "prefix": graylogPrefix, |
| 53 | + }).Info("GraylogHost:", p.conf.GraylogHost) |
| 54 | + log.WithFields(logrus.Fields{ |
| 55 | + "prefix": graylogPrefix, |
| 56 | + }).Info("GraylogPort:", p.conf.GraylogPort) |
| 57 | + |
| 58 | + p.connect() |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (p *GraylogPump) connect() { |
| 63 | + p.client = gelf.New(gelf.Config{ |
| 64 | + GraylogPort: p.conf.GraylogPort, |
| 65 | + GraylogHostname: p.conf.GraylogHost, |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func (p *GraylogPump) WriteData(data []interface{}) error { |
| 70 | + log.WithFields(logrus.Fields{ |
| 71 | + "prefix": graylogPrefix, |
| 72 | + }).Info("Writing ", len(data), " records") |
| 73 | + |
| 74 | + if p.client == nil { |
| 75 | + p.connect() |
| 76 | + p.WriteData(data) |
| 77 | + } |
| 78 | + |
| 79 | + for _, item := range data { |
| 80 | + record := item.(analytics.AnalyticsRecord) |
| 81 | + |
| 82 | + rReq, err := base64.StdEncoding.DecodeString(record.RawRequest) |
| 83 | + if err != nil { |
| 84 | + log.WithFields(logrus.Fields{ |
| 85 | + "prefix": graylogPrefix, |
| 86 | + }).Fatal(err) |
| 87 | + } |
| 88 | + |
| 89 | + rResp, err := base64.StdEncoding.DecodeString(record.RawRequest) |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + log.WithFields(logrus.Fields{ |
| 93 | + "prefix": graylogPrefix, |
| 94 | + }).Fatal(err) |
| 95 | + } |
| 96 | + |
| 97 | + mapping := map[string]interface{}{ |
| 98 | + "method": record.Method, |
| 99 | + "path": record.Path, |
| 100 | + "response_code": strconv.Itoa(record.ResponseCode), |
| 101 | + "api_key": record.APIKey, |
| 102 | + "api_version": record.APIVersion, |
| 103 | + "api_name": record.APIName, |
| 104 | + "api_id": record.APIID, |
| 105 | + "org_id": record.OrgID, |
| 106 | + "oauth_id": record.OauthID, |
| 107 | + "raw_request": string(rReq), |
| 108 | + "request_time": strconv.Itoa(int(record.RequestTime)), |
| 109 | + "raw_response": string(rResp), |
| 110 | + } |
| 111 | + |
| 112 | + messageMap := map[string]string{} |
| 113 | + |
| 114 | + for _, key := range p.conf.Tags { |
| 115 | + if value, ok := mapping[key]; ok { |
| 116 | + messageMap[key] = value.(string) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + message, err := json.Marshal(messageMap) |
| 121 | + if err != nil { |
| 122 | + log.WithFields(logrus.Fields{ |
| 123 | + "prefix": graylogPrefix, |
| 124 | + }).Fatal(err) |
| 125 | + } |
| 126 | + |
| 127 | + gelfData := map[string]interface{}{ |
| 128 | + //"version": "1.1", |
| 129 | + "host": "tyk-pumps", |
| 130 | + "timestamp": record.TimeStamp.Unix(), |
| 131 | + "message": string(message), |
| 132 | + } |
| 133 | + |
| 134 | + gelfString, err := json.Marshal(gelfData) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + log.WithFields(logrus.Fields{ |
| 138 | + "prefix": graylogPrefix, |
| 139 | + }).Fatal(err) |
| 140 | + } |
| 141 | + |
| 142 | + log.WithFields(logrus.Fields{ |
| 143 | + "prefix": graylogPrefix, |
| 144 | + }).Info("Writing ", string(message)) |
| 145 | + |
| 146 | + p.client.Log(string(gelfString)) |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
0 commit comments