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

Kaarolch kch/slo time slice #314

Merged
merged 5 commits into from
May 27, 2024
Merged
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
10 changes: 9 additions & 1 deletion lib/kennel/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

module Kennel
class Importer
# title will have the lock symbol we need to remove when re-importing
TITLES = [:name, :title].freeze
SORT_ORDER = [*TITLES, :id, :kennel_id, :type, :tags, :query, *Models::Record.subclasses.map { |k| k::TRACKING_FIELDS }, :template_variables].freeze
Copy link
Owner Author

@grosser grosser May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this had an unrelated bug too where we needed flat_map because TRACKING_FIELDS can have multiple entries


# bring important fields to the top
SORT_ORDER = [
*TITLES, :id, :kennel_id, :type, :tags, :query, :sli_specification,
*Models::Record.subclasses.flat_map { |k| k::TRACKING_FIELDS },
:template_variables
].freeze

def initialize(api)
@api = api
Expand All @@ -19,6 +26,7 @@ def import(resource, id)
raise(ArgumentError, "#{resource} is not supported")

data = @api.show(model.api_resource, id)

id = data.fetch(:id) # keep native value
model.normalize({}, data) # removes id
data[:id] = id
Expand Down
11 changes: 8 additions & 3 deletions lib/kennel/models/slo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module Models
class Slo < Record
include TagsValidation

READONLY_ATTRIBUTES = superclass::READONLY_ATTRIBUTES + [:type_id, :monitor_tags, :target_threshold, :timeframe, :warning_threshold]
READONLY_ATTRIBUTES = [
*superclass::READONLY_ATTRIBUTES,
:type_id, :monitor_tags, :target_threshold, :timeframe, :warning_threshold
].freeze
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this missing freeze was hiding the issue with the importer removing sli_specification during normalize (we added sli_specification to READONLY_ATTRIBUTES in a local hack)

TRACKING_FIELD = :description
DEFAULTS = {
description: nil,
Expand All @@ -14,7 +17,7 @@ class Slo < Record
thresholds: []
}.freeze

settings :type, :description, :thresholds, :query, :tags, :monitor_ids, :monitor_tags, :name, :groups
settings :type, :description, :thresholds, :query, :tags, :monitor_ids, :monitor_tags, :name, :groups, :sli_specification

defaults(
tags: -> { @project.tags },
Expand All @@ -35,7 +38,9 @@ def build_json
type: type
)

if v = query
if type == "time_slice"
data[:sli_specification] = sli_specification
elsif v = query
data[:query] = v
end

Expand Down
28 changes: 28 additions & 0 deletions test/kennel/models/slo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ def slo(options = {})
expected_basic_json
)
end

it "includes sliSpecification for time slices" do
sli_spec = {
time_slice: {
query: {
formulas: [{ formula: "query1" }],
queries: [
{
data_source: "metrics",
name: "query1",
query: "ewma_7(avg:system_cpu{} by {env})"
}
]
},
query_interval_seconds: 300,
comparator: "<=",
threshold: 0.1,
no_data_strategy: "COUNT_AS_UPTIME"
}
}

expected_basic_json[:sli_specification] = sli_spec
expected_basic_json[:type] = "time_slice"
assert_json_equal(
slo(type: "time_slice", sli_specification: sli_spec).build_json,
expected_basic_json
)
end
end

describe "#resolve_linked_tracking_ids!" do
Expand Down
Loading