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 ( |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 18 | "flag" |
| 19 | "fmt" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 20 | "net/http" |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 21 | "sync" |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/Shopify/sarama" |
| 24 | "github.com/prometheus/client_golang/prometheus" |
| 25 | ) |
| 26 | |
| 27 | var ( |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 28 | broker = flag.String("broker", "voltha-kafka.default.svc.cluster.local:9092", "The Kafka broker") |
| 29 | volthaTopic = "voltha.kpis" |
| 30 | onosTopic = "onos.kpis" |
| 31 | |
| 32 | volthaTopicPointer = &volthaTopic |
| 33 | onosTopicPointer = &onosTopic |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
Matteo Scandolo | 8bc8ba6 | 2018-07-27 12:16:20 -0700 | [diff] [blame] | 36 | var brokers []string |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 37 | |
Matteo Scandolo | 8bc8ba6 | 2018-07-27 12:16:20 -0700 | [diff] [blame] | 38 | func kafkaInit(brokers []string) { |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 39 | config := sarama.NewConfig() |
| 40 | config.Consumer.Return.Errors = true |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 41 | var wg sync.WaitGroup |
| 42 | |
| 43 | wg.Add(2) // we are spinning up two thread and we need to wait for them to exit before stopping the kafka connection |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 44 | |
| 45 | master, err := sarama.NewConsumer(brokers, config) |
| 46 | if err != nil { |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 47 | fmt.Println("kafkaInit panic") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 48 | panic(err) |
| 49 | } |
| 50 | defer func() { |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 51 | fmt.Println("kafkaInit close connection") |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 52 | if err := master.Close(); err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | }() |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 56 | go VOLTHAListener(volthaTopicPointer, master, wg) |
| 57 | go ONOSListener(onosTopicPointer, master, wg) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 58 | |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 59 | wg.Wait() |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func runServer() { |
| 63 | fmt.Println("Starting Server") |
| 64 | http.Handle("/metrics", prometheus.Handler()) |
| 65 | http.ListenAndServe(":8080", nil) |
| 66 | } |
| 67 | |
| 68 | func init() { |
| 69 | |
| 70 | // read config from cli flags |
| 71 | flag.Parse() |
Matteo Scandolo | 8bc8ba6 | 2018-07-27 12:16:20 -0700 | [diff] [blame] | 72 | brokers = make([]string, 0) |
| 73 | brokers = []string{*broker} |
| 74 | fmt.Println("Connecting to broker: ", brokers) |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 75 | fmt.Println("Listening to voltha on topic: ", *volthaTopicPointer) |
| 76 | fmt.Println("Listening to onos on topic: ", *onosTopicPointer) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 77 | |
| 78 | // register metrics within Prometheus |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 79 | prometheus.MustRegister(volthaTxBytesTotal) |
| 80 | prometheus.MustRegister(volthaRxBytesTotal) |
| 81 | prometheus.MustRegister(volthaTxPacketsTotal) |
| 82 | prometheus.MustRegister(volthaRxPacketsTotal) |
| 83 | prometheus.MustRegister(volthaTxErrorPacketsTotal) |
| 84 | prometheus.MustRegister(volthaRxErrorPacketsTotal) |
| 85 | |
| 86 | prometheus.MustRegister(onosTxBytesTotal) |
| 87 | prometheus.MustRegister(onosRxBytesTotal) |
| 88 | prometheus.MustRegister(onosTxPacketsTotal) |
| 89 | prometheus.MustRegister(onosRxPacketsTotal) |
| 90 | prometheus.MustRegister(onosTxDropPacketsTotal) |
| 91 | prometheus.MustRegister(onosRxDropPacketsTotal) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func main() { |
Matteo Scandolo | 8bc8ba6 | 2018-07-27 12:16:20 -0700 | [diff] [blame] | 95 | go kafkaInit(brokers) |
Matteo Scandolo | 189526a | 2018-07-13 09:10:23 -0700 | [diff] [blame] | 96 | runServer() |
| 97 | } |