Skip to content

Commit

Permalink
Fix/metrics interpolation (#2030)
Browse files Browse the repository at this point in the history
* fix(agent): fixed tags not being pivoted in the metrics

Signed-off-by: Luiz Pegoraro <luiz.pegoraro@encora.com>

* fix(agent): fixed metrics with multiple data points not having the attributes.

Signed-off-by: Luiz Pegoraro <luiz.pegoraro@encora.com>

Signed-off-by: Luiz Pegoraro <luiz.pegoraro@encora.com>
  • Loading branch information
lpegoraro authored Nov 18, 2022
1 parent d949c0b commit 1837962
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
6 changes: 3 additions & 3 deletions agent/backend/pktvisor/pktvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type pktvisorBackend struct {
adminAPIProtocol string

// added for Strings
agentTags string
agentTags map[string]string

// OpenTelemetry management
scrapeOtel bool
Expand Down Expand Up @@ -300,8 +300,8 @@ func (p *pktvisorBackend) Configure(logger *zap.Logger, repo policies.PolicyRepo
if p.adminAPIPort, prs = config["api_port"]; !prs {
return errors.New("you must specify pktvisor admin API port")
}
if agentTags, ok := config["agent_tags"]; ok {
p.agentTags = agentTags
if agentTags, ok := otelConfig["agent_tags"]; ok {
p.agentTags = agentTags.(map[string]string)
}

for k, v := range otelConfig {
Expand Down
6 changes: 3 additions & 3 deletions agent/otel/bridgeservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ type AgentBridgeService interface {
type AgentDataPerPolicy struct {
PolicyID string
Datasets string
AgentTags string
AgentTags map[string]string
}

var _ AgentBridgeService = (*bridgeService)(nil)

type bridgeService struct {
policyRepo policies.PolicyRepo
AgentTags string
AgentTags map[string]string
}

func NewBridgeService(policyRepo *policies.PolicyRepo, agentTags string) *bridgeService {
func NewBridgeService(policyRepo *policies.PolicyRepo, agentTags map[string]string) *bridgeService {
return &bridgeService{
policyRepo: *policyRepo,
AgentTags: agentTags,
Expand Down
26 changes: 20 additions & 6 deletions agent/otel/otlpmqttexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,25 @@ func (e *exporter) injectAttribute(metricsRequest pmetricotlp.Request, attribute
metricItem := metrics.At(i)
switch metricItem.Type() {
case pmetric.MetricTypeExponentialHistogram:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).ExponentialHistogram().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.ExponentialHistogram().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeGauge:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Gauge().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Gauge().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeHistogram:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Histogram().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Histogram().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeSum:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Sum().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Sum().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeSummary:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Summary().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Summary().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
default:
e.logger.Error("Unknown metric type: " + metricItem.Type().String())
}
Expand Down Expand Up @@ -204,7 +214,11 @@ func (e *exporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error {
// injecting policy ID attribute on metrics
tr = e.injectAttribute(tr, "policy_id", e.policyID)
tr = e.injectAttribute(tr, "dataset_ids", datasets)
tr = e.injectAttribute(tr, "agent_tags", agentData.AgentTags)
// Insert pivoted agentTags
for key, value := range agentData.AgentTags {
tr = e.injectAttribute(tr, key, value)
}

e.logger.Info("scraped metrics for policy", zap.String("policy", e.policyName), zap.String("policy_id", e.policyID))
request, err := tr.MarshalProto()
if err != nil {
Expand Down
40 changes: 30 additions & 10 deletions sinker/otel/orbreceiver/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,25 @@ func (r *OrbReceiver) injectAttribute(metricsRequest pmetricotlp.Request, attrib
metricItem := metrics.At(i)
switch metricItem.Type() {
case pmetric.MetricTypeExponentialHistogram:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).ExponentialHistogram().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.ExponentialHistogram().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeGauge:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Gauge().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Gauge().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeHistogram:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Histogram().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Histogram().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeSum:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Sum().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Sum().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
case pmetric.MetricTypeSummary:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Summary().DataPoints().At(0).Attributes().PutStr(attribute, value)
for i := 0; i < metricItem.Summary().DataPoints().Len(); i++ {
metricItem.Gauge().DataPoints().At(i).Attributes().PutStr(attribute, value)
}
default:
r.cfg.Logger.Error("Unknown metric type: " + metricItem.Type().String())
}
Expand All @@ -191,15 +201,25 @@ func (r *OrbReceiver) deleteAttribute(metricsRequest pmetricotlp.Request, attrib
metricItem := metrics.At(i)
switch metricItem.Type() {
case pmetric.MetricTypeExponentialHistogram:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).ExponentialHistogram().DataPoints().At(0).Attributes().Remove(attribute)
for i := 0; i < metricItem.ExponentialHistogram().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(0).Attributes().Remove(attribute)
}
case pmetric.MetricTypeGauge:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Gauge().DataPoints().At(0).Attributes().Remove(attribute)
for i := 0; i < metricItem.Gauge().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(0).Attributes().Remove(attribute)
}
case pmetric.MetricTypeHistogram:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Histogram().DataPoints().At(0).Attributes().Remove(attribute)
for i := 0; i < metricItem.Histogram().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(0).Attributes().Remove(attribute)
}
case pmetric.MetricTypeSum:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Sum().DataPoints().At(0).Attributes().Remove(attribute)
for i := 0; i < metricItem.Sum().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(0).Attributes().Remove(attribute)
}
case pmetric.MetricTypeSummary:
metricsRequest.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i).Summary().DataPoints().At(0).Attributes().Remove(attribute)
for i := 0; i < metricItem.Summary().DataPoints().Len(); i++ {
metricItem.ExponentialHistogram().DataPoints().At(0).Attributes().Remove(attribute)
}
default:
r.cfg.Logger.Error("Unknown metric type: " + metricItem.Type().String())
}
Expand Down

0 comments on commit 1837962

Please sign in to comment.