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

Decouple config models from internal models #1161

Merged
merged 2 commits into from
Oct 9, 2023

Conversation

cristiangreco
Copy link
Contributor

With this change, models used for config parsing are fully decoupled from models used internally at scrape time. Once the config is parsed and validated, it is converted to a representation used internally for the actual scrape logic.

The down side of this approach:

  • structs appear duplicated between the config and models
  • after parsing we go through a complete mapping of fields to the internal models representation (this has unnoticeable impact at startup and zero impact at runtime)

On the pros side of things:

  • allows to continue improving the design of internal models without impacting the configuration
  • allows multiple config format to co-exist

Coincidentally this also fixes #1160

@cristiangreco cristiangreco added this to the v0.56.0 milestone Sep 25, 2023
Copy link
Contributor

@kgeckhart kgeckhart left a comment

Choose a reason for hiding this comment

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

Just a suggestion and two small comments. Nice work going through all of these!

@@ -347,6 +355,160 @@ func (m *Metric) validateMetric(metricIdx int, parent string, discovery *JobLeve
return nil
}

func (c *ScrapeConf) toModelConfig() model.JobsConfig {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since YACE has the two entry points there is a risk that this mapping diverges from what's in use in exporter.go. IE a field is added to model.JobsConfig but not given a config field/mapped from the config. I can't say I know how to easily test for something like this but just something to note.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I see what you mean. It should be responsibility of config loader to make sure that any field in the config file is mapped correctly/fully to the model.JobsConfig. Likewise, when adding or editing fields in model.JobsConfig one should go back to the config parser to populate those fields correcly (otherwise the changes might be of no use).

Comment on lines 480 to 504
for _, role := range customNamespaceJob.Roles {
job.Roles = append(job.Roles, model.Role{
RoleArn: role.RoleArn,
ExternalID: role.ExternalID,
})
}

for _, customTag := range customNamespaceJob.CustomTags {
job.CustomTags = append(job.CustomTags, model.Tag{
Key: customTag.Key,
Value: customTag.Value,
})
}

for _, metric := range customNamespaceJob.Metrics {
job.Metrics = append(job.Metrics, &model.MetricConfig{
Name: metric.Name,
Statistics: metric.Statistics,
Period: metric.Period,
Length: metric.Length,
Delay: metric.Delay,
NilToZero: metric.NilToZero,
AddCloudwatchTimestamp: metric.AddCloudwatchTimestamp,
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm reading this right it looks like these three mappings show up for each job type. Thoughts on a making a toModelRole, toModelTag, and toModelMetricConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, done!

Comment on lines +376 to +384
job.ExportedTagsOnMetrics = []string{}
if len(c.Discovery.ExportedTagsOnMetrics) > 0 {
svc := SupportedServices.GetService(job.Type)
if exportedTags, ok := c.Discovery.ExportedTagsOnMetrics[svc.Namespace]; ok {
job.ExportedTagsOnMetrics = exportedTags
} else if exportedTags, ok := c.Discovery.ExportedTagsOnMetrics[svc.Alias]; ok {
job.ExportedTagsOnMetrics = exportedTags
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the code which fixes #1160 right?

I'm a bit surprised that this field exists at the root of discovery (I think you mentioned this elsewhere as well). I guess you could have multiple discovery jobs for the same service but that seems more like an exception vs the normal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess you could have multiple discovery jobs for the same service but that seems more like an exception vs the normal.

Yes I think that is more of an exception -- that case however should be covered by this transformation loop.

With this change, models used for config parsing are fully decoupled
from models used internally at scrape time. Once the config is parsed
and validated, it is converted to a representation used internally for
the actual scrape logic.

The down side of this approach:
- structs appear duplicated between the `config` and `models`
- after parsing we go through a complete mapping of fields to the
internal models representation (this has unnoticeable impact at startup
and zero impact at runtime)

On the pros side of things:
- allows to continue improving the design of internal models without
impacting the configuration
- allows multiple config format to co-exist

Coincidentally this also fixes #1160
@cristiangreco cristiangreco force-pushed the cristian/config-models branch from cafebbb to 2caa711 Compare October 9, 2023 09:57
@cristiangreco cristiangreco merged commit c292431 into master Oct 9, 2023
4 checks passed
@cristiangreco cristiangreco deleted the cristian/config-models branch October 9, 2023 14:07
cristiangreco added a commit that referenced this pull request Oct 9, 2023
In a previous PR I've changed the Load() signature to include the
conversion of the yaml-based config into internal representation. This
PR changes the Validate() function as well so that library users can
validate and convert an in-memory config in one pass.

Followup of #1161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] inconsistent mix of namespace and alias in config files breaks exportedTagsOnMetrics
2 participants