domain

package
v1.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

README

Domain Layer

The domain layer implements the Application Core in CloudZero Agent's hexagonal architecture, containing core business logic for cost allocation, metric processing, and Kubernetes resource management.

Key Components

Metric Processing
  • metric_collector.go - Processes all Prometheus metrics (both cost and observability)
  • filter/ - Classifies metrics by type (cost vs observability)
  • shipper/ - Uploads processed data to CloudZero platform
Kubernetes Integration
  • webhook/ - Admission control for resource metadata collection
  • k8s/ - Kubernetes client abstractions and utilities
Operational Services
  • monitor/ - Certificate management and secret rotation
  • healthz/ - Health checking and service monitoring
  • diagnostic/ - System diagnostics and troubleshooting

Architecture

Uses hexagonal (ports and adapters) architecture with dependency injection. Business logic depends on interfaces defined in ../types/, with concrete implementations in adapters.

Testing

# Test domain layer
make test GO_TEST_TARGET=./app/domain

# Test with mocks
make generate && make test GO_TEST_TARGET=./app/domain

Development

See CLAUDE.md for detailed architecture documentation and AI-assistant guidance.

Documentation

Overview

Package domain contains the Application Core business logic for the CloudZero Agent.

This package implements the domain layer of hexagonal architecture, containing metric collection, filtering, and processing services. It orchestrates the flow from Prometheus remote_write ingestion through classification and storage preparation for the CloudZero cost allocation platform.

Index

Constants

View Source
const (
	// FileCreated event type
	FileCreated = "file_created"
	// FileChanged event type
	FileChanged = "file_changed"
	// FileDeleted event type
	FileDeleted = "file_deleted"
	// FileRenamed event type
	FileRenamed = "file_rename"
)
View Source
const (
	// SnappyBlockCompression identifies snappy compression used in Prometheus remote_write protocol.
	SnappyBlockCompression = "snappy"
)

Protocol and encoding constants for Prometheus remote_write integration.

Variables

View Source
var (
	// ErrJSONUnmarshal indicates failure to parse metric data from request body.
	// This error occurs when Prometheus remote_write data is malformed or incompatible.
	ErrJSONUnmarshal = errors.New("failed to parse metric from request body")

	// ErrMetricIDMismatch indicates inconsistency between URL path and request body identifiers.
	// This error prevents data corruption in metric processing operations.
	ErrMetricIDMismatch = errors.New("metric ID in path does not match product ID in body")
)

Domain-level errors for metric collection operations.

Functions

This section is empty.

Types

type FileMonitor

type FileMonitor struct {
	// contains filtered or unexported fields
}

FileMonitor is responsible for monitoring changes in the secret file, and will notify components in the application that depend on the secret filePath can be a directory or a single file

func NewFileMonitor

func NewFileMonitor(ctx context.Context, bus types.Bus, filePath string) (*FileMonitor, error)

NewFileMonitor creates a new file monitor.

func (*FileMonitor) Close

func (m *FileMonitor) Close()

func (*FileMonitor) Start

func (m *FileMonitor) Start()

type MetricCollector

type MetricCollector struct {
	// contains filtered or unexported fields
}

MetricCollector orchestrates the core metric collection and classification pipeline. This service receives Prometheus remote_write requests, classifies metrics into cost vs observability categories, and routes them to appropriate storage backends for the CloudZero platform.

func NewMetricCollector

func NewMetricCollector(s *config.Settings, clock types.TimeProvider, costStore types.WritableStore, observabilityStore types.WritableStore) (*MetricCollector, error)

NewMetricCollector creates a MetricCollector and initializes the background flush cycle. The collector starts accepting Prometheus remote_write requests immediately and begins periodic flushing of buffered metrics to storage backends based on configuration.

func (*MetricCollector) Close

func (d *MetricCollector) Close()

Close stops the flushing goroutine gracefully.

func (*MetricCollector) DecodeV1

func (d *MetricCollector) DecodeV1(data []byte) ([]types.Metric, error)

DecodeV1 decompresses and decodes a Protobuf v1 WriteRequest, then converts it to a slice of Metric structs.

func (*MetricCollector) DecodeV2

func (d *MetricCollector) DecodeV2(data []byte) ([]types.Metric, *remote.WriteResponseStats, error)

DecodeV2 decompresses and decodes a Protobuf v2 WriteRequest, then converts it to a slice of Metric structs and collects stats.

func (*MetricCollector) Flush

func (d *MetricCollector) Flush(ctx context.Context) error

Flush triggers the flushing of accumulated metrics.

func (*MetricCollector) PutMetrics

func (d *MetricCollector) PutMetrics(ctx context.Context, contentType, encodingType string, body []byte) (*remote.WriteResponseStats, error)

PutMetrics processes a Prometheus remote_write request and stores classified metrics. This method handles decompression, protocol version detection, metric classification, and routing to appropriate storage backends while maintaining compatibility statistics.

func (*MetricCollector) Settings added in v1.2.0

func (d *MetricCollector) Settings() *config.Settings

Settings returns the collector configuration for external inspection and validation. This method provides read-only access to the collector's operational parameters.

type MetricFilter

