2015-02-18 21:40:55 +00:00
// Copyright 2013 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2013-07-02 16:16:39 +00:00
//
2015-02-18 21:40:55 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-07-02 16:16:39 +00:00
package main
import (
2018-09-22 01:09:36 +00:00
"bufio"
2019-10-10 02:08:25 +00:00
"fmt"
2013-07-02 16:16:39 +00:00
"net"
"net/http"
2019-04-22 11:17:06 +00:00
_ "net/http/pprof"
2018-09-22 01:09:36 +00:00
"os"
2019-04-22 23:31:24 +00:00
"os/signal"
2013-07-02 16:16:39 +00:00
"strconv"
2019-04-22 23:31:24 +00:00
"syscall"
2013-07-02 16:16:39 +00:00
2019-10-10 02:08:25 +00:00
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
2013-07-02 16:16:39 +00:00
"github.com/prometheus/client_golang/prometheus"
2019-06-11 10:05:21 +00:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2019-10-10 02:08:25 +00:00
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
2016-05-04 19:16:17 +00:00
"github.com/prometheus/common/version"
2018-08-20 20:12:45 +00:00
"gopkg.in/alecthomas/kingpin.v2"
2018-08-10 12:28:38 +00:00
2018-08-14 09:20:00 +00:00
"github.com/prometheus/statsd_exporter/pkg/mapper"
2013-07-02 16:16:39 +00:00
)
2016-05-04 19:16:17 +00:00
func init ( ) {
prometheus . MustRegister ( version . NewCollector ( "statsd_exporter" ) )
}
2016-05-03 17:16:44 +00:00
2020-01-10 14:02:59 +00:00
func serveHTTP ( listenAddress , metricsEndpoint string , logger log . Logger ) {
2019-06-11 10:05:21 +00:00
http . Handle ( metricsEndpoint , promhttp . Handler ( ) )
2015-10-10 00:26:05 +00:00
http . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
w . Write ( [ ] byte ( ` < html >
2018-01-30 20:01:29 +00:00
< head > < title > StatsD Exporter < / title > < / head >
2015-10-10 00:26:05 +00:00
< body >
2018-01-30 20:01:29 +00:00
< h1 > StatsD Exporter < / h1 >
2018-08-20 20:12:45 +00:00
< p > < a href = "` + metricsEndpoint + `" > Metrics < / a > < / p >
2015-10-10 00:26:05 +00:00
< / body >
< / html > ` ) )
} )
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , http . ListenAndServe ( listenAddress , nil ) )
os . Exit ( 1 )
2013-07-02 16:16:39 +00:00
}
2020-01-10 14:02:59 +00:00
func ipPortFromString ( addr string ) ( * net . IPAddr , int , error ) {
2015-02-11 21:30:57 +00:00
host , portStr , err := net . SplitHostPort ( addr )
2013-07-02 16:16:39 +00:00
if err != nil {
2020-01-10 14:02:59 +00:00
return nil , 0 , fmt . Errorf ( "bad StatsD listening address: %s" , addr )
2013-07-02 16:16:39 +00:00
}
if host == "" {
host = "0.0.0.0"
}
ip , err := net . ResolveIPAddr ( "ip" , host )
if err != nil {
2020-01-10 14:02:59 +00:00
return nil , 0 , fmt . Errorf ( "Unable to resolve %s: %s" , host , err )
2013-07-02 16:16:39 +00:00
}
port , err := strconv . Atoi ( portStr )
if err != nil || port < 0 || port > 65535 {
2020-01-10 14:02:59 +00:00
return nil , 0 , fmt . Errorf ( "Bad port %s: %s" , portStr , err )
2013-07-02 16:16:39 +00:00
}
2020-01-10 14:02:59 +00:00
return ip , port , nil
2017-08-01 10:21:00 +00:00
}
2020-01-10 14:02:59 +00:00
func udpAddrFromString ( addr string ) ( * net . UDPAddr , error ) {
ip , port , err := ipPortFromString ( addr )
if err != nil {
return nil , err
}
2013-07-02 16:16:39 +00:00
return & net . UDPAddr {
IP : ip . IP ,
Port : port ,
Zone : ip . Zone ,
2020-01-10 14:02:59 +00:00
} , nil
2013-07-02 16:16:39 +00:00
}
2020-01-10 14:02:59 +00:00
func tcpAddrFromString ( addr string ) ( * net . TCPAddr , error ) {
ip , port , err := ipPortFromString ( addr )
if err != nil {
return nil , err
}
2017-08-01 10:21:00 +00:00
return & net . TCPAddr {
IP : ip . IP ,
Port : port ,
Zone : ip . Zone ,
2020-01-10 14:02:59 +00:00
} , nil
2017-08-01 10:21:00 +00:00
}
2020-01-02 13:39:39 +00:00
func configReloader ( fileName string , mapper * mapper . MetricMapper , cacheSize int , logger log . Logger , option mapper . CacheOption ) {
2013-07-08 18:37:12 +00:00
2019-07-05 13:27:47 +00:00
signals := make ( chan os . Signal , 1 )
signal . Notify ( signals , syscall . SIGHUP )
2013-07-08 18:37:12 +00:00
2019-07-05 13:27:47 +00:00
for s := range signals {
if fileName == "" {
2019-10-10 02:08:25 +00:00
level . Warn ( logger ) . Log ( "msg" , "Received signal but no mapping config to reload" , "signal" , s )
2019-07-05 13:27:47 +00:00
continue
}
2019-10-10 02:08:25 +00:00
level . Info ( logger ) . Log ( "msg" , "Received signal, attempting reload" , "signal" , s )
2020-01-02 13:39:39 +00:00
err := mapper . InitFromFile ( fileName , cacheSize , option )
2019-07-05 13:27:47 +00:00
if err != nil {
2019-10-10 02:08:25 +00:00
level . Info ( logger ) . Log ( "msg" , "Error reloading config" , "error" , err )
2019-07-05 13:27:47 +00:00
configLoads . WithLabelValues ( "failure" ) . Inc ( )
} else {
2019-10-10 02:08:25 +00:00
level . Info ( logger ) . Log ( "msg" , "Config reloaded successfully" )
2019-07-05 13:27:47 +00:00
configLoads . WithLabelValues ( "success" ) . Inc ( )
2013-07-08 18:37:12 +00:00
}
}
}
2019-10-10 02:08:25 +00:00
func dumpFSM ( mapper * mapper . MetricMapper , dumpFilename string , logger log . Logger ) error {
2018-09-22 01:09:36 +00:00
f , err := os . Create ( dumpFilename )
if err != nil {
return err
}
2019-10-10 02:08:25 +00:00
level . Info ( logger ) . Log ( "msg" , "Start dumping FSM" , "file_name" , dumpFilename )
2018-09-22 01:09:36 +00:00
w := bufio . NewWriter ( f )
mapper . FSM . DumpFSM ( w )
w . Flush ( )
f . Close ( )
2019-10-10 02:08:25 +00:00
level . Info ( logger ) . Log ( "msg" , "Finish dumping FSM" )
2018-09-22 01:09:36 +00:00
return nil
}
2013-07-02 16:16:39 +00:00
func main ( ) {
2018-08-20 20:12:45 +00:00
var (
2019-04-05 23:37:23 +00:00
listenAddress = kingpin . Flag ( "web.listen-address" , "The address on which to expose the web interface and generated Prometheus metrics." ) . Default ( ":9102" ) . String ( )
metricsEndpoint = kingpin . Flag ( "web.telemetry-path" , "Path under which to expose metrics." ) . Default ( "/metrics" ) . String ( )
statsdListenUDP = kingpin . Flag ( "statsd.listen-udp" , "The UDP address on which to receive statsd metric lines. \"\" disables it." ) . Default ( ":9125" ) . String ( )
statsdListenTCP = kingpin . Flag ( "statsd.listen-tcp" , "The TCP address on which to receive statsd metric lines. \"\" disables it." ) . Default ( ":9125" ) . String ( )
statsdListenUnixgram = kingpin . Flag ( "statsd.listen-unixgram" , "The Unixgram socket path to receive statsd metric lines in datagram. \"\" disables it." ) . Default ( "" ) . String ( )
// not using Int here because flag diplays default in decimal, 0755 will show as 493
2019-04-09 18:13:33 +00:00
statsdUnixSocketMode = kingpin . Flag ( "statsd.unixsocket-mode" , "The permission mode of the unix socket." ) . Default ( "755" ) . String ( )
mappingConfig = kingpin . Flag ( "statsd.mapping-config" , "Metric mapping configuration file name." ) . String ( )
readBuffer = kingpin . Flag ( "statsd.read-buffer" , "Size (in bytes) of the operating system's transmit read buffer associated with the UDP or Unixgram connection. Please make sure the kernel parameters net.core.rmem_max is set to a value greater than the value specified." ) . Int ( )
2019-04-03 15:36:34 +00:00
cacheSize = kingpin . Flag ( "statsd.cache-size" , "Maximum size of your metric mapping cache. Relies on least recently used replacement policy if max size is reached." ) . Default ( "1000" ) . Int ( )
2020-01-02 13:39:39 +00:00
cacheType = kingpin . Flag ( "statsd.cache-type" , "Metric mapping cache type. Valid options are \"lru\" and \"random\"" ) . Default ( "lru" ) . Enum ( "lru" , "random" )
2019-05-26 13:08:54 +00:00
eventQueueSize = kingpin . Flag ( "statsd.event-queue-size" , "Size of internal queue for processing events" ) . Default ( "10000" ) . Int ( )
eventFlushThreshold = kingpin . Flag ( "statsd.event-flush-threshold" , "Number of events to hold in queue before flushing" ) . Default ( "1000" ) . Int ( )
eventFlushInterval = kingpin . Flag ( "statsd.event-flush-interval" , "Number of events to hold in queue before flushing" ) . Default ( "200ms" ) . Duration ( )
2019-04-09 18:13:33 +00:00
dumpFSMPath = kingpin . Flag ( "debug.dump-fsm" , "The path to dump internal FSM generated for glob matching as Dot file." ) . Default ( "" ) . String ( )
2018-08-20 20:12:45 +00:00
)
2019-10-10 02:08:25 +00:00
promlogConfig := & promlog . Config { }
flag . AddFlags ( kingpin . CommandLine , promlogConfig )
2018-08-20 20:12:45 +00:00
kingpin . Version ( version . Print ( "statsd_exporter" ) )
kingpin . HelpFlag . Short ( 'h' )
kingpin . Parse ( )
2019-10-10 02:08:25 +00:00
logger := promlog . New ( promlogConfig )
2017-08-01 10:21:00 +00:00
2020-01-02 13:39:39 +00:00
cacheOption := mapper . WithCacheType ( * cacheType )
2019-04-05 23:37:23 +00:00
if * statsdListenUDP == "" && * statsdListenTCP == "" && * statsdListenUnixgram == "" {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "At least one of UDP/TCP/Unixgram listeners must be specified." )
os . Exit ( 1 )
2017-08-01 10:21:00 +00:00
}
2019-10-10 02:08:25 +00:00
level . Info ( logger ) . Log ( "msg" , "Starting StatsD -> Prometheus Exporter" , "version" , version . Info ( ) )
level . Info ( logger ) . Log ( "msg" , "Build context" , "context" , version . BuildContext ( ) )
level . Info ( logger ) . Log ( "msg" , "Accepting StatsD Traffic" , "udp" , * statsdListenUDP , "tcp" , * statsdListenTCP , "unixgram" , * statsdListenUnixgram )
level . Info ( logger ) . Log ( "msg" , "Accepting Prometheus Requests" , "addr" , * listenAddress )
2013-07-03 13:25:52 +00:00
2020-01-10 14:02:59 +00:00
go serveHTTP ( * listenAddress , * metricsEndpoint , logger )
2013-07-02 16:16:39 +00:00
2019-05-26 13:08:54 +00:00
events := make ( chan Events , * eventQueueSize )
2013-07-02 16:16:39 +00:00
defer close ( events )
2019-05-26 13:08:54 +00:00
eventQueue := newEventQueue ( events , * eventFlushThreshold , * eventFlushInterval )
2013-07-02 16:16:39 +00:00
2017-08-01 10:21:00 +00:00
if * statsdListenUDP != "" {
2020-01-10 14:02:59 +00:00
udpListenAddr , err := udpAddrFromString ( * statsdListenUDP )
if err != nil {
level . Error ( logger ) . Log ( "msg" , "invalid UDP listen address" , "address" , * statsdListenUDP , "error" , err )
os . Exit ( 1 )
}
2017-08-01 10:21:00 +00:00
uconn , err := net . ListenUDP ( "udp" , udpListenAddr )
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "failed to start UDP listener" , "error" , err )
os . Exit ( 1 )
2017-08-01 10:21:00 +00:00
}
if * readBuffer != 0 {
err = uconn . SetReadBuffer ( * readBuffer )
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "error setting UDP read buffer" , "error" , err )
os . Exit ( 1 )
2017-08-01 10:21:00 +00:00
}
}
2020-01-13 09:55:07 +00:00
ul := & StatsDUDPListener { conn : uconn , eventHandler : eventQueue , logger : logger }
2019-05-26 13:08:54 +00:00
go ul . Listen ( )
2013-07-02 16:16:39 +00:00
}
2015-09-09 11:58:18 +00:00
2017-08-01 10:21:00 +00:00
if * statsdListenTCP != "" {
2020-01-10 14:02:59 +00:00
tcpListenAddr , err := tcpAddrFromString ( * statsdListenTCP )
if err != nil {
level . Error ( logger ) . Log ( "msg" , "invalid TCP listen address" , "address" , * statsdListenUDP , "error" , err )
os . Exit ( 1 )
}
2017-08-01 10:21:00 +00:00
tconn , err := net . ListenTCP ( "tcp" , tcpListenAddr )
2015-09-09 11:58:18 +00:00
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , err )
os . Exit ( 1 )
2015-09-09 11:58:18 +00:00
}
2017-08-01 10:21:00 +00:00
defer tconn . Close ( )
2015-09-09 11:58:18 +00:00
2019-10-10 02:08:25 +00:00
tl := & StatsDTCPListener { conn : tconn , eventHandler : eventQueue , logger : logger }
2019-05-26 13:08:54 +00:00
go tl . Listen ( )
2017-08-01 10:21:00 +00:00
}
2013-07-02 16:16:39 +00:00
2019-04-05 23:37:23 +00:00
if * statsdListenUnixgram != "" {
2019-04-22 23:31:24 +00:00
var err error
if _ , err = os . Stat ( * statsdListenUnixgram ) ; ! os . IsNotExist ( err ) {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "Unixgram socket already exists" , "socket_name" , * statsdListenUnixgram )
os . Exit ( 1 )
2019-04-05 23:37:23 +00:00
}
uxgconn , err := net . ListenUnixgram ( "unixgram" , & net . UnixAddr {
Net : "unixgram" ,
Name : * statsdListenUnixgram ,
} )
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "failed to listen on Unixgram socket" , "error" , err )
os . Exit ( 1 )
2019-04-05 23:37:23 +00:00
}
defer uxgconn . Close ( )
if * readBuffer != 0 {
err = uxgconn . SetReadBuffer ( * readBuffer )
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "error setting Unixgram read buffer" , "error" , err )
os . Exit ( 1 )
2019-04-05 23:37:23 +00:00
}
}
2019-10-10 02:08:25 +00:00
ul := & StatsDUnixgramListener { conn : uxgconn , eventHandler : eventQueue , logger : logger }
2019-05-26 13:08:54 +00:00
go ul . Listen ( )
2019-04-05 23:37:23 +00:00
// if it's an abstract unix domain socket, it won't exist on fs
// so we can't chmod it either
if _ , err := os . Stat ( * statsdListenUnixgram ) ; ! os . IsNotExist ( err ) {
2019-04-22 23:31:24 +00:00
defer os . Remove ( * statsdListenUnixgram )
2019-04-05 23:37:23 +00:00
// convert the string to octet
2019-04-09 18:13:33 +00:00
perm , err := strconv . ParseInt ( "0" + string ( * statsdUnixSocketMode ) , 8 , 32 )
2019-04-05 23:37:23 +00:00
if err != nil {
2019-10-10 02:08:25 +00:00
level . Warn ( logger ) . Log ( "Bad permission %s: %v, ignoring\n" , * statsdUnixSocketMode , err )
2019-04-05 23:37:23 +00:00
} else {
err = os . Chmod ( * statsdListenUnixgram , os . FileMode ( perm ) )
if err != nil {
2019-10-10 02:08:25 +00:00
level . Warn ( logger ) . Log ( "Failed to change unixgram socket permission: %v" , err )
2019-04-05 23:37:23 +00:00
}
}
}
}
2018-08-14 09:31:38 +00:00
mapper := & mapper . MetricMapper { MappingsCount : mappingsCount }
2013-07-08 18:37:12 +00:00
if * mappingConfig != "" {
2020-01-02 13:39:39 +00:00
err := mapper . InitFromFile ( * mappingConfig , * cacheSize , cacheOption )
2013-07-05 17:31:53 +00:00
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "error loading config" , "error" , err )
os . Exit ( 1 )
2013-07-05 17:31:53 +00:00
}
2018-09-22 01:09:36 +00:00
if * dumpFSMPath != "" {
2019-10-10 02:08:25 +00:00
err := dumpFSM ( mapper , * dumpFSMPath , logger )
2018-09-22 01:09:36 +00:00
if err != nil {
2020-01-10 14:02:59 +00:00
level . Error ( logger ) . Log ( "msg" , "error dumping FSM" , "error" , err )
// Failure to dump the FSM is an error (the user asked for it and it
// didn't happen) but not fatal (the exporter is fully functional
// afterwards).
2018-09-22 01:09:36 +00:00
}
}
2019-04-03 15:36:34 +00:00
} else {
2020-01-02 13:39:39 +00:00
mapper . InitCache ( * cacheSize , cacheOption )
2013-07-05 17:31:53 +00:00
}
2019-07-05 13:27:47 +00:00
2020-01-02 13:39:39 +00:00
go configReloader ( * mappingConfig , mapper , * cacheSize , logger , cacheOption )
2019-07-05 13:27:47 +00:00
2019-10-10 02:08:25 +00:00
exporter := NewExporter ( mapper , logger )
2019-04-22 23:31:24 +00:00
2019-04-22 23:35:12 +00:00
signals := make ( chan os . Signal , 1 )
2019-04-22 23:31:24 +00:00
signal . Notify ( signals , os . Interrupt , syscall . SIGTERM )
go exporter . Listen ( events )
<- signals
2013-07-02 16:16:39 +00:00
}