Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: release v0.20240201 #167

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Release

on:
push:
branches:
- "main"
paths:
- 'rockspec/**'

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Lua
uses: leafo/gh-actions-lua@v8

- name: Install Luarocks
uses: leafo/gh-actions-luarocks@v4

- name: Extract release name
id: release_env
shell: bash
run: |
title="${{ github.event.head_commit.message }}"
re="^feat: release v*(\S+)"
if [[ $title =~ $re ]]; then
v=${BASH_REMATCH[1]}
echo "##[set-output name=version;]${v}"
echo "##[set-output name=version_withou_v;]${BASH_REMATCH[1]}"
else
echo "commit format is not correct"
exit 1
fi

- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.release_env.outputs.version }}
release_name: ${{ steps.release_env.outputs.version }}
draft: false
prerelease: false

- name: Upload to luarocks
env:
LUAROCKS_TOKEN: ${{ secrets.LUAROCKS_TOKEN }}
run: |
luarocks install dkjson
luarocks upload rockspec/nginx-lua-prometheus-api7-${{ steps.release_env.outputs.version_withou_v }}-1.rockspec --api-key=${LUAROCKS_TOKEN}
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
lua: ["5.1", "5.2"]
lua: ["5.2"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
Expand All @@ -35,7 +35,7 @@ jobs:
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Only report test coverage for lua 5.1 to avoid doing it twice.
if: ${{ matrix.lua == '5.1' }}
if: ${{ matrix.lua == '5.2' }}
integration:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
32 changes: 18 additions & 14 deletions prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ local function lookup_or_create(self, label_values)

self.lookup_size = self.lookup_size + 1

local err = self._key_index:add(full_name, ERR_MSG_LRU_EVICTION)
local err = self._key_index:add(full_name, ERR_MSG_LRU_EVICTION, self.exptime)
if err then
return nil, err
end
Expand Down Expand Up @@ -475,6 +475,9 @@ local function inc_gauge(self, value, label_values)
if err or forcible then
self._log_error_kv(k, value, err or ERR_MSG_LRU_EVICTION)
end
if self.exptime then
self._dict:expire(k, self.exptime)
end
end

-- Increment a counter metric.
Expand Down Expand Up @@ -509,7 +512,7 @@ local function inc_counter(self, value, label_values)
end
self._counter = c
end
c:incr(k, value)
c:incr(k, value, self.exptime)
end

-- Delete a counter or a gauge metric.
Expand Down Expand Up @@ -567,7 +570,7 @@ local function set(self, value, label_values)
self._log_error(err)
return
end
_, err, forcible = self._dict:set(k, value)
_, err, forcible = self._dict:set(k, value, self.exptime)
if err or forcible then
self._log_error_kv(k, value, err or ERR_MSG_LRU_EVICTION)
end
Expand Down Expand Up @@ -602,20 +605,20 @@ local function observe(self, value, label_values)
end

-- _count metric.
c:incr(keys[1], 1)
c:incr(keys[1], 1, self.exptime)

-- _sum metric.
c:incr(keys[2], value)
c:incr(keys[2], value, self.exptime)

-- the last bucket (le="Inf").
c:incr(keys[self.bucket_count+3], 1)
c:incr(keys[self.bucket_count+3], 1, self.exptime)

local seen = false
-- check in reverse order, otherwise we will always
-- need to traverse the whole table.
for i=self.bucket_count, 1, -1 do
if value <= self.buckets[i] then
c:incr(keys[2+i], 1)
c:incr(keys[2+i], 1, self.exptime)
seen = true
elseif seen then
break
Expand Down Expand Up @@ -798,7 +801,7 @@ end
--
-- Returns:
-- a new metric object.
local function register(self, name, help, label_names, buckets, typ)
local function register(self, name, help, label_names, buckets, typ, exptime)
if not self.initialized then
ngx.log(ngx.ERR, "Prometheus module has not been initialized")
return
Expand Down Expand Up @@ -851,6 +854,7 @@ local function register(self, name, help, label_names, buckets, typ)
_key_index = self.key_index,
_dict = self.dict,
reset = reset,
exptime = exptime,
}
if typ < TYPE_HISTOGRAM then
if typ == TYPE_GAUGE then
Expand Down Expand Up @@ -893,18 +897,18 @@ do
end

-- Public function to register a counter.
function Prometheus:counter(name, help, label_names)
return register(self, name, help, label_names, nil, TYPE_COUNTER)
function Prometheus:counter(name, help, label_names, exptime)
return register(self, name, help, label_names, nil, TYPE_COUNTER, exptime)
end

