Skip to content

Prometheus

Enable prometheus plugin.

Quick start#

1.Install#

$ go get github.com/rookie-ninja/rk-boot/v2
$ go get github.com/rookie-ninja/rk-gin/v2
$ go get github.com/rookie-ninja/rk-db/postgres

2.Create boot.yaml#

---
gin:
  - name: user-service
    port: 8080
    enabled: true
    prom:
      enabled: true
postgres:
  - name: demo-db
    enabled: true
    addr: "localhost:5432"
    user: postgres              # Optional, default: postgres
    pass: pass                  # Optional, default: pass
    database:
      - name: demo
        plugins:
          prom: enabled         # Enable prometheus plugin
        autoCreate: true

3.Create main.go#

package main

import (
    "context"
    "github.com/gin-gonic/gin"
    "github.com/rookie-ninja/rk-boot/v2"
    "github.com/rookie-ninja/rk-db/postgres"
    "github.com/rookie-ninja/rk-entry/v2/middleware"
    "github.com/rookie-ninja/rk-gin/v2/boot"
    "github.com/rookie-ninja/rk-gin/v2/middleware/context"
    "gorm.io/gorm"
    "net/http"
    "time"
)

var userDb *gorm.DB

func main() {
    boot := rkboot.NewBoot()

    boot.Bootstrap(context.TODO())

    // Auto migrate database and init global userDb variable
    postgresEntry := rkpostgres.GetPostgresEntry("demo-db")
    userDb = postgresEntry.GetDB("demo")

    if !userDb.DryRun {
        userDb.AutoMigrate(&User{})
    }

    // Register APIs
    ginEntry := rkgin.GetGinEntry("user-service")

    // Register collector to prometheus
    // User can create own prometheus.Registry
    postgresEntry.RegisterPromMetrics(ginEntry.PromEntry.Registry)

    ginEntry.Router.GET("/v1/user", ListUsers)

    boot.WaitForShutdownSig(context.TODO())
}

// *************************************
// *************** Model ***************
// *************************************

type Base struct {
    CreatedAt time.Time      `yaml:"-" json:"-"`
    UpdatedAt time.Time      `yaml:"-" json:"-"`
    DeletedAt gorm.DeletedAt `yaml:"-" json:"-" gorm:"index"`
}

type User struct {
    Base
    Id   int    `yaml:"id" json:"id" gorm:"primaryKey"`
    Name string `yaml:"name" json:"name"`
}

func ListUsers(ctx *gin.Context) {
    userList := make([]User, 0)

    dbCtx := context.WithValue(ctx, rkmid.LoggerKey.String(), rkginctx.GetLogger(ctx))
    res := userDb.WithContext(dbCtx).Find(&userList)

    if res.Error != nil {
        ctx.JSON(http.StatusInternalServerError, res.Error)
        return
    }
    ctx.JSON(http.StatusOK, userList)
}

3.Run server#

$ go run main.go

2022-09-19T01:05:01.861+0800    INFO    postgres/boot.go:378       Bootstrap postgresEntry    {"eventId": "a568e408-38e8-4279-9701-2e58a6f73c36", "entryName": "demo-db", "entryType": "postgresEntry"}
2022-09-19T01:05:01.861+0800    INFO    postgres/boot.go:497       Creating database [demo]
2022-09-19T01:05:01.880+0800    INFO    postgres/boot.go:519       Creating database [demo] successs
2022-09-19T01:05:01.880+0800    INFO    postgres/boot.go:522       Connecting to database [demo]
2022-09-19T01:05:01.887+0800    INFO    postgres/boot.go:540       Connecting to database [demo] success
2022-09-19T01:05:01.887+0800    INFO    boot/gin_entry.go:672   Bootstrap GinEntry      {"eventId": "a568e408-38e8-4279-9701-2e58a6f73c36", "entryName": "user-service", "entryType": "GinEntry"}
2022-09-19T01:05:01.887+0800    INFO    boot/gin_entry.go:432   PromEntry: http://localhost:8080/metrics

4.Validate#

  • Send request
$ curl localhost:8080/v1/user
  • Access prom client

http://localhost:8080/metrics