Matteo Scandolo | a8bd93e | 2018-09-13 13:36:50 -0700 | [diff] [blame] | 1 | // Copyright 2018 Open Networking Foundation |
| 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 | |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 15 | package main |
| 16 | |
| 17 | import ( |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 18 | "gerrit.opencord.org/kafka-topic-exporter/common/logger" |
| 19 | "github.com/Shopify/sarama" |
| 20 | "github.com/prometheus/client_golang/prometheus" |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 21 | "gopkg.in/yaml.v2" |
| 22 | "io/ioutil" |
| 23 | "log" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 24 | "net/http" |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 25 | "strconv" |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 26 | "strings" |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 27 | "sync" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 30 | func kafkaInit(broker BrokerInfo) { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 31 | config := sarama.NewConfig() |
| 32 | config.Consumer.Return.Errors = true |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 33 | var wg sync.WaitGroup |
| 34 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 35 | master, err := sarama.NewConsumer([]string{broker.Host}, config) |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 36 | |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 37 | if err != nil { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 38 | logger.Panic("kafkaInit panic") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 39 | panic(err) |
| 40 | } |
| 41 | defer func() { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 42 | logger.Debug("kafkaInit close connection") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 43 | if err := master.Close(); err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | }() |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 47 | |
| 48 | // read topics from config |
| 49 | topics := broker.Topics |
| 50 | |
| 51 | // we are spinning threads for each topic, we need to wait for |
| 52 | // them to exit before stopping the kafka connection |
| 53 | wg.Add(len(topics)) |
| 54 | |
| 55 | for _, topic := range topics { |
| 56 | t := topic |
| 57 | go topicListener(&t, master, wg) |
| 58 | } |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 59 | |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 60 | wg.Wait() |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 63 | func runServer(target TargetInfo) { |
| 64 | if target.Port == 0 { |
| 65 | logger.Warn("Prometheus target port not configured, using default 8080") |
| 66 | target.Port = 8080 |
| 67 | } |
| 68 | logger.Debug("Starting HTTP Server on %d port", target.Port) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 69 | http.Handle("/metrics", prometheus.Handler()) |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 70 | http.ListenAndServe(":"+strconv.Itoa(target.Port), nil) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func init() { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 74 | // register metrics within Prometheus |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 75 | prometheus.MustRegister(volthaTxBytesTotal) |
| 76 | prometheus.MustRegister(volthaRxBytesTotal) |
| 77 | prometheus.MustRegister(volthaTxPacketsTotal) |
| 78 | prometheus.MustRegister(volthaRxPacketsTotal) |
| 79 | prometheus.MustRegister(volthaTxErrorPacketsTotal) |
| 80 | prometheus.MustRegister(volthaRxErrorPacketsTotal) |
| 81 | |
| 82 | prometheus.MustRegister(onosTxBytesTotal) |
| 83 | prometheus.MustRegister(onosRxBytesTotal) |
| 84 | prometheus.MustRegister(onosTxPacketsTotal) |
| 85 | prometheus.MustRegister(onosRxPacketsTotal) |
| 86 | prometheus.MustRegister(onosTxDropPacketsTotal) |
| 87 | prometheus.MustRegister(onosRxDropPacketsTotal) |
kartikey dubey | 72ef3b8 | 2019-05-27 06:50:04 +0000 | [diff] [blame] | 88 | |
| 89 | prometheus.MustRegister(onosaaaRxAcceptResponses) |
| 90 | prometheus.MustRegister(onosaaaRxRejectResponses) |
| 91 | prometheus.MustRegister(onosaaaRxChallengeResponses) |
| 92 | prometheus.MustRegister(onosaaaTxAccessRequests) |
| 93 | prometheus.MustRegister(onosaaaRxInvalidValidators) |
| 94 | prometheus.MustRegister(onosaaaRxUnknownType) |
| 95 | prometheus.MustRegister(onosaaaPendingRequests) |
| 96 | prometheus.MustRegister(onosaaaRxDroppedResponses) |
| 97 | prometheus.MustRegister(onosaaaRxMalformedResponses) |
| 98 | prometheus.MustRegister(onosaaaRxUnknownserver) |
| 99 | prometheus.MustRegister(onosaaaRequestRttMillis) |
| 100 | prometheus.MustRegister(onosaaaRequestReTx) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 103 | func loadConfigFile() Config { |
| 104 | m := Config{} |
| 105 | // this file path is configmap mounted in pod yaml |
| 106 | yamlFile, err := ioutil.ReadFile("/etc/config/conf.yaml") |
| 107 | if err != nil { |
| 108 | log.Printf("yamlFile.Get err: %v ", err) |
| 109 | } |
| 110 | err = yaml.Unmarshal(yamlFile, &m) |
| 111 | if err != nil { |
| 112 | log.Fatalf("Unmarshal: %v", err) |
| 113 | } |
| 114 | return m |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 115 | } |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 116 | |
| 117 | func main() { |
| 118 | // load configuration |
| 119 | conf := loadConfigFile() |
| 120 | |
| 121 | // logger setup |
| 122 | logger.Setup(conf.Logger.Host, strings.ToUpper(conf.Logger.LogLevel)) |
| 123 | logger.Info("Connecting to broker: [%s]", conf.Broker.Host) |
| 124 | |
| 125 | go kafkaInit(conf.Broker) |
| 126 | runServer(conf.Target) |
| 127 | } |