Skip to content
代码片段 群组 项目
未验证 提交 ea98b0eb 编辑于 作者: Sashi Kumar Kumaresan's avatar Sashi Kumar Kumaresan 提交者: GitLab
浏览文件

Merge branch 'ah-support-accepting-epoch-time-for-alert-manager-ended-at' into 'master'

Support accepting epoch millis for alert manager ended at

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/145502



Merged-by: default avatarSashi Kumar Kumaresan <skumar@gitlab.com>
Approved-by: default avatarSashi Kumar Kumaresan <skumar@gitlab.com>
Reviewed-by: default avatarSean Arnold <sarnold@gitlab.com>
Reviewed-by: default avatarSashi Kumar Kumaresan <skumar@gitlab.com>
Co-authored-by: default avatarAdam Hegyi <ahegyi@gitlab.com>
No related branches found
No related tags found
无相关合并请求
显示
75 个添加5 个删除
...@@ -31693,6 +31693,7 @@ Values for alert field types used in the custom mapping. ...@@ -31693,6 +31693,7 @@ Values for alert field types used in the custom mapping.
| ----- | ----------- | | ----- | ----------- |
| <a id="alertmanagementpayloadalertfieldtypearray"></a>`ARRAY` | Array field type. | | <a id="alertmanagementpayloadalertfieldtypearray"></a>`ARRAY` | Array field type. |
| <a id="alertmanagementpayloadalertfieldtypedatetime"></a>`DATETIME` | DateTime field type. | | <a id="alertmanagementpayloadalertfieldtypedatetime"></a>`DATETIME` | DateTime field type. |
| <a id="alertmanagementpayloadalertfieldtypenumber"></a>`NUMBER` | Number field type. |
| <a id="alertmanagementpayloadalertfieldtypestring"></a>`STRING` | String field type. | | <a id="alertmanagementpayloadalertfieldtypestring"></a>`STRING` | String field type. |
   
