Skip to content

Commit

Permalink
Merge pull request #1172 from Redish101/v3-beta
Browse files Browse the repository at this point in the history
feat: Migrate Monitor Middleware
  • Loading branch information
ReneWerner87 authored Oct 9, 2024
2 parents 78ee238 + 2a73e75 commit 6741a0e
Show file tree
Hide file tree
Showing 12 changed files with 1,177 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/release-drafter-monitor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name-template: 'Monitor - v$RESOLVED_VERSION'
tag-template: 'monitor/v$RESOLVED_VERSION'
tag-prefix: monitor/v
include-paths:
- monitor
categories:
- title: '❗ Breaking Changes'
labels:
- '❗ BreakingChange'
- title: '🚀 New'
labels:
- '✏️ Feature'
- title: '🧹 Updates'
labels:
- '🧹 Updates'
- '🤖 Dependencies'
- title: '🐛 Fixes'
labels:
- '☢️ Bug'
- title: '📚 Documentation'
labels:
- '📒 Documentation'
change-template: '- $TITLE (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
exclude-contributors:
- dependabot
- dependabot[bot]
version-resolver:
major:
labels:
- 'major'
- '❗ BreakingChange'
minor:
labels:
- 'minor'
- '✏️ Feature'
patch:
labels:
- 'patch'
- '📒 Documentation'
- '☢️ Bug'
- '🤖 Dependencies'
- '🧹 Updates'
default: patch
template: |
$CHANGES
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...monitor/v$RESOLVED_VERSION
Thank you $CONTRIBUTORS for making this update possible.
19 changes: 19 additions & 0 deletions .github/workflows/release-drafter-monitor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release Drafter Monitor
on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- master
- main
paths:
- 'monitor/**'
jobs:
draft_release_casbin:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: release-drafter/release-drafter@v5
with:
config-name: release-drafter-monitor.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions .github/workflows/test-monitor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "Test Monitor"

on:
push:
branches:
- master
- main
paths:
- 'monitor/**'
pull_request:
paths:
- 'monitor/**'

jobs:
Tests:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- 1.22.x
- 1.23.x
steps:
- name: Fetch Repository
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '${{ matrix.go-version }}'
- name: Run Test
working-directory: ./monitor
run: go test -v -race ./...
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Repository for third party middlewares with dependencies.
* [JWT](./jwt/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+jwt%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-jwt.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
* [Loadshed](./loadshed/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+loadshed%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-loadshed.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
* [NewRelic](./fibernewrelic/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fibernewrelic%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fibernewrelic.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
* [Monitor](./monitor/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Monitor%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-monitor.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
* [Open Policy Agent](./opafiber/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+opafiber%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-opafiber.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
* [Otelfiber (OpenTelemetry)](./otelfiber/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+otelfiber%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-otelfiber.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
* [Paseto](./paseto/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+paseto%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-paseto.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
Expand Down
84 changes: 84 additions & 0 deletions monitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
id: monitor
---

# Monitor

![Release](https://img.shields.io/github/v/tag/gofiber/contrib?filter=monitor*)
![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg)
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg)
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg)

Monitor middleware for [Fiber](https://github.com/gofiber/fiber) that reports server metrics, inspired by [express-status-monitor](https://github.com/RafalWilinski/express-status-monitor)

![](https://i.imgur.com/nHAtBpJ.gif)

## Install

This middleware supports Fiber v3.

```
go get -u github.com/gofiber/fiber/v3
go get -u github.com/gofiber/contrib/monitor
```

### Signature

```go
monitor.New(config ...monitor.Config) fiber.Handler
```

### Config

| Property | Type | Description | Default |
| :--------- | :------------------------ | :----------------------------------------------------------------------------------- | :-------------------------------------------------------------------------- |
| Title | `string` | Metrics page title. | `Fiber Monitor` |
| Refresh | `time.Duration` | Refresh period. | `3 seconds` |
| APIOnly | `bool` | Whether the service should expose only the montioring API. | `false` |
| Next | `func(c *fiber.Ctx) bool` | Define a function to add custom fields. | `nil` |
| CustomHead | `string` | Custom HTML code to Head Section(Before End). | `empty` |
| FontURL | `string` | FontURL for specilt font resource path or URL. also you can use relative path. | `https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap` |
| ChartJsURL | `string` | ChartJsURL for specilt chartjs library, path or URL, also you can use relative path. | `https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.bundle.min.js` |

### Example

```go
package main

import (
"log"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/contrib/monitor"
)

func main() {
app := fiber.New()

// Initialize default config (Assign the middleware to /metrics)
app.Get("/metrics", monitor.New())

// Or extend your config for customization
// Assign the middleware to /metrics
// and change the Title to `MyService Metrics Page`
app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"}))

log.Fatal(app.Listen(":3000"))
}
```


## Default Config

```go
var ConfigDefault = Config{
Title: defaultTitle,
Refresh: defaultRefresh,
FontURL: defaultFontURL,
ChartJsURL: defaultChartJSURL,
CustomHead: defaultCustomHead,
APIOnly: false,
Next: nil,
}
```
132 changes: 132 additions & 0 deletions monitor/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package monitor

import (
"time"

"github.com/gofiber/fiber/v3"
)

// Config defines the config for middleware.
type Config struct {
// Metrics page title
//
// Optional. Default: "Fiber Monitor"
Title string

// Refresh period
//
// Optional. Default: 3 seconds
Refresh time.Duration

// Whether the service should expose only the monitoring API.
//
// Optional. Default: false
APIOnly bool

// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool

// Custom HTML Code to Head Section(Before End)
//
// Optional. Default: empty
CustomHead string

// FontURL to specify font resource path or URL. You can also use a relative path.
//
// Optional. Default: https://fonts.googleapis.com/css2?family=Roboto:wght@400;900&display=swap
FontURL string

// ChartJSURL to specify ChartJS library path or URL. You can also use a relative path.
//
// Optional. Default: https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.bundle.min.js
ChartJSURL string

index string
}

var ConfigDefault = Config{
Title: defaultTitle,
Refresh: defaultRefresh,
FontURL: defaultFontURL,
ChartJSURL: defaultChartJSURL,
CustomHead: defaultCustomHead,
APIOnly: false,
Next: nil,
index: newIndex(viewBag{
defaultTitle,
defaultRefresh,
defaultFontURL,
defaultChartJSURL,
defaultCustomHead,
}),
}

func configDefault(config ...Config) Config {
// Users can change ConfigDefault.Title/Refresh which then
// become incompatible with ConfigDefault.index
if ConfigDefault.Title != defaultTitle ||
ConfigDefault.Refresh != defaultRefresh ||
ConfigDefault.FontURL != defaultFontURL ||
ConfigDefault.ChartJSURL != defaultChartJSURL ||
ConfigDefault.CustomHead != defaultCustomHead {
if ConfigDefault.Refresh < minRefresh {
ConfigDefault.Refresh = minRefresh
}
// update default index with new default title/refresh
ConfigDefault.index = newIndex(viewBag{
ConfigDefault.Title,
ConfigDefault.Refresh,
ConfigDefault.FontURL,
ConfigDefault.ChartJSURL,
ConfigDefault.CustomHead,
})
}

// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}

// Override default config
cfg := config[0]

// Set default values
if cfg.Title == "" {
cfg.Title = ConfigDefault.Title
}

if cfg.Refresh == 0 {
cfg.Refresh = ConfigDefault.Refresh
}
if cfg.FontURL == "" {
cfg.FontURL = defaultFontURL
}

if cfg.ChartJSURL == "" {
cfg.ChartJSURL = defaultChartJSURL
}
if cfg.Refresh < minRefresh {
cfg.Refresh = minRefresh
}

if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}

if !cfg.APIOnly {
cfg.APIOnly = ConfigDefault.APIOnly
}

// update cfg.index with custom title/refresh
cfg.index = newIndex(viewBag{
title: cfg.Title,
refresh: cfg.Refresh,
fontURL: cfg.FontURL,
chartJSURL: cfg.ChartJSURL,
customHead: cfg.CustomHead,
})

return cfg
}
Loading

0 comments on commit 6741a0e

Please sign in to comment.