To monitor a Go application by using Managed Service for Prometheus, you can instrument the Go application to expose data, use Managed Service for Prometheus to collect data, configure a Grafana dashboard to display the data, and then create an alert rule. This topic describes how to use Managed Service for Prometheus to monitor a Go application. In this example, Container Service for Kubernetes (ACK) and Container Registry are used.
Prerequisites
Before you begin, make sure that the following requirements are met:
An ACK cluster is monitored by Managed Service for Prometheus. For more information, see Create a Prometheus instance to monitor an ACK cluster.
An image repository is created in Container Registry. For more information, see Step 2: Create an image repository.
Sample project
You can also use a sample project to learn how to monitor a Go application by using Managed Service for Prometheus. For more information about the project, visit GitHub.
Procedure
The following figure shows the process of using Managed Service for Prometheus to monitor a Go application.
Step 1: Instrument a Go application
To instrument a Go application to expose data, perform the following steps:
Import the monitoring package to the Go application.
import ( "fmt" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" "strconv" )
Associate the HTTP endpoint of Managed Service for Prometheus with promhttp.Handler().
http.Handle(path, promhttp.Handler()) // Initialize an HTTP handler.
Step 2: Upload the Go application
To build an image for the instrumented Go application and upload the image to an image repository of Container Registry, perform the following steps:
Run the following command to recompile a module:
go build
Run the following command to build an image:
docker build -t <Name of the local temporary Docker image>:<Version number of the local temporary Docker image> . --no-cache
Example:
docker build -t prometheus-go-demo:v0 . --no-cache
Run the following command to tag the image:
sudo docker tag <Name of the local temporary Docker image>:<Version number of the local temporary Docker image> <Domain name of the image repository>/<Namespace>/<Image name>:<Image version number>
Example:
sudo docker tag prometheus-go-demo:v0 registry.cn-hangzhou.aliyuncs.com/testnamespace/prometheus-go-demo:v0
Run the following command to upload the image to the image repository:
sudo docker push <Domain name of the image repository>/<Namespace>/<Image name>:<Image version number>
Example:
sudo docker push registry.cn-hangzhou.aliyuncs.com/testnamespace/prometheus-go-demo:v0
You can view the information about the uploaded application image on the Tags page of the Container Registry console.
Step 3: Deploy the Go application
To deploy the Go application to an ACK cluster, perform the following steps:
- Log on to the ACK console.
In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster to which you want to deploy the application and click Applications in the Actions column.
Create a container group.
In the left-side navigation pane, choose .
On the Deployments page, click Create from YAML in the upper-right corner.
On the Create page, enter the following code in the Template code editor and click Create:
apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1 kind: Deployment metadata: name: prometheus-go-demo labels: app: go-exporter spec: replicas: 2 selector: matchLabels: app: go-exporter template: metadata: labels: app: go-exporter spec: containers: - name: prometheus-go-demo imagePullPolicy: Always image: <Domain name of the image repository>/<Namespace>/<Image name>:<Image version number> ports: - containerPort: 8077 name: arms-go-demo
Sample code:
apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1 kind: Deployment metadata: name: prometheus-go-demo labels: app: go-exporter spec: replicas: 2 selector: matchLabels: app: go-exporter template: metadata: labels: app: go-exporter spec: containers: - name: prometheus-go-demo imagePullPolicy: Always image: registry.cn-hangzhou.aliyuncs.com/fuling/prometheus-go-demo:v0 ports: - containerPort: 8077 name: arms-go-demo
The newly created container group is displayed on the Deployments page.
Create a Service.
In the left-side navigation pane, choose .
On the Services page, click Create Resources in YAML.
On the Create page, enter the following code in the Template code editor and click Create:
apiVersion: v1 kind: Service metadata: labels: app: prometheus-go-demo name: prometheus-go-demo spec: ports: - name: arms-go-demo port: 8077 protocol: TCP targetPort: 8077 type: NodePort selector: app: prometheus-go-demo
The newly created Service is displayed on the Services page.
Step 4: Configure a service discovery task
To configure a service discovery task of Managed Service for Prometheus to receive data from the Go application, perform the following steps:
- Log on to the ARMS console.
In the left-side navigation pane, choose .
In the top navigation bar of the Managed Service for Prometheus page, select the region where the monitored ACK cluster resides. Then, click the name of the Prometheus instance that monitors the ACK cluster.
In the left-side navigation pane, click Service Discovery. Then, click the Configure tab.
On the Service Discovery tab, click the ServiceMonitor tab. Then, click Add ServiceMonitor.
In the Add ServiceMonitor dialog box, enter the following code and click OK:
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: # Enter a unique name. name: prometheus-go-demo # Enter a namespace. namespace: default spec: endpoints: - interval: 30s # Enter the value of the Name field of the Port parameter of the Prometheus exporter. port: arms-go-demo # Enter the value of the Path field of the Prometheus exporter. path: /metrics namespaceSelector: any: true # The namespace of the demo. selector: matchLabels: app: prometheus-go-demo
The configured service discovery task is displayed on the ServiceMonitor tab.
Step 5: Configure a Grafana dashboard
To configure a Grafana dashboard to display data, perform the following steps:
Go to the homepage of Grafana dashboards.
NoteYou must activate Grafana Pro Edition.
In the left-side navigation pane, click theicon and select + Import.
On the Import page, enter 6671 (the ID of the Grafana template provided by Prometheus for Go applications) in the Import via grafana.com field and click Load next to the field.
On the Import page, perform the following operations and click Import:
Enter a dashboard name in the Name field.
Select your ACK cluster from the Folder drop-down list.
Select your ACK cluster from the prometheus-apl drop-down list.
The following figure shows the configured Grafana dashboard.
Step 6: Create an alert rule
For information about how to create an alert rule, see Create an alert rule for a Prometheus instance.