### `AlertManagementSeverity` ### `AlertManagementSeverity`
...@@ -9,6 +9,7 @@ class PayloadAlertFieldTypeEnum < BaseEnum ...@@ -9,6 +9,7 @@ class PayloadAlertFieldTypeEnum < BaseEnum
value 'ARRAY', 'Array field type.', value: 'array' value 'ARRAY', 'Array field type.', value: 'array'
value 'DATETIME', 'DateTime field type.', value: 'datetime' value 'DATETIME', 'DateTime field type.', value: 'datetime'
value 'STRING', 'String field type.', value: 'string' value 'STRING', 'String field type.', value: 'string'
value 'NUMBER', 'Number field type.', value: 'number'
end end
end end
end end
...@@ -10,7 +10,8 @@ class AlertPayloadField ...@@ -10,7 +10,8 @@ class AlertPayloadField
ARRAY_TYPE = 'array' ARRAY_TYPE = 'array'
DATETIME_TYPE = 'datetime' DATETIME_TYPE = 'datetime'
STRING_TYPE = 'string' STRING_TYPE = 'string'
SUPPORTED_TYPES = [ARRAY_TYPE, DATETIME_TYPE, STRING_TYPE].freeze NUMBER_TYPE = 'number'
SUPPORTED_TYPES = [ARRAY_TYPE, DATETIME_TYPE, STRING_TYPE, NUMBER_TYPE].freeze
validates :project, presence: true validates :project, presence: true
validates :label, presence: true validates :label, presence: true
......
...@@ -56,7 +56,9 @@ def custom_mapping_value(attribute_name) ...@@ -56,7 +56,9 @@ def custom_mapping_value(attribute_name)
end end
def custom_mapping_type(attribute_name) def custom_mapping_type(attribute_name)
:time if custom_mapping.dig(attribute_name, 'type') == 'datetime' return :time if custom_mapping.dig(attribute_name, 'type') == 'datetime'
:time_with_epoch_millis if attribute_name == 'end_time'
end end
end end
end end
......
...@@ -34,7 +34,7 @@ def self.alert_fields ...@@ -34,7 +34,7 @@ def self.alert_fields
name: 'end_time', name: 'end_time',
label: 'End time', label: 'End time',
description: 'The resolved time of the incident.', description: 'The resolved time of the incident.',
types: %w[datetime] types: %w[datetime number]
}, },
{ {
name: 'service', name: 'service',
......
...@@ -90,6 +90,8 @@ def type_of(value) ...@@ -90,6 +90,8 @@ def type_of(value)
::AlertManagement::AlertPayloadField::DATETIME_TYPE ::AlertManagement::AlertPayloadField::DATETIME_TYPE
when String when String
::AlertManagement::AlertPayloadField::STRING_TYPE ::AlertManagement::AlertPayloadField::STRING_TYPE
when Integer
::AlertManagement::AlertPayloadField::NUMBER_TYPE
end end
end end
......
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
RSpec.describe GitlabSchema.types['AlertManagementPayloadAlertFieldType'] do RSpec.describe GitlabSchema.types['AlertManagementPayloadAlertFieldType'] do
it 'exposes all alert field types' do it 'exposes all alert field types' do
expect(described_class.values.keys).to match_array(%w[ARRAY DATETIME STRING]) expect(described_class.values.keys).to match_array(%w[ARRAY DATETIME STRING NUMBER])
end end
end end
...@@ -45,6 +45,20 @@ ...@@ -45,6 +45,20 @@
end end
end end
context 'when payload is integer' do
let(:payload) { { key: 100 } }
it 'returns parsed fields' do
fields = response.payload[:payload_alert_fields]
field = fields.first
expect(fields.count).to eq(1)
expect(field.label).to eq('key')
expect(field.type).to eq('number')
expect(field.path).to eq(%w[key])
end
end
context 'when limits are exceeded' do context 'when limits are exceeded' do
before do before do
allow(Gitlab::Utils::DeepSize) allow(Gitlab::Utils::DeepSize)
......
...@@ -43,6 +43,8 @@ class Base ...@@ -43,6 +43,8 @@ class Base
:title :title
].freeze ].freeze
EPOCH_MILLISECONDS_DIGIT_COUNT = (Time.current.to_f * 1000).to_i.to_s.size
private_constant :EXPECTED_PAYLOAD_ATTRIBUTES private_constant :EXPECTED_PAYLOAD_ATTRIBUTES
# Define expected API for a payload # Define expected API for a payload
...@@ -188,6 +190,12 @@ def value_for_paths(paths) ...@@ -188,6 +190,12 @@ def value_for_paths(paths)
def parse_value(value, type) def parse_value(value, type)
case type case type
when :time_with_epoch_millis
if value.is_a?(Integer) && value.to_s.size == EPOCH_MILLISECONDS_DIGIT_COUNT
Time.at(0, value, :millisecond).utc
else
parse_time(value)
end
when :time when :time
parse_time(value) parse_time(value)
when :integer when :integer
......
...@@ -9,7 +9,7 @@ class Generic < Base ...@@ -9,7 +9,7 @@ class Generic < Base
DEFAULT_SOURCE = 'Generic Alert Endpoint' DEFAULT_SOURCE = 'Generic Alert Endpoint'
attribute :description, paths: 'description' attribute :description, paths: 'description'
attribute :ends_at, paths: 'end_time', type: :time attribute :ends_at, paths: 'end_time', type: :time_with_epoch_millis
attribute :environment_name, paths: 'gitlab_environment_name' attribute :environment_name, paths: 'gitlab_environment_name'
attribute :hosts, paths: 'hosts' attribute :hosts, paths: 'hosts'
attribute :monitoring_tool, paths: 'monitoring_tool' attribute :monitoring_tool, paths: 'monitoring_tool'
......
...@@ -124,6 +124,37 @@ ...@@ -124,6 +124,37 @@
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
end end
context 'with a time_with_epoch_millis type provided' do
let(:timestamp) { 3.hours.ago }
let(:epoch_millis) { (timestamp.to_f * 1000).to_i }
let(:payload_class) do
Class.new(described_class) do
attribute :test, paths: [['test']], type: :time_with_epoch_millis
end
end
it { is_expected.to be_nil }
context 'with a compatible matching value' do
let(:raw_payload) { { 'test' => epoch_millis } }
it { is_expected.to be_within(1.second).of(timestamp) }
end
context 'with an incompatible matching value' do
let(:raw_payload) { { 'test' => String } }
it { is_expected.to be_nil }
end
context 'with an incompatible matching value' do
let(:raw_payload) { { 'test' => 'apple' } }
it { is_expected.to be_nil }
end
end
end end
describe '#alert_params' do describe '#alert_params' do
......
...@@ -142,6 +142,16 @@ ...@@ -142,6 +142,16 @@
end end
it { is_expected.to eq(value) } it { is_expected.to eq(value) }
context 'when integer is given' do
let(:current_time) { Time.current }
before do
raw_payload['end_time'] = (current_time.to_f * 1000).to_i
end
it { is_expected.to be_within(1.second).of(current_time) }
end
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册