Skip to content

Commit

Permalink
Run Prometheus exporter inside IC Binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Marrero committed Mar 8, 2019
1 parent 6ab5983 commit 9cf4874
Show file tree
Hide file tree
Showing 162 changed files with 29,139 additions and 3,748 deletions.
82 changes: 80 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@
[[constraint]]
name = "github.com/nginxinc/nginx-plus-go-sdk"
version = "0.2.0"

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.9.2"

[[constraint]]
name = "github.com/nginxinc/nginx-prometheus-exporter"
version = "0.3.0"

# github.com/prometheus/client_golang "0.9.2" requires github.com/golang/protobuf "1.3.0"
[[override]]
name = "github.com/golang/protobuf"
version = "1.3.0"
29 changes: 25 additions & 4 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/controller"
"github.com/nginxinc/kubernetes-ingress/internal/handlers"
"github.com/nginxinc/kubernetes-ingress/internal/metrics"
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
"github.com/nginxinc/kubernetes-ingress/internal/nginx/plus"
"github.com/nginxinc/kubernetes-ingress/internal/utils"
Expand Down Expand Up @@ -98,6 +99,12 @@ The external address of the service is used when reporting the status of Ingress
`A Secret with a TLS certificate and key for TLS termination of every Ingress host for which TLS termination is enabled but the Secret is not specified.
Format: <namespace>/<name>. If the argument is not set, for such Ingress hosts NGINX will break any attempt to establish a TLS connection.
If the argument is set, but the Ingress controller is not able to fetch the Secret from Kubernetes API, the Ingress controller will fail to start.`)

enablePrometheusMetrics = flag.Bool("enable-prometheus-metrics", false,
"Enable exposing NGINX or NGINX Plus metrics in the Prometheus format")

prometheusMetricsListenPort = flag.String("prometheus-metrics-listen-port", "9113",
"Set the port where the Prometheus metrics are exposed. [1023 - 65535]")
)

func main() {
Expand Down Expand Up @@ -166,7 +173,7 @@ func main() {
nginxBinaryPath = "/usr/sbin/nginx-debug"
}

templateExecutor, err := nginx.NewTemplateExecutor(nginxConfTemplatePath, nginxIngressTemplatePath, *healthStatus, *nginxStatus, allowedCIDRs, *nginxStatusPort)
templateExecutor, err := nginx.NewTemplateExecutor(nginxConfTemplatePath, nginxIngressTemplatePath, *healthStatus, *nginxStatus, allowedCIDRs, *nginxStatusPort, *enablePrometheusMetrics)
if err != nil {
glog.Fatalf("Error creating TemplateExecutor: %v", err)
}
Expand Down Expand Up @@ -243,7 +250,7 @@ func main() {

var nginxAPI *plus.NginxAPIController
if *nginxPlus {
httpClient := getSocketClient()
httpClient := getSocketClient("/var/run/nginx-plus-api.sock")
nginxAPI, err = plus.NewNginxAPIController(&httpClient, "http://nginx-plus-api/api", local)
if err != nil {
glog.Fatalf("Failed to create NginxAPIController: %v", err)
Expand Down Expand Up @@ -298,6 +305,20 @@ func main() {
lbc.AddLeaderHandler(leaderHandler)
}

if *enablePrometheusMetrics {
if *nginxPlus {
go metrics.RunPrometheusListenerForNginxPlus(*prometheusMetricsListenPort, nginxAPI.GetClientPlus())

} else {
httpClient := getSocketClient("/var/run/nginx-status.sock")
client, err := metrics.NewNginxMetricsClient(&httpClient)
if err != nil {
glog.Fatalf("Error creating the Nginx client for Prometheus metrics: %v", err)
}
go metrics.RunPrometheusListenerForNginx(*prometheusMetricsListenPort, client)
}
}

go handleTermination(lbc, ngxc, nginxDone)
lbc.Run()

Expand Down Expand Up @@ -341,11 +362,11 @@ func handleTermination(lbc *controller.LoadBalancerController, ngxc *nginx.Contr
}

// getSocketClient gets an http.Client with the a unix socket transport.
func getSocketClient() http.Client {
func getSocketClient(sockPath string) http.Client {
return http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", "/var/run/nginx-plus-api.sock")
return net.Dial("unix", sockPath)
},
},
}
Expand Down
54 changes: 0 additions & 54 deletions deployments/daemon-set/nginx-ingress-with-prometheus.yaml

This file was deleted.

42 changes: 0 additions & 42 deletions deployments/daemon-set/nginx-ingress.yaml

This file was deleted.

57 changes: 0 additions & 57 deletions deployments/daemon-set/nginx-plus-ingress-with-prometheus.yaml

This file was deleted.

Loading

0 comments on commit 9cf4874

Please sign in to comment.