|
| 1 | +// Package telemetry provides functionality for collecting and exporting NIC telemetry data. |
| 2 | +package telemetry |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/nginxinc/kubernetes-ingress/internal/configs" |
| 10 | + |
| 11 | + k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned" |
| 12 | + "k8s.io/apimachinery/pkg/util/wait" |
| 13 | + "k8s.io/client-go/kubernetes" |
| 14 | + |
| 15 | + "github.com/golang/glog" |
| 16 | +) |
| 17 | + |
| 18 | +// Option is a functional option used for configuring TraceReporter. |
| 19 | +type Option func(*Collector) error |
| 20 | + |
| 21 | +// WithExporter configures telemetry collector to use given exporter. |
| 22 | +// |
| 23 | +// This may change in the future when we use exporter implemented |
| 24 | +// in the external module. |
| 25 | +func WithExporter(e Exporter) Option { |
| 26 | + return func(c *Collector) error { |
| 27 | + c.Exporter = e |
| 28 | + return nil |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Collector is NIC telemetry data collector. |
| 33 | +type Collector struct { |
| 34 | + // Exporter is a temp exporter for exporting telemetry data. |
| 35 | + // The concrete implementation will be implemented in a separate module. |
| 36 | + Exporter Exporter |
| 37 | + |
| 38 | + // Configuration for the collector. |
| 39 | + Config CollectorConfig |
| 40 | +} |
| 41 | + |
| 42 | +// CollectorConfig contains configuration options for a Collector |
| 43 | +type CollectorConfig struct { |
| 44 | + // K8sClientReader is a kubernetes client. |
| 45 | + K8sClientReader kubernetes.Interface |
| 46 | + |
| 47 | + // CustomK8sClientReader is a kubernetes client for our CRDs. |
| 48 | + // Note: May not need this client. |
| 49 | + CustomK8sClientReader k8s_nginx.Interface |
| 50 | + |
| 51 | + // Period to collect telemetry |
| 52 | + Period time.Duration |
| 53 | + |
| 54 | + Configurator *configs.Configurator |
| 55 | +} |
| 56 | + |
| 57 | +// NewCollector takes 0 or more options and creates a new TraceReporter. |
| 58 | +// If no options are provided, NewReporter returns TraceReporter |
| 59 | +// configured to gather data every 24h. |
| 60 | +func NewCollector(cfg CollectorConfig, opts ...Option) (*Collector, error) { |
| 61 | + c := Collector{ |
| 62 | + Exporter: &StdoutExporter{Endpoint: io.Discard}, |
| 63 | + Config: cfg, |
| 64 | + } |
| 65 | + for _, o := range opts { |
| 66 | + if err := o(&c); err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + } |
| 70 | + return &c, nil |
| 71 | +} |
| 72 | + |
| 73 | +// Start starts running NIC Telemetry Collector. |
| 74 | +func (c *Collector) Start(ctx context.Context) { |
| 75 | + wait.JitterUntilWithContext(ctx, c.Collect, c.Config.Period, 0.1, true) |
| 76 | +} |
| 77 | + |
| 78 | +// Collect collects and exports telemetry data. |
| 79 | +// It exports data using provided exporter. |
| 80 | +func (c *Collector) Collect(ctx context.Context) { |
| 81 | + glog.V(3).Info("Collecting telemetry data") |
| 82 | + // TODO: Re-add ctx to BuildReport when collecting Node Count. |
| 83 | + data, err := c.BuildReport() |
| 84 | + if err != nil { |
| 85 | + glog.Errorf("Error collecting telemetry data: %v", err) |
| 86 | + } |
| 87 | + err = c.Exporter.Export(ctx, data) |
| 88 | + if err != nil { |
| 89 | + glog.Errorf("Error exporting telemetry data: %v", err) |
| 90 | + } |
| 91 | + glog.V(3).Infof("Exported telemetry data: %+v", data) |
| 92 | +} |
| 93 | + |
| 94 | +// BuildReport takes context and builds report from gathered telemetry data. |
| 95 | +func (c *Collector) BuildReport() (Data, error) { |
| 96 | + d := Data{} |
| 97 | + var err error |
| 98 | + |
| 99 | + if c.Config.Configurator != nil { |
| 100 | + d.NICResourceCounts.VirtualServers, d.NICResourceCounts.VirtualServerRoutes = c.Config.Configurator.GetVirtualServerCounts() |
| 101 | + d.NICResourceCounts.TransportServers = c.Config.Configurator.GetTransportServerCounts() |
| 102 | + } |
| 103 | + return d, err |
| 104 | +} |
0 commit comments