mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 08:21:11 +00:00 
			
		
		
		
	Move metrics from macaron to chi (#13601)
This commit is contained in:
		
					parent
					
						
							
								75ebf7c5bd
							
						
					
				
			
			
				commit
				
					
						9ec5e6c40b
					
				
			
		
					 3 changed files with 22 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -6,29 +6,29 @@ package routers
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/subtle"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/context"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus/promhttp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Metrics validate auth token and render prometheus metrics
 | 
			
		||||
func Metrics(ctx *context.Context) {
 | 
			
		||||
func Metrics(resp http.ResponseWriter, req *http.Request) {
 | 
			
		||||
	if setting.Metrics.Token == "" {
 | 
			
		||||
		promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
 | 
			
		||||
		promhttp.Handler().ServeHTTP(resp, req)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	header := ctx.Req.Header.Get("Authorization")
 | 
			
		||||
	header := req.Header.Get("Authorization")
 | 
			
		||||
	if header == "" {
 | 
			
		||||
		ctx.Error(401)
 | 
			
		||||
		http.Error(resp, "", 401)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	got := []byte(header)
 | 
			
		||||
	want := []byte("Bearer " + setting.Metrics.Token)
 | 
			
		||||
	if subtle.ConstantTimeCompare(got, want) != 1 {
 | 
			
		||||
		ctx.Error(401)
 | 
			
		||||
		http.Error(resp, "", 401)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request)
 | 
			
		||||
	promhttp.Handler().ServeHTTP(resp, req)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,12 +17,15 @@ import (
 | 
			
		|||
	"time"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/metrics"
 | 
			
		||||
	"code.gitea.io/gitea/modules/public"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/storage"
 | 
			
		||||
	"code.gitea.io/gitea/routers"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-chi/chi"
 | 
			
		||||
	"github.com/go-chi/chi/middleware"
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type routerLoggerOptions struct {
 | 
			
		||||
| 
						 | 
				
			
			@ -251,6 +254,18 @@ func NormalRoutes() http.Handler {
 | 
			
		|||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r.Get("/apple-touch-icon.png", func(w http.ResponseWriter, req *http.Request) {
 | 
			
		||||
		http.Redirect(w, req, path.Join(setting.StaticURLPrefix, "img/apple-touch-icon.png"), 301)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// prometheus metrics endpoint
 | 
			
		||||
	if setting.Metrics.Enabled {
 | 
			
		||||
		c := metrics.NewCollector()
 | 
			
		||||
		prometheus.MustRegister(c)
 | 
			
		||||
 | 
			
		||||
		r.Get("/metrics", routers.Metrics)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,14 +6,12 @@ package routes
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/gob"
 | 
			
		||||
	"path"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/auth"
 | 
			
		||||
	"code.gitea.io/gitea/modules/context"
 | 
			
		||||
	"code.gitea.io/gitea/modules/lfs"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/metrics"
 | 
			
		||||
	"code.gitea.io/gitea/modules/options"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/templates"
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +41,6 @@ import (
 | 
			
		|||
	"gitea.com/macaron/macaron"
 | 
			
		||||
	"gitea.com/macaron/session"
 | 
			
		||||
	"gitea.com/macaron/toolbox"
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus"
 | 
			
		||||
	"github.com/tstranex/u2f"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -978,23 +975,11 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
 | 
			
		|||
		private.RegisterRoutes(m)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	m.Get("/apple-touch-icon.png", func(ctx *context.Context) {
 | 
			
		||||
		ctx.Redirect(path.Join(setting.StaticURLPrefix, "img/apple-touch-icon.png"), 301)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// Progressive Web App
 | 
			
		||||
	m.Get("/manifest.json", templates.JSONRenderer(), func(ctx *context.Context) {
 | 
			
		||||
		ctx.HTML(200, "pwa/manifest_json")
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// prometheus metrics endpoint
 | 
			
		||||
	if setting.Metrics.Enabled {
 | 
			
		||||
		c := metrics.NewCollector()
 | 
			
		||||
		prometheus.MustRegister(c)
 | 
			
		||||
 | 
			
		||||
		m.Get("/metrics", routers.Metrics)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Not found handler.
 | 
			
		||||
	m.NotFound(routers.NotFound)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue