Skip to content

Commit 512ce89

Browse files
committed
Switch to typed enums.
1 parent cd885c1 commit 512ce89

6 files changed

Lines changed: 115 additions & 278 deletions

File tree

collector/config.example.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ receivers:
44
# host: ${env:OXIDE_HOST}
55
# token: ${env:OXIDE_TOKEN}
66
metric_patterns:
7+
# - .*
78
- virtual_machine:.*
8-
- virtual_disk:.*
9-
- hardware_component:.*
9+
# - virtual_disk:.*
10+
# - hardware_component:.*
1011
collection_interval: 60s
1112

1213
processors:
@@ -15,6 +16,9 @@ processors:
1516
exporters:
1617
prometheus:
1718
endpoint: "0.0.0.0:9091"
19+
translation_strategy: UnderscoreEscapingWithSuffixes
20+
resource_to_telemetry_conversion:
21+
enabled: true
1822

1923
service:
2024
telemetry:
@@ -30,5 +34,5 @@ service:
3034
pipelines:
3135
metrics:
3236
receivers: [oxide]
33-
processors: [batch]
37+
processors: []
3438
exporters: [prometheus]

collector/manifest.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ providers:
5454

5555
replaces:
5656
- github.com/oxidecomputer/oxidereceiver => ..
57+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.25.0
44

55
require (
66
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.139.0
7-
github.com/oxidecomputer/oxide.go v0.7.0
7+
github.com/oxidecomputer/oxide.go v0.7.1-0.20251125151852-d4d5bb8b949b
88
github.com/stretchr/testify v1.11.1
99
go.opentelemetry.io/collector/component v1.45.0
1010
go.opentelemetry.io/collector/consumer v1.45.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.139.0
3434
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.139.0/go.mod h1:0v3C+DUgl/J/Q9g/xK5m0nsYnHgqzH5ICEtCzalO2uY=
3535
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.139.0 h1:6/j0Ta8ZJnmAFVEoC3aZ1Hs19RB4fHzlN6kOZhsBJqM=
3636
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.139.0/go.mod h1:VfA8xHz4xg7Fyj5bBsCDbOO3iVYzDn9wP/QFsjcAE5c=
37-
github.com/oxidecomputer/oxide.go v0.7.0 h1:ZRxH3vSgwZys/dsQthphW4o57xp0BivUCkND18b5nr4=
38-
github.com/oxidecomputer/oxide.go v0.7.0/go.mod h1:e/Azkl4nFUrsdsJ113siW/ZNUQkiG539e4i7a6GEcD8=
37+
github.com/oxidecomputer/oxide.go v0.7.1-0.20251125151852-d4d5bb8b949b h1:yQJF01Fq9V5/Bho2Bcz/1iY7pU0WbGIcO6Fson0LcQg=
38+
github.com/oxidecomputer/oxide.go v0.7.1-0.20251125151852-d4d5bb8b949b/go.mod h1:e/Azkl4nFUrsdsJ113siW/ZNUQkiG539e4i7a6GEcD8=
3939
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
4040
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
4141
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

scraper.go

Lines changed: 50 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package oxidereceiver
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"regexp"
87
"slices"
@@ -304,22 +303,31 @@ func (s *oxideScraper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
304303

305304
func addLabels(series oxide.Timeseries, table oxide.OxqlTable, resource pcommon.Resource) error {
306305
for key, value := range series.Fields {
307-
switch value.Type {
308-
case oxide.FieldValueTypeString:
309-
strValue, ok := value.Value.(string)
310-
if !ok {
311-
return fmt.Errorf("couldn't cast label %+v for metric %s to string; got unexpected type %T", value.Value, table.Name, value.Value)
312-
}
313-
resource.Attributes().PutStr(key, strValue)
314-
case oxide.FieldValueTypeI8, oxide.FieldValueTypeI16, oxide.FieldValueTypeI32, oxide.FieldValueTypeI64,
315-
oxide.FieldValueTypeU8, oxide.FieldValueTypeU16, oxide.FieldValueTypeU32, oxide.FieldValueTypeU64:
316-
intValue, ok := value.Value.(float64)
317-
if !ok {
318-
return fmt.Errorf("couldn't cast label%+v for metric %s to float64; got unexpected type %T", value.Value, table.Name, value.Value)
319-
}
320-
resource.Attributes().PutInt(key, int64(intValue))
306+
switch v := value.Value.(type) {
307+
case oxide.FieldValueString:
308+
resource.Attributes().PutStr(key, string(v))
309+
case oxide.FieldValueUuid:
310+
resource.Attributes().PutStr(key, string(v))
311+
case oxide.FieldValueIpAddr:
312+
resource.Attributes().PutStr(key, string(v))
313+
case oxide.FieldValueI8:
314+
resource.Attributes().PutInt(key, int64(v))
315+
case oxide.FieldValueI16:
316+
resource.Attributes().PutInt(key, int64(v))
317+
case oxide.FieldValueI32:
318+
resource.Attributes().PutInt(key, int64(v))
319+
case oxide.FieldValueI64:
320+
resource.Attributes().PutInt(key, int64(v))
321+
case oxide.FieldValueU8:
322+
resource.Attributes().PutInt(key, int64(v))
323+
case oxide.FieldValueU16:
324+
resource.Attributes().PutInt(key, int64(v))
325+
case oxide.FieldValueU32:
326+
resource.Attributes().PutInt(key, int64(v))
327+
case oxide.FieldValueU64:
328+
resource.Attributes().PutInt(key, int64(v))
321329
default:
322-
resource.Attributes().PutStr(key, fmt.Sprintf("%v", value.Value))
330+
return fmt.Errorf("unrecognized value type %T for label %s in metric %s", value.Value, key, table.Name)
323331
}
324332
}
325333
return nil
@@ -344,28 +352,12 @@ func addHistogram(dataPoints pmetric.HistogramDataPointSlice, quantileGauge pmet
344352
dp := dataPoints.AppendEmpty()
345353
dp.SetTimestamp(pcommon.NewTimestampFromTime(series.Points.Timestamps[idx]))
346354

347-
values, ok := point.Values.Values.([]any)
348-
if !ok {
349-
return fmt.Errorf("couldn't cast values %+v for metric %s to []any; got unexpected type %T", point.Values.Values, table.Name, point.Values.Values)
350-
}
351-
if len(timestamps) != len(values) {
352-
return fmt.Errorf("invariant violated: number of timestamps %d must match number of values %d", len(timestamps), len(values))
353-
}
354-
for _, value := range values {
355-
// The histogram value is an `any`, so marshal and unmarshal json to fit it into the appropriate distribution type.
356-
marshalled, err := json.Marshal(value)
357-
if err != nil {
358-
return fmt.Errorf("couldn't marshal distribution %+v for metric %s: %w", value, table.Name, err)
355+
switch values := point.Values.Values.(type) {
356+
case oxide.ValueArrayIntegerDistribution:
357+
if len(timestamps) != len(values.Values) {
358+
return fmt.Errorf("invariant violated: number of timestamps %d must match number of values %d", len(timestamps), len(values.Values))
359359
}
360-
361-
switch point.Values.Type {
362-
case oxide.ValueArrayTypeIntegerDistribution:
363-
// Unmarshal the marshalled JSON back to the expected histogram type.
364-
var distValue oxide.Distributionint64
365-
if err := json.Unmarshal(marshalled, &distValue); err != nil {
366-
return fmt.Errorf("couldn't unmarshal distribution %+v for metric %s: %w", value, table.Name, err)
367-
}
368-
360+
for _, distValue := range values.Values {
369361
bins := make([]float64, len(distValue.Bins))
370362
for idx := range distValue.Bins {
371363
bins[idx] = float64(distValue.Bins[idx])
@@ -382,13 +374,12 @@ func addHistogram(dataPoints pmetric.HistogramDataPointSlice, quantileGauge pmet
382374
dp.SetTimestamp(pcommon.NewTimestampFromTime(timestamps[idx]))
383375

384376
addQuantiles(quantileGauge, []oxide.Quantile{distValue.P50, distValue.P90, distValue.P99}, dp.Timestamp())
385-
case oxide.ValueArrayTypeDoubleDistribution:
386-
// Unmarshal the marshalled JSON back to the expected histogram type.
387-
var distValue oxide.Distributiondouble
388-
if err := json.Unmarshal(marshalled, &distValue); err != nil {
389-
return fmt.Errorf("couldn't unmarshal distribution %+v for metric %s: %w", value, table.Name, err)
390-
}
391-
377+
}
378+
case oxide.ValueArrayDoubleDistribution:
379+
if len(timestamps) != len(values.Values) {
380+
return fmt.Errorf("invariant violated: number of timestamps %d must match number of values %d", len(timestamps), len(values.Values))
381+
}
382+
for _, distValue := range values.Values {
392383
bins := make([]float64, len(distValue.Bins))
393384
for idx := range distValue.Bins {
394385
bins[idx] = float64(distValue.Bins[idx])
@@ -406,6 +397,8 @@ func addHistogram(dataPoints pmetric.HistogramDataPointSlice, quantileGauge pmet
406397

407398
addQuantiles(quantileGauge, []oxide.Quantile{distValue.P50, distValue.P90, distValue.P99}, dp.Timestamp())
408399
}
400+
default:
401+
return fmt.Errorf("unrecognized value type %T for metric %s", point.Values.Values, table.Name)
409402
}
410403
}
411404
return nil
@@ -414,67 +407,34 @@ func addHistogram(dataPoints pmetric.HistogramDataPointSlice, quantileGauge pmet
414407
func addPoint(dataPoints pmetric.NumberDataPointSlice, table oxide.OxqlTable, series oxide.Timeseries) error {
415408
timestamps := series.Points.Timestamps
416409
for _, point := range series.Points.Values {
417-
switch point.Values.Type {
418-
case oxide.ValueArrayTypeInteger:
419-
values, ok := point.Values.Values.([]any)
420-
if !ok {
421-
return fmt.Errorf("couldn't cast values %+v for metric %s to []any; got unexpected type %T", point.Values.Values, table.Name, point.Values.Values)
410+
switch values := point.Values.Values.(type) {
411+
case oxide.ValueArrayInteger:
412+
if len(timestamps) != len(values.Values) {
413+
return fmt.Errorf("invariant violated: number of timestamps %d must match number of values %d", len(timestamps), len(values.Values))
422414
}
423-
if len(timestamps) != len(values) {
424-
return fmt.Errorf("invariant violated: number of timestamps %d must match number of values %d", len(timestamps), len(values))
425-
}
426-
for idx, value := range values {
427-
if value == nil {
428-
continue
429-
}
430-
intValue, ok := value.(float64)
431-
if !ok {
432-
return fmt.Errorf("couldn't cast value %+v for metric %s to float; got type %T", value, table.Name, value)
433-
}
415+
for idx, value := range values.Values {
434416
dp := dataPoints.AppendEmpty()
435417
dp.SetTimestamp(pcommon.NewTimestampFromTime(timestamps[idx]))
436-
dp.SetIntValue(int64(intValue))
437-
}
438-
case oxide.ValueArrayTypeDouble:
439-
values, ok := point.Values.Values.([]any)
440-
if !ok {
441-
return fmt.Errorf("couldn't cast values %+v for metric %s to []any; got unexpected type %T", point.Values.Values, table.Name, point.Values.Values)
418+
dp.SetIntValue(int64(value))
442419
}
443-
for idx, value := range values {
444-
if value == nil {
445-
continue
446-
}
447-
floatValue, ok := value.(float64)
448-
if !ok {
449-
return fmt.Errorf("couldn't cast value %+v for metric %s to float; got type %T", value, table.Name, value)
450-
}
420+
case oxide.ValueArrayDouble:
421+
for idx, value := range values.Values {
451422
dp := dataPoints.AppendEmpty()
452423
dp.SetTimestamp(pcommon.NewTimestampFromTime(series.Points.Timestamps[idx]))
453-
dp.SetDoubleValue(floatValue)
454-
}
455-
case oxide.ValueArrayTypeBoolean:
456-
values, ok := point.Values.Values.([]any)
457-
if !ok {
458-
return fmt.Errorf("couldn't cast values %+v for metric %s to []any; got unexpected type %T", point.Values.Values, table.Name, point.Values.Values)
424+
dp.SetDoubleValue(value)
459425
}
460-
for idx, value := range values {
461-
if value == nil {
462-
continue
463-
}
464-
boolValue, ok := value.(bool)
465-
if !ok {
466-
return fmt.Errorf("couldn't cast value %+v for metric %s to bool; got type %T", value, table.Name, value)
467-
}
426+
case oxide.ValueArrayBoolean:
427+
for idx, value := range values.Values {
468428
dp := dataPoints.AppendEmpty()
469429
dp.SetTimestamp(pcommon.NewTimestampFromTime(series.Points.Timestamps[idx]))
470430
intValue := 0
471-
if boolValue {
431+
if value {
472432
intValue = 1
473433
}
474434
dp.SetIntValue(int64(intValue))
475435
}
476436
default:
477-
return fmt.Errorf("got unexpected metric value type %s", point.Values.Type)
437+
return fmt.Errorf("unrecognized value type %T for metric %s", point.Values.Values, table.Name)
478438
}
479439
}
480440
return nil

0 commit comments

Comments
 (0)