Skip to content

Commit cde5c93

Browse files
committed
Add oxide.host to synthetic and self-metrics.
1 parent 79e5d26 commit cde5c93

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

collector/manifest.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dist:
66
module: github.com/oxidecomputer/otelcol-oxide
77
name: otelcol-oxide
88
description: OpenTelemetry Collector with Oxide receiver
9-
version: 0.3.3
9+
version: 0.3.4
1010
output_path: ./dist
1111
build_tags: "grpcnotrace"
1212

@@ -40,9 +40,9 @@ receivers:
4040
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.145.0
4141

4242
# Add the Oxide receivers.
43-
- gomod: github.com/oxidecomputer/opentelemetry-collector-components v0.3.3
43+
- gomod: github.com/oxidecomputer/opentelemetry-collector-components v0.3.4
4444
import: github.com/oxidecomputer/opentelemetry-collector-components/receiver/oxidemetricsreceiver
45-
- gomod: github.com/oxidecomputer/opentelemetry-collector-components v0.3.3
45+
- gomod: github.com/oxidecomputer/opentelemetry-collector-components v0.3.4
4646
import: github.com/oxidecomputer/opentelemetry-collector-components/receiver/oxideauditlogsreceiver
4747

4848
connectors:

receiver/oxideauditlogsreceiver/scraper.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type auditLogScraper struct {
5252
cfg *Config
5353
settings component.TelemetrySettings
5454
logger *zap.Logger
55+
host string
5556
metrics scraperMetrics
5657
cursor cursor
5758
}
@@ -65,6 +66,7 @@ func newAuditLogScraper(
6566
client: client,
6667
cfg: cfg,
6768
settings: settings,
69+
host: normalizeHost(client.Host()),
6870
logger: settings.Logger,
6971
}
7072
}
@@ -127,11 +129,7 @@ func (s *auditLogScraper) Scrape(ctx context.Context) (plog.Logs, error) {
127129
resource := logs.ResourceLogs().AppendEmpty()
128130
attrs := resource.Resource().Attributes()
129131
attrs.PutStr("service.name", "oxide")
130-
host := s.client.Host()
131-
if u, err := url.Parse(host); err == nil && u.Host != "" {
132-
host = u.Host
133-
}
134-
attrs.PutStr("oxide.host", host)
132+
attrs.PutStr("oxide.host", s.host)
135133
scope := resource.ScopeLogs().AppendEmpty()
136134
startTime := time.Now()
137135

@@ -147,13 +145,20 @@ func (s *auditLogScraper) Scrape(ctx context.Context) (plog.Logs, error) {
147145
pageStart := time.Now()
148146
page, err := s.client.AuditLogList(ctx, params)
149147
pageLatency := time.Since(pageStart).Seconds()
150-
s.metrics.apiRequestDuration.Record(ctx, pageLatency)
148+
s.metrics.apiRequestDuration.Record(
149+
ctx,
150+
pageLatency,
151+
metric.WithAttributes(attribute.String("oxide.host", s.host)),
152+
)
151153
if err != nil {
152154
s.logger.Warn("audit log list request failed", zap.Error(err))
153155
s.metrics.scrapeCount.Add(
154156
ctx,
155157
1,
156-
metric.WithAttributes(attribute.String("status", "failure")),
158+
metric.WithAttributes(
159+
attribute.String("status", "failure"),
160+
attribute.String("oxide.host", s.host),
161+
),
157162
)
158163
// Emit partial results on error.
159164
if logs.LogRecordCount() > 0 {
@@ -195,8 +200,19 @@ func (s *auditLogScraper) Scrape(ctx context.Context) (plog.Logs, error) {
195200
}
196201

197202
elapsed := time.Since(startTime)
198-
s.metrics.scrapeDuration.Record(ctx, elapsed.Seconds())
199-
s.metrics.scrapeCount.Add(ctx, 1, metric.WithAttributes(attribute.String("status", "success")))
203+
s.metrics.scrapeDuration.Record(
204+
ctx,
205+
elapsed.Seconds(),
206+
metric.WithAttributes(attribute.String("oxide.host", s.host)),
207+
)
208+
s.metrics.scrapeCount.Add(
209+
ctx,
210+
1,
211+
metric.WithAttributes(
212+
attribute.String("status", "success"),
213+
attribute.String("oxide.host", s.host),
214+
),
215+
)
200216

201217
s.saveCursor()
202218

@@ -271,6 +287,13 @@ func (s *auditLogScraper) saveCursor() {
271287
}
272288
}
273289

290+
func normalizeHost(raw string) string {
291+
if u, err := url.Parse(raw); err == nil && u.Host != "" {
292+
return u.Host
293+
}
294+
return raw
295+
}
296+
274297
func addLogRecord(scope plog.ScopeLogs, entry oxide.AuditLogEntry, observedTime time.Time) error {
275298
raw, err := json.Marshal(entry)
276299
if err != nil {

receiver/oxidemetricsreceiver/scraper.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type oxideScraper struct {
2424
settings component.TelemetrySettings
2525
cfg *Config
2626
logger *zap.Logger
27+
host string
2728

2829
metricNames []string
2930

@@ -32,6 +33,13 @@ type oxideScraper struct {
3233
scrapeDuration metric.Float64Gauge
3334
}
3435

36+
func normalizeHost(raw string) string {
37+
if u, err := url.Parse(raw); err == nil && u.Host != "" {
38+
return u.Host
39+
}
40+
return raw
41+
}
42+
3543
func newOxideScraper(
3644
cfg *Config,
3745
settings component.TelemetrySettings,
@@ -42,6 +50,7 @@ func newOxideScraper(
4250
settings: settings,
4351
cfg: cfg,
4452
logger: settings.Logger,
53+
host: normalizeHost(client.Host()),
4554
}
4655
}
4756

@@ -163,7 +172,10 @@ func (s *oxideScraper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
163172
s.apiRequestDuration.Record(
164173
ctx,
165174
elapsed.Seconds(),
166-
metric.WithAttributes(attribute.String("request_name", metricName)),
175+
metric.WithAttributes(
176+
attribute.String("request_name", metricName),
177+
attribute.String("oxide.host", s.host),
178+
),
167179
)
168180
}
169181

@@ -178,8 +190,9 @@ func (s *oxideScraper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
178190
elapsed := time.Since(startTime)
179191
s.logger.Info("scrape finished", zap.Float64("latency", elapsed.Seconds()))
180192

181-
s.scrapeDuration.Record(ctx, elapsed.Seconds())
182-
s.scrapeCount.Add(ctx, 1)
193+
hostAttr := attribute.String("oxide.host", s.host)
194+
s.scrapeDuration.Record(ctx, elapsed.Seconds(), metric.WithAttributes(hostAttr))
195+
s.scrapeCount.Add(ctx, 1, metric.WithAttributes(hostAttr))
183196

184197
var queryErrors int
185198
for _, result := range results {
@@ -226,11 +239,7 @@ func (s *oxideScraper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
226239
rm := metrics.ResourceMetrics().AppendEmpty()
227240
resource := rm.Resource()
228241
resource.Attributes().PutStr("service.name", "oxide")
229-
host := s.client.Host()
230-
if u, err := url.Parse(host); err == nil && u.Host != "" {
231-
host = u.Host
232-
}
233-
resource.Attributes().PutStr("oxide.host", host)
242+
resource.Attributes().PutStr("oxide.host", s.host)
234243

235244
addLabels(series, resource)
236245

@@ -344,7 +353,7 @@ func (s *oxideScraper) addSiloUtilization(ctx context.Context, metrics pmetric.M
344353
if err != nil {
345354
return err
346355
}
347-
addSiloUtilizationMetrics(metrics, resp, pcommon.NewTimestampFromTime(time.Now()))
356+
addSiloUtilizationMetrics(metrics, resp, pcommon.NewTimestampFromTime(time.Now()), s.host)
348357
return nil
349358
}
350359

@@ -353,8 +362,11 @@ func addSiloUtilizationMetrics(
353362
metrics pmetric.Metrics,
354363
utilizations []oxide.SiloUtilization,
355364
timestamp pcommon.Timestamp,
365+
host string,
356366
) {
357367
rm := metrics.ResourceMetrics().AppendEmpty()
368+
rm.Resource().Attributes().PutStr("service.name", "oxide")
369+
rm.Resource().Attributes().PutStr("oxide.host", host)
358370
sm := rm.ScopeMetrics().AppendEmpty()
359371

360372
addGauge := func(name string) pmetric.Gauge {

receiver/oxidemetricsreceiver/scraper_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,13 @@ func TestAddSiloUtilizationMetrics(t *testing.T) {
343343
}
344344

345345
metrics := pmetric.NewMetrics()
346-
addSiloUtilizationMetrics(metrics, utilizations, timestamp)
346+
addSiloUtilizationMetrics(metrics, utilizations, timestamp, "example.oxide.computer")
347347

348348
require.Equal(t, 1, metrics.ResourceMetrics().Len())
349-
sm := metrics.ResourceMetrics().At(0).ScopeMetrics().At(0)
349+
rm := metrics.ResourceMetrics().At(0)
350+
require.Equal(t, "oxide", rm.Resource().Attributes().AsRaw()["service.name"])
351+
require.Equal(t, "example.oxide.computer", rm.Resource().Attributes().AsRaw()["oxide.host"])
352+
sm := rm.ScopeMetrics().At(0)
350353
require.Equal(t, len(wantMetrics), sm.Metrics().Len())
351354

352355
for i := 0; i < sm.Metrics().Len(); i++ {

0 commit comments

Comments
 (0)