Skip to content

Commit

Permalink
[chore] Remove usage of MoveAndAppend in dockerstatsreceiver (#14621)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Sep 30, 2022
1 parent 83b059f commit def6d0b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
7 changes: 3 additions & 4 deletions receiver/dockerstatsreceiver/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ func ContainerStatsToMetrics(
containerStats *dtypes.StatsJSON,
container docker.Container,
config *Config,
) pmetric.Metrics {
md := pmetric.NewMetrics()
rs := md.ResourceMetrics().AppendEmpty()
) pmetric.ResourceMetrics {
rs := pmetric.NewResourceMetrics()
rs.SetSchemaUrl(conventions.SchemaURL)
resourceAttr := rs.Resource().Attributes()
resourceAttr.PutString(conventions.AttributeContainerRuntime, "docker")
Expand All @@ -55,7 +54,7 @@ func ContainerStatsToMetrics(
appendMemoryMetrics(ils.Metrics(), &containerStats.MemoryStats, now)
appendNetworkMetrics(ils.Metrics(), &containerStats.Networks, now)

return md
return rs
}

func updateConfiguredResourceAttributes(resourceAttr pcommon.Map, container docker.Container, config *Config) {
Expand Down
11 changes: 5 additions & 6 deletions receiver/dockerstatsreceiver/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ func metricsData(
ts pcommon.Timestamp,
resourceLabels map[string]string,
metrics ...Metric,
) pmetric.Metrics {
) pmetric.ResourceMetrics {
rLabels := mergeMaps(defaultLabels(), resourceLabels)
md := pmetric.NewMetrics()
rs := md.ResourceMetrics().AppendEmpty()
rs := pmetric.NewResourceMetrics()
rs.SetSchemaUrl(conventions.SchemaURL)
rsAttr := rs.Resource().Attributes()
for k, v := range rLabels {
Expand Down Expand Up @@ -96,7 +95,7 @@ func metricsData(
}
}

return md
return rs
}

func defaultLabels() map[string]string {
Expand Down Expand Up @@ -193,9 +192,9 @@ func assertMetricsDataEqual(
now pcommon.Timestamp,
expected []Metric,
labels map[string]string,
actual pmetric.Metrics,
actual pmetric.ResourceMetrics,
) {
actual.ResourceMetrics().At(0).Resource().Attributes().Sort()
actual.Resource().Attributes().Sort()
assert.Equal(t, metricsData(now, labels, expected...), actual)
}

Expand Down
6 changes: 3 additions & 3 deletions receiver/dockerstatsreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (r *receiver) start(ctx context.Context, _ component.Host) error {
}

type result struct {
md pmetric.Metrics
md pmetric.ResourceMetrics
err error
}

Expand All @@ -87,7 +87,7 @@ func (r *receiver) scrape(ctx context.Context) (pmetric.Metrics, error) {
defer wg.Done()
statsJSON, err := r.client.FetchContainerStatsAsJSON(ctx, c)
if err != nil {
results <- result{md: pmetric.Metrics{}, err: err}
results <- result{md: pmetric.ResourceMetrics{}, err: err}
return
}

Expand All @@ -108,7 +108,7 @@ func (r *receiver) scrape(ctx context.Context) (pmetric.Metrics, error) {
errs = multierr.Append(errs, scrapererror.NewPartialScrapeError(res.err, 0))
continue
}
res.md.ResourceMetrics().MoveAndAppendTo(md.ResourceMetrics())
res.md.MoveTo(md.ResourceMetrics().AppendEmpty())
}

return md, errs
Expand Down
9 changes: 4 additions & 5 deletions receiver/dockerstatsreceiver/receiver_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,19 @@ func (r *receiver) scrapeV2(ctx context.Context) (pmetric.Metrics, error) {
var errs error

now := pcommon.NewTimestampFromTime(time.Now())
md := pmetric.NewMetrics()
for res := range results {
if res.err != nil {
// Don't know the number of failed stats, but one container fetch is a partial error.
errs = multierr.Append(errs, scrapererror.NewPartialScrapeError(res.err, 0))
continue
}
r.recordContainerStats(now, res.stats, res.container).ResourceMetrics().MoveAndAppendTo(md.ResourceMetrics())
r.recordContainerStats(now, res.stats, res.container)
}

return md, errs
return r.mb.Emit(), errs
}

func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *dtypes.StatsJSON, container *docker.Container) pmetric.Metrics {
func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *dtypes.StatsJSON, container *docker.Container) {
r.recordCPUMetrics(now, &containerStats.CPUStats, &containerStats.PreCPUStats)
r.recordMemoryMetrics(now, &containerStats.MemoryStats)
r.recordBlkioMetrics(now, &containerStats.BlkioStats)
Expand Down Expand Up @@ -112,7 +111,7 @@ func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *d
}
}

return r.mb.Emit(resourceMetricsOptions...)
r.mb.EmitForResource(resourceMetricsOptions...)
}

func (r *receiver) recordMemoryMetrics(now pcommon.Timestamp, memoryStats *dtypes.MemoryStats) {
Expand Down

0 comments on commit def6d0b

Please sign in to comment.