type MetricFilter struct {
	// contains filtered or unexported fields
}

MetricFilter is a filter that can be used to filter metrics, including labels, according to the configuration provided.

func NewMetricFilter

func NewMetricFilter(cfg *config.Metrics) (*MetricFilter, error)

NewMetricFilter creates a new MetricFilter for the given configuration.

func (*MetricFilter) Filter

func (mf *MetricFilter) Filter(metrics []types.Metric) (costMetrics []types.Metric, observabilityMetrics []types.Metric, droppedMetrics []types.Metric)

Filter processes the supplied metrics through the filter. It returns three slices: the first being the list of cost metrics, the second being the list of observability metrics, and the third being the list of dropped metrics. Both cost and observability metrics have also had the labels filtered to only include those that match the filter. Dropped metrics are defined as those which matched neither the cost nor observability filters.

Directories

Path Synopsis
Package certificate provides certificate generation and management functionality for the CloudZero Agent, including TLS certificate creation and Kubernetes webhook configuration management.
Package certificate provides certificate generation and management functionality for the CloudZero Agent, including TLS certificate creation and Kubernetes webhook configuration management.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
Package diagnostic contains an interface to be implemented by diagnostics providers.
Package diagnostic contains an interface to be implemented by diagnostics providers.
catalog
Package catalog contains the registry of diagnostics.
Package catalog contains the registry of diagnostics.
cz
Package cz contains code for checking a CloudZero API token.
Package cz contains code for checking a CloudZero API token.
k8s/namespace
Package namespace contains code for checking the Kubernetes configuration.
Package namespace contains code for checking the Kubernetes configuration.
k8s/provider
Package provider contains code for checking the Kubernetes configuration.
Package provider contains code for checking the Kubernetes configuration.
k8s/version
Package version contains code for checking the Kubernetes configuration.
Package version contains code for checking the Kubernetes configuration.
kms
Package kms contains code for testing access the Kubernetes Management Service.
Package kms contains code for testing access the Kubernetes Management Service.
prom/config
Package config contains a diagnostic provider for checking the Prometheus configuration.
Package config contains a diagnostic provider for checking the Prometheus configuration.
prom/version
Package version contains a diagnostic provider for checking the Prometheus version.
Package version contains a diagnostic provider for checking the Prometheus version.
runner
Package runner contains tools for running diagnostics.
Package runner contains tools for running diagnostics.
stage
Package stage contains a diagnostic provider for checking the stage.
Package stage contains a diagnostic provider for checking the stage.
webhook
Package webhook contains code for checking a CloudZero API token.
Package webhook contains code for checking a CloudZero API token.
Package filter provides high-performance metric filtering utilities for CloudZero Agent cost allocation.
Package filter provides high-performance metric filtering utilities for CloudZero Agent cost allocation.
Package healthz provides a simple and extensible health check mechanism for HTTP services.
Package healthz provides a simple and extensible health check mechanism for HTTP services.
Package housekeeper provides a mechanism for cleaning up stale data in a resource store.
Package housekeeper provides a mechanism for cleaning up stale data in a resource store.
k8s
Package k8s gives a unified interface for k8s information to be retrieved.
Package k8s gives a unified interface for k8s information to be retrieved.
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
Package monitor provides secure dynamic secret management and certificate monitoring for CloudZero Agent operations.
Package monitor provides secure dynamic secret management and certificate monitoring for CloudZero Agent operations.
Package pusher provides a mechanism for pushing metrics to a remote write endpoint.
Package pusher provides a mechanism for pushing metrics to a remote write endpoint.
Package shipper provides metric upload and transmission services for the CloudZero Agent.
Package shipper provides metric upload and transmission services for the CloudZero Agent.
Package transform provides metric transformation capabilities for standardizing vendor-specific metrics into common formats for cost allocation.
Package transform provides metric transformation capabilities for standardizing vendor-specific metrics into common formats for cost allocation.
catalog
Package catalog provides a catalog-based metric transformer that routes metrics to registered specialized transformers.
Package catalog provides a catalog-based metric transformer that routes metrics to registered specialized transformers.
dcgm
Package dcgm provides NVIDIA DCGM metric transformation for cost allocation.
Package dcgm provides NVIDIA DCGM metric transformation for cost allocation.
Package webhook provides Kubernetes admission webhook business logic for CloudZero Agent cost allocation.
Package webhook provides Kubernetes admission webhook business logic for CloudZero Agent cost allocation.
backfiller
Package backfiller provides functionality to backfill Kubernetes Resource objects, and if enabled invokes the webhook domain logic
Package backfiller provides functionality to backfill Kubernetes Resource objects, and if enabled invokes the webhook domain logic
handler
Package handler admission webhook handlers (hook.Handler) for various resource types.
Package handler admission webhook handlers (hook.Handler) for various resource types.
helper
Package helper contains decode helper methods for transforming kubernetes metav1.Objects into K8sObjects
Package helper contains decode helper methods for transforming kubernetes metav1.Objects into K8sObjects
hook
Package hook contains structures and interfaces for implementing admission webhooks handlers.
Package hook contains structures and interfaces for implementing admission webhooks handlers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL