-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathgrafana_loki.lua
63 lines (52 loc) · 1.87 KB
/
grafana_loki.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local cjson = require("cjson")
local http = require("resty.http")
local _M = {}
function _M.fetch_logs_from_loki(from, to, options)
options = options or {}
local direction = options.direction or "backward"
local limit = options.limit or "10"
local query = options.query or [[{job="apisix"} | json]]
local url = options.url or "http://127.0.0.1:3100/loki/api/v1/query_range"
local headers = options.headers or {
["X-Scope-OrgID"] = "tenant_1"
}
local httpc = http.new()
local res, err = httpc:request_uri(url, {
query = {
start = from,
["end"] = to,
direction = direction,
limit = limit,
query = query,
},
headers = headers
})
if not res or err then
return nil, err
end
if res.status > 300 then
return nil, "HTTP status code: " .. res.status .. ", body: " .. res.body
end
local data = cjson.decode(res.body)
if not data then
return nil, "failed to decode response body: " .. res.body
end
return data, nil
end
return _M