-- Public function to register a gauge.
function Prometheus:gauge(name, help, label_names)
return register(self, name, help, label_names, nil, TYPE_GAUGE)
function Prometheus:gauge(name, help, label_names, exptime)
return register(self, name, help, label_names, nil, TYPE_GAUGE, exptime)
end

-- Public function to register a histogram.
function Prometheus:histogram(name, help, label_names, buckets)
return register(self, name, help, label_names, buckets, TYPE_HISTOGRAM)
function Prometheus:histogram(name, help, label_names, buckets, exptime)
return register(self, name, help, label_names, buckets, TYPE_HISTOGRAM, exptime)
end

-- Prometheus compatible metric data as an array of strings.
Expand Down
34 changes: 31 additions & 3 deletions prometheus_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
-- using ngx.shared.dict:get_keys (see https://github.com/openresty/lua-nginx-module#ngxshareddictget_keys),
-- which blocks all workers and therefore it shouldn't be used with large
-- amounts of keys.

local KeyIndex = {}
KeyIndex.__index = KeyIndex

Expand All @@ -16,6 +15,7 @@ function KeyIndex.new(shared_dict, prefix)
self.key_count = prefix .. "key_count"
self.last = 0
self.deleted = 0
self.not_expired_index = 1
self.keys = {}
self.index = {}
return self
Expand All @@ -33,6 +33,7 @@ function KeyIndex:sync()
-- Sync only new keys, if there are any.
self:sync_range(self.last, N)
end
self:sync_expired(N)
return N
end

Expand All @@ -52,6 +53,33 @@ function KeyIndex:sync_range(first, last)
self.last = last
end

function KeyIndex:sync_expired(N)
local first = self.not_expired_index
--- the key is sorted by created time, so the key will expire in order
for i = first, N do
self.not_expired_index = i
-- Read i-th key. If it is nil, it means it was expired
local ttl, err = self.dict:ttl(self.key_prefix .. i)
if ttl then
if ttl == 0 then
goto CONTINUE
else
break
end
else
if err ~= "not found" then
break
end
if self.keys[i] then
-- we don't need to update self.delete_count and self.key_count
self.index[self.keys[i]] = nil
self.keys[i] = nil
end
end
::CONTINUE::
end
end

-- Returns array of all keys.
function KeyIndex:list()
self:sync()
Expand All @@ -71,7 +99,7 @@ end
--
-- Returns:
-- nil on success, string with error message otherwise
function KeyIndex:add(key_or_keys, err_msg_lru_eviction)
function KeyIndex:add(key_or_keys, err_msg_lru_eviction, exptime)
local keys = key_or_keys
if type(key_or_keys) == "string" then
keys = { key_or_keys }
Expand All @@ -85,7 +113,7 @@ function KeyIndex:add(key_or_keys, err_msg_lru_eviction)
break
end
N = N+1
local ok, err, forcible = self.dict:add(self.key_prefix .. N, key)
local ok, err, forcible = self.dict:add(self.key_prefix .. N, key, exptime)
if ok then
local _, _, forcible2 = self.dict:incr(self.key_count, 1, 0)
self.keys[N] = key
Expand Down
24 changes: 16 additions & 8 deletions prometheus_resty_counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ local id
local function sync(_, self)
local err, _, forcible
local ok = true
for k, v in pairs(self.increments) do
for k, value in pairs(self.increments) do
local v = value.v
_, err, forcible = self.dict:incr(k, v, 0)
if forcible then
ngx.log(ngx.ERR, "increasing counter in shdict: lru eviction: key=", k)
Expand All @@ -43,6 +44,9 @@ local function sync(_, self)
ngx.log(ngx.ERR, "error increasing counter in shdict key: ", k, ", err: ", err)
ok = false
end
if value.t then
self.dict:expire(k, value.t)
end
end

clear_tab(self.increments)
Expand Down Expand Up @@ -89,22 +93,26 @@ function _M:sync()
return sync(false, self)
end

function _M:incr(key, step)
function _M:incr(key, step, exptime)
step = step or 1
local v = self.increments[key]
if v then
step = step + v
local value = self.increments[key]
if value then
step = step + value.v
end

self.increments[key] = step
self.increments[key] = {v = step, t = exptime}
return true
end

function _M:reset(key, number)
function _M:reset(key, number, exptime)
if not number then
return nil, "expect a number at #2"
end
return self.dict:incr(key, -number, number)
local newval, err, forcible = self.dict:incr(key, -number, number)
if exptime then
self.dict:expire(key, exptime)
end
return newval, err, forcible
end

function _M:get(key)
Expand Down
Loading
Loading