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

[Metrics builder] Add ability to override start time per resource #9992

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
45 changes: 33 additions & 12 deletions cmd/mdatagen/metrics_v2.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,58 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
}
}

// ResourceOption applies changes to provided resource.
type ResourceOption func(pcommon.Resource)
// ResourceMetricsOption applies changes to provided resource metrics.
type ResourceMetricsOption func(pmetric.ResourceMetrics)

{{- range $name, $attr := .ResourceAttributes }}
// With{{ $name.Render }} sets provided value as "{{ $name }}" attribute for current resource.
func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceOption {
return func(r pcommon.Resource) {
r.Attributes().Upsert{{ $attr.Type }}("{{ $name }}", val)
func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceMetricsOption {
return func(rm pmetric.ResourceMetrics) {
rm.Resource().Attributes().Upsert{{ $attr.Type }}("{{ $name }}", val)
}
}
{{ end }}

// WithStartTimeOverride overrides start time for all the resource metrics data points.
// This option should be only used if different start time has to be set on metrics coming from different resources.
func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption {
return func(rm pmetric.ResourceMetrics) {
metrics := rm.ScopeMetrics().At(0).Metrics()
for i := 0; i < metrics.Len(); i++ {
dps := pmetric.NewNumberDataPointSlice()
switch metrics.At(i).DataType() {
case pmetric.MetricDataTypeGauge:
dps = metrics.At(i).Gauge().DataPoints()
case pmetric.MetricDataTypeSum:
dps = metrics.At(i).Sum().DataPoints()
}
for j := 0; j < dps.Len(); j++ {
dps.At(j).SetStartTimestamp(start)
}
}
}
}

// EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for
// recording another set of data points as part of another resource. This function can be helpful when one scraper
// needs to emit metrics from several resources. Otherwise calling this function is not required,
// just `Emit` function can be called instead. Resource attributes should be provided as ResourceOption arguments.
func (mb *MetricsBuilder) EmitForResource(ro ...ResourceOption) {
// just `Emit` function can be called instead.
// Resource attributes should be provided as ResourceMetricsOption arguments.
func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) {
rm := pmetric.NewResourceMetrics()
{{- if .SemConvVersion }}
rm.SetSchemaUrl(conventions.SchemaURL)
{{- end }}
rm.Resource().Attributes().EnsureCapacity(mb.resourceCapacity)
for _, op := range ro {
op(rm.Resource())
}
ils := rm.ScopeMetrics().AppendEmpty()
ils.Scope().SetName("otelcol/{{ .Name }}")
ils.Metrics().EnsureCapacity(mb.metricsCapacity)
{{- range $name, $metric := .Metrics }}
mb.metric{{- $name.Render }}.emit(ils.Metrics())
{{- end }}
for _, op := range rmo {
op(rm)
}
if ils.Metrics().Len() > 0 {
mb.updateCapacity(rm)
rm.MoveTo(mb.metricsBuffer.ResourceMetrics().AppendEmpty())
Expand All @@ -225,8 +246,8 @@ func (mb *MetricsBuilder) EmitForResource(ro ...ResourceOption) {
// Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for
// recording another set of metrics. This function will be responsible for applying all the transformations required to
// produce metric representation defined in metadata and user settings, e.g. delta or cumulative.
func (mb *MetricsBuilder) Emit(ro ...ResourceOption) pmetric.Metrics {
mb.EmitForResource(ro...)
func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics {
mb.EmitForResource(rmo...)
metrics := pmetric.NewMetrics()
mb.metricsBuffer.MoveTo(metrics)
return metrics
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading