2023-07-20 08:10:55 +00:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-08-30 14:32:28 +00:00
|
|
|
|
2023-07-20 08:10:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
|
|
)
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// RequestHandler processes Loki insert requests
|
2023-07-20 08:10:55 +00:00
|
|
|
func RequestHandler(path string, w http.ResponseWriter, r *http.Request) bool {
|
2023-08-30 14:32:28 +00:00
|
|
|
switch path {
|
|
|
|
case "/api/v1/push":
|
|
|
|
return handleInsert(r, w)
|
|
|
|
case "/ready":
|
|
|
|
// See https://grafana.com/docs/loki/latest/api/#identify-ready-loki-instance
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("ready"))
|
|
|
|
return true
|
|
|
|
default:
|
2023-07-20 08:10:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-08-30 14:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See https://grafana.com/docs/loki/latest/api/#push-log-entries-to-loki
|
|
|
|
func handleInsert(r *http.Request, w http.ResponseWriter) bool {
|
2023-07-20 23:21:47 +00:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
switch contentType {
|
|
|
|
case "application/json":
|
|
|
|
return handleJSON(r, w)
|
|
|
|
default:
|
2023-09-16 13:10:29 +00:00
|
|
|
// Protobuf request body should be handled by default according to https://grafana.com/docs/loki/latest/api/#push-log-entries-to-loki
|
2023-07-20 23:21:47 +00:00
|
|
|
return handleProtobuf(r, w)
|
|
|
|
}
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCommonParams(r *http.Request) (*insertutils.CommonParams, error) {
|
|
|
|
cp, err := insertutils.GetCommonParams(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If parsed tenant is (0,0) it is likely to be default tenant
|
|
|
|
// Try parsing tenant from Loki headers
|
|
|
|
if cp.TenantID.AccountID == 0 && cp.TenantID.ProjectID == 0 {
|
|
|
|
org := r.Header.Get("X-Scope-OrgID")
|
|
|
|
if org != "" {
|
|
|
|
tenantID, err := logstorage.GetTenantIDFromString(org)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cp.TenantID = tenantID
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return cp, nil
|
|
|
|
}
|