mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
30098ac8bd
- Parse protobuf if Content-Type isn't set to `application/json` - this behavior is documented at https://grafana.com/docs/loki/latest/api/#push-log-entries-to-loki - Properly handle gzip'ped JSON requests. The `gzip` header must be read from `Content-Encoding` instead of `Content-Type` header - Properly flush all the parsed logs with the explicit call to vlstorage.MustAddRows() at the end of query handler - Check JSON field types more strictly. - Allow parsing Loki timestamp as floating-point number. Such a timestamp can be generated by some clients, which store timestamps in float64 instead of int64. - Optimize parsing of Loki labels in Prometheus text exposition format. - Simplify tests. - Remove lib/slicesutil, since there are no more users for it. - Update docs with missing info and fix various typos. For example, it should be enough to have `instance` and `job` labels as stream fields in most Loki setups. - Allow empty of missing timestamps in the ingested logs. The current timestamp at VictoriaLogs side is then used for the ingested logs. This simplifies debugging and testing of the provided HTTP-based data ingestion APIs. The remaining MAJOR issue, which needs to be addressed: victoria-logs binary size increased from 13MB to 22MB after adding support for Loki data ingestion protocol at https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4482 . This is because of shitty protobuf dependencies. They must be replaced with another protobuf implementation similar to the one used at lib/prompb or lib/prompbmarshal .
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package loki
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
"github.com/VictoriaMetrics/metrics"
|
|
)
|
|
|
|
var (
|
|
lokiRequestsJSONTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/loki/api/v1/push",format="json"}`)
|
|
lokiRequestsProtobufTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/loki/api/v1/push",format="protobuf"}`)
|
|
)
|
|
|
|
// RequestHandler processes Loki insert requests
|
|
//
|
|
// See https://grafana.com/docs/loki/latest/api/#push-log-entries-to-loki
|
|
func RequestHandler(path string, w http.ResponseWriter, r *http.Request) bool {
|
|
if path != "/api/v1/push" {
|
|
return false
|
|
}
|
|
contentType := r.Header.Get("Content-Type")
|
|
switch contentType {
|
|
case "application/json":
|
|
lokiRequestsJSONTotal.Inc()
|
|
return handleJSON(r, w)
|
|
default:
|
|
// Protobuf request body should be handled by default accoring to https://grafana.com/docs/loki/latest/api/#push-log-entries-to-loki
|
|
lokiRequestsProtobufTotal.Inc()
|
|
return handleProtobuf(r, w)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|