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

receiver/dockerstatsreceiver: add container.uptime metric #22851

Merged
merged 5 commits into from
Jun 12, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add scraper error for invalid time format
gbbr committed Jun 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 7555d194ca132f00bf3fa075f9bd91ab21e15966
17 changes: 12 additions & 5 deletions receiver/dockerstatsreceiver/receiver.go
Original file line number Diff line number Diff line change
@@ -105,19 +105,24 @@ func (r *receiver) scrapeV2(ctx context.Context) (pmetric.Metrics, error) {
errs = multierr.Append(errs, scrapererror.NewPartialScrapeError(res.err, 0))
continue
}
r.recordContainerStats(now, res.stats, res.container)
if err := r.recordContainerStats(now, res.stats, res.container); err != nil {
errs = multierr.Append(errs, err)
}
}

return r.mb.Emit(), errs
}

func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *dtypes.StatsJSON, container *docker.Container) {
func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *dtypes.StatsJSON, container *docker.Container) error {
var errs error
r.recordCPUMetrics(now, &containerStats.CPUStats, &containerStats.PreCPUStats)
r.recordMemoryMetrics(now, &containerStats.MemoryStats)
r.recordBlkioMetrics(now, &containerStats.BlkioStats)
r.recordNetworkMetrics(now, &containerStats.Networks)
r.recordPidsMetrics(now, &containerStats.PidsStats)
r.recordBaseMetrics(now, container.ContainerJSONBase)
if err := r.recordBaseMetrics(now, container.ContainerJSONBase); err != nil {
errs = multierr.Append(errs, err)
}

// Always-present resource attrs + the user-configured resource attrs
resourceCapacity := defaultResourcesLen + len(r.config.EnvVarsToMetricLabels) + len(r.config.ContainerLabelsToMetricLabels)
@@ -147,6 +152,7 @@ func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *d
}

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

func (r *receiver) recordMemoryMetrics(now pcommon.Timestamp, memoryStats *dtypes.MemoryStats) {
@@ -268,13 +274,14 @@ func (r *receiver) recordPidsMetrics(now pcommon.Timestamp, pidsStats *dtypes.Pi
}
}

func (r *receiver) recordBaseMetrics(now pcommon.Timestamp, base *types.ContainerJSONBase) {
func (r *receiver) recordBaseMetrics(now pcommon.Timestamp, base *types.ContainerJSONBase) error {
t, err := time.Parse(time.RFC3339, base.State.StartedAt)
if err != nil {
// value not available or invalid
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
return
return scrapererror.NewPartialScrapeError(fmt.Errorf("error retrieving container.uptime from Container.State.StartedAt: %w", err), 1)
}
if v := now.AsTime().Sub(t); v > 0 {
r.mb.RecordContainerUptimeDataPoint(now, v.Seconds())
}
return nil
}
40 changes: 28 additions & 12 deletions receiver/dockerstatsreceiver/receiver_test.go
Original file line number Diff line number Diff line change
@@ -275,19 +275,35 @@ func TestRecordBaseMetrics(t *testing.T) {
r := newReceiver(receivertest.NewNopCreateSettings(), cfg)
now := time.Now()
started := now.Add(-2 * time.Second).Format(time.RFC3339)
r.recordBaseMetrics(
pcommon.NewTimestampFromTime(now),
&types.ContainerJSONBase{
State: &types.ContainerState{
StartedAt: started,

t.Run("ok", func(t *testing.T) {
err := r.recordBaseMetrics(
pcommon.NewTimestampFromTime(now),
&types.ContainerJSONBase{
State: &types.ContainerState{
StartedAt: started,
},
},
},
)
m := r.mb.Emit().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
assert.Equal(t, "container.uptime", m.Name())
dp := m.Gauge().DataPoints()
assert.Equal(t, 1, dp.Len())
assert.Equal(t, 2, int(dp.At(0).DoubleValue()))
)
require.NoError(t, err)
m := r.mb.Emit().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
assert.Equal(t, "container.uptime", m.Name())
dp := m.Gauge().DataPoints()
assert.Equal(t, 1, dp.Len())
assert.Equal(t, 2, int(dp.At(0).DoubleValue()))
})

t.Run("error", func(t *testing.T) {
err := r.recordBaseMetrics(
pcommon.NewTimestampFromTime(now),
&types.ContainerJSONBase{
State: &types.ContainerState{
StartedAt: "bad date",
},
},
)
require.Error(t, err)
})
}

func dockerMockServer(urlToFile *map[string]string) (*httptest.Server, error) {