Add Custom Collection
The framework provides convenient APIs, including module startup, data storage, container information, BPF-related (load, attach, read, detach, unload), etc. You can implement custom collection logic and flexibly choose the appropriate collection mode and storage method.
Tracing Type
Based on your scenarios, you can implement the ITracingEvent interface in the core/autotracing or core/events directory to complete tracing-type collection.
// ITracingEvent represents a tracing/event
type ITracingEvent interface {
Start(ctx context.Context) error
}
example:
type exampleTracing struct{}
// Register callback
func init() {
tracing.RegisterEventTracing("example", newExample)
}
// Create tracing
func newExample() (*tracing.EventTracingAttr, error) {
return &tracing.EventTracingAttr{
TracingData: &exampleTracing{},
Internal: 10, // Interval for enable tracing again (in seconds)
Flag: tracing.FlagTracing, // mark as tracing type
}, nil
}
// Implement ITracingEvent
func (t *exampleTracing) Start(ctx context.Context) error {
// do something
...
// Save data to ES and local file
storage.Save("example", ccontainerID, time.Now(), tracerData)
}
// Implement Collector interface for Prometheus format output (optional)
func (c *exampleTracing) Update() ([]*metric.Data, error) {
// from tracerData to prometheus.Metric
...
return data, nil
}
Metric Type
Implement the Collector interface in the path core/metrics to complete metric-type collection.
type Collector interface {
// Get new metrics and expose them via prometheus registry.
Update() ([]*Data, error)
}
example:
type exampleMetric struct{}
// Register callback
func init() {
tracing.RegisterEventTracing("example", newExample)
}
// Create Metric
func newExample() (*tracing.EventTracingAttr, error) {
return &tracing.EventTracingAttr{
TracingData: &filenrCollector{
metric: []*metric.Data{
metric.NewGaugeData("name1", 0, "description of example_name1", nil),
metric.NewGaugeData("name2", 0, "description of example_name2", nil),
},
},
Flag: tracing.FlagMetric, // mark as Metric type
}, nil
}
// Implement Collector interface for Prometheus format output
func (c *exampleMetric) Update() ([]*metric.Data, error) {
// do something
...
return data, nil
}
The path core of the project includes multiple useful examples of the three collection modules, covering BPF code, map data interaction, container information, and more. For further details, refer to the corresponding code implementations.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.