|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2021 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "io/ioutil" |
| 27 | + "os" |
| 28 | + "os/signal" |
| 29 | + "syscall" |
| 30 | + "time" |
| 31 | + |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/exporter" |
| 33 | + |
| 34 | + "github.com/rs/zerolog/log" |
| 35 | + "github.com/spf13/cobra" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + cmdExporter = &cobra.Command{ |
| 40 | + Use: "exporter", |
| 41 | + Run: cmdExporterCheck, |
| 42 | + } |
| 43 | + |
| 44 | + exporterInput struct { |
| 45 | + listenAddress string |
| 46 | + |
| 47 | + endpoint string |
| 48 | + jwtFile string |
| 49 | + timeout time.Duration |
| 50 | + |
| 51 | + keyfile string |
| 52 | + } |
| 53 | +) |
| 54 | + |
| 55 | +func init() { |
| 56 | + f := cmdExporter.PersistentFlags() |
| 57 | + |
| 58 | + f.StringVar(&exporterInput.listenAddress, "server.address", ":9101", "Address the exporter will listen on (IP:port)") |
| 59 | + f.StringVar(&exporterInput.keyfile, "ssl.keyfile", "", "File containing TLS certificate used for the metrics server. Format equal to ArangoDB keyfiles") |
| 60 | + |
| 61 | + f.StringVar(&exporterInput.endpoint, "arangodb.endpoint", "http://127.0.0.1:8529", "Endpoint used to reach the ArangoDB server") |
| 62 | + f.StringVar(&exporterInput.jwtFile, "arangodb.jwt-file", "", "File containing the JWT for authentication with ArangoDB server") |
| 63 | + f.DurationVar(&exporterInput.timeout, "arangodb.timeout", time.Second*15, "Timeout of statistics requests for ArangoDB") |
| 64 | + |
| 65 | + cmdMain.AddCommand(cmdExporter) |
| 66 | +} |
| 67 | + |
| 68 | +func cmdExporterCheck(cmd *cobra.Command, args []string) { |
| 69 | + if err := cmdExporterCheckE(); err != nil { |
| 70 | + log.Error().Err(err).Msgf("Fatal") |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func onSigterm(f func()) { |
| 76 | + sigs := make(chan os.Signal, 1) |
| 77 | + |
| 78 | + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) |
| 79 | + |
| 80 | + go func() { |
| 81 | + defer f() |
| 82 | + <-sigs |
| 83 | + }() |
| 84 | +} |
| 85 | + |
| 86 | +func cmdExporterCheckE() error { |
| 87 | + p, err := exporter.NewPassthru(exporterInput.endpoint, func() (string, error) { |
| 88 | + if exporterInput.jwtFile == "" { |
| 89 | + return "", nil |
| 90 | + } |
| 91 | + |
| 92 | + data, err := ioutil.ReadFile(exporterInput.jwtFile) |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + |
| 97 | + return string(data), nil |
| 98 | + }, false, 15*time.Second) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + exporter := exporter.NewExporter(exporterInput.listenAddress, "/metrics", p) |
| 104 | + if exporterInput.keyfile != "" { |
| 105 | + if e, err := exporter.WithKeyfile(exporterInput.keyfile); err != nil { |
| 106 | + return err |
| 107 | + } else { |
| 108 | + if r, err := e.Start(); err != nil { |
| 109 | + return err |
| 110 | + } else { |
| 111 | + onSigterm(r.Stop) |
| 112 | + return r.Wait() |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + if r, err := exporter.Start(); err != nil { |
| 117 | + return err |
| 118 | + } else { |
| 119 | + onSigterm(r.Stop) |
| 120 | + return r.Wait() |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments