metrics.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package render
  2. import (
  3. "context"
  4. "github.com/weaveworks/scope/report"
  5. )
  6. // PropagateSingleMetrics creates a renderer which propagates metrics
  7. // from a node's child to the node. The child is selected based on the
  8. // specified topology. No metrics are propagated when there is more
  9. // than one such child.
  10. func PropagateSingleMetrics(topology string, r Renderer) Renderer {
  11. return propagateSingleMetrics{topology: topology, r: r}
  12. }
  13. type propagateSingleMetrics struct {
  14. topology string
  15. r Renderer
  16. }
  17. func (p propagateSingleMetrics) Render(ctx context.Context, rpt report.Report) Nodes {
  18. nodes := p.r.Render(ctx, rpt)
  19. outputs := make(report.Nodes, len(nodes.Nodes))
  20. for id, n := range nodes.Nodes {
  21. var first report.Node
  22. found := 0
  23. n.Children.ForEach(func(child report.Node) {
  24. if child.Topology == p.topology {
  25. if _, ok := child.Latest.Lookup(report.DoesNotMakeConnections); !ok {
  26. if found == 0 {
  27. first = child
  28. }
  29. found++
  30. }
  31. }
  32. })
  33. if found == 1 {
  34. n = n.WithMetrics(first.Metrics)
  35. }
  36. outputs[id] = n
  37. }
  38. return Nodes{Nodes: outputs, Filtered: nodes.Filtered}
  39. }