Skip to content

Commit

Permalink
Add configuration option for remapping otel metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar committed May 9, 2024
1 parent 7b581db commit 9164551
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
18 changes: 13 additions & 5 deletions input/otlp/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type ConsumerConfig struct {
// Semaphore holds a semaphore on which Processor.HandleStream will acquire a
// token before proceeding, to limit concurrency.
Semaphore input.Semaphore

// RemapOTelMetrics remaps certain OpenTelemetry metrics to elastic metrics.
// Note that both, OTel and Elastic, metrics would be published.
RemapOTelMetrics bool
}

// Consumer transforms OpenTelemetry data to the Elastic APM data model,
Expand All @@ -64,12 +68,16 @@ func NewConsumer(config ConsumerConfig) *Consumer {
} else {
config.Logger = config.Logger.Named("otel")
}
return &Consumer{
config: config,
sem: config.Semaphore,
remappers: []remapper{
var remappers []remapper
if config.RemapOTelMetrics {
remappers = []remapper{
hostmetrics.NewRemapper(config.Logger),
},
}
}
return &Consumer{
config: config,
sem: config.Semaphore,
remappers: remappers,
}
}

Expand Down
8 changes: 5 additions & 3 deletions input/otlp/hostmetrics/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ func remapMemoryMetrics(src, out pmetric.MetricSlice, dataset string) error {
var total, free, cached, usedBytes, actualFree, actualUsedBytes int64
var usedPercent, actualUsedPercent float64

// iterate all metrics in the current scope and generate the additional Elastic system integration metrics
// iterate all metrics in the current scope and generate the additional Elastic system
// integration metrics
for i := 0; i < src.Len(); i++ {
metric := src.At(i)
// TODO (lahisvjar): If system.memory.utilization is not present then derive it from system.memory.usage
// TODO (lahisvjar): If system.memory.utilization is not present then derive it from
// system.memory.usage.
if metric.Name() == "system.memory.usage" {
dataPoints := metric.Sum().DataPoints()
for j := 0; j < dataPoints.Len(); j++ {
Expand All @@ -29,7 +31,7 @@ func remapMemoryMetrics(src, out pmetric.MetricSlice, dataset string) error {
total += value
case "free":
// TODO (lahsivjar): if system.linux.memory.available is present then use it for
// free mem calculation
// free mem calculation.
free = value
usedBytes -= value
total += value
Expand Down
14 changes: 8 additions & 6 deletions input/otlp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ func (c *Consumer) handleScopeMetrics(
// Handle remapping if any. Remapped metrics will be added to a new
// metric slice and then processed as any other metric in the scope.
// TODO (lahsivjar): Possible to approximate capacity of the slice?
remappedMetrics := pmetric.NewMetricSlice()
for _, r := range c.remappers {
r.Remap(in, remappedMetrics)
}
for i := 0; i < remappedMetrics.Len(); i++ {
c.addMetric(remappedMetrics.At(i), ms, remainingDataPoints, remainingMetrics)
if len(c.remappers) > 0 {
remappedMetrics := pmetric.NewMetricSlice()
for _, r := range c.remappers {
r.Remap(in, remappedMetrics)
}
for i := 0; i < remappedMetrics.Len(); i++ {
c.addMetric(remappedMetrics.At(i), ms, remainingDataPoints, remainingMetrics)
}
}
// Process all the metrics added to the metricset.
for key, ms := range ms {
Expand Down

0 comments on commit 9164551

Please sign in to comment.