prometheus_collector_bridge

package module
v0.0.0-...-1744a07 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 16 Imported by: 1

README

prometheus-collector-bridge

A bridge between Prometheus Go registry and OTel-Collector pipeline metric formats.

Documentation

Overview

Package prometheus_collector_bridge provides a framework for embedding Prometheus exporters as native OpenTelemetry Collector receivers.

This package enables Prometheus exporters written in Go to run directly inside an OpenTelemetry Collector.

Overview

The prometheus_collector_bridge package provides the core infrastructure for converting Prometheus exporters into OTel receivers:

  1. Config system for exporter-specific configuration with automatic validation
  2. Factory pattern for creating receiver instances
  3. Lifecycle management (Start/Shutdown)
  4. Periodic metric scraping from Prometheus registries
  5. Conversion from Prometheus to OpenTelemetry metric format

Usage

To integrate a Prometheus exporter, implement two interfaces:

ExporterLifecycleManager: Manages the exporter lifecycle

type MyExporterLifecycleManager struct {
    // exporter state
}

func (i *MyExporterLifecycleManager) Start(ctx context.Context, cfg Config) (*prometheus.Registry, error) {
    // Start your exporter and return its registry
}

func (i *MyExporterLifecycleManager) Shutdown(ctx context.Context) error {
    // Clean up resources
}

ConfigUnmarshaler: Handles exporter-specific configuration using mapstructure

type MyConfig struct {
    EnableFeature bool     `mapstructure:"enable_feature"`
    Timeout       string   `mapstructure:"timeout"`
    Items         []string `mapstructure:"items"`
}

type MyConfigUnmarshaler struct{}

func (u *MyConfigUnmarshaler) GetConfigStruct() Config {
    return &MyConfig{}
}

Then create a receiver factory:

factory := prometheus_collector_bridge.NewFactory(
    prometheus_collector_bridge.WithType("prometheus/myexporter"),
    prometheus_collector_bridge.WithInitializer(&MyExporterInitializer{}),
    prometheus_collector_bridge.WithConfigUnmarshaler(&MyConfigUnmarshaler{}),
)

Configuration

The receiver supports common configuration options:

receivers:
  prometheus/myexporter:
    scrape_interval: 30s
    exporter_config:
      enable_feature: true
      timeout: "30s"
      items: ["item1", "item2"]

The framework automatically validates configuration using mapstructure tags: - Unknown fields are rejected - Type mismatches are caught (e.g., string where bool expected) - Custom validation can be added via the Config.Validate() method

Architecture

The package follows a layered architecture:

┌─────────────────────────────────────┐
│  OTel Collector Pipeline            │
└──────────────┬──────────────────────┘
               │ ConsumeMetrics()
┌──────────────▼──────────────────────┐
│  prometheusReceiver                 │
│  - Lifecycle management             │
│  - Scrape scheduling                │
└──────────────┬──────────────────────┘
               │
┌──────────────▼──────────────────────┐
│  scraper                            │
│  - Gather from registry             │
│  - Convert to OTel format           │
└──────────────┬──────────────────────┘
               │
┌──────────────▼──────────────────────┐
│  ExporterInitializer                │
│  (Your exporter implementation)     │
└─────────────────────────────────────┘

Thread Safety

The receiver is designed to be thread-safe. The scraping loop runs in its own goroutine and coordinates gracefully with the shutdown process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFactory

func NewFactory(
	typeStr component.Type,
	lifecycleManager ExporterLifecycleManager,
	configUnmarshaler ConfigUnmarshaler,
	opts ...FactoryOption,
) receiver.Factory

NewFactory creates a new receiver factory for a Prometheus exporter. The factory uses the provided type, ExporterLifecycleManager, and ConfigUnmarshaler to manage the exporter lifecycle and configuration.

Required parameters:

  • typeStr: The receiver type identifier (e.g., "node_exporter")
  • lifecycleManager: Handles starting/stopping the exporter
  • configUnmarshaler: Unmarshals exporter-specific configuration

Optional parameters via FactoryOption:

  • WithComponentDefaults: Sets default exporter configuration values

Types

type Config

type Config interface {
	// Validate checks if the configuration is valid.
	Validate() error
}

Config is the interface that exporter-specific configurations must implement. Each Prometheus exporter will provide its own Config implementation.

type ConfigUnmarshaler

type ConfigUnmarshaler interface {
	// GetConfigStruct returns a pointer to the config struct that mapstructure
	// will populate. The struct should have appropriate mapstructure tags.
	GetConfigStruct() Config
}

ConfigUnmarshaler is the interface used to unmarshal the exporter-specific configuration using mapstructure and struct tags.

type ExporterLifecycleManager

type ExporterLifecycleManager interface {
	// Start sets up the exporter and returns a prometheus.Registry
	// containing all the metrics collectors.
	Start(ctx context.Context, exporterConfig Config) (*prometheus.Registry, error)

	// Shutdown is used to release resources when the receiver is shutting down.
	Shutdown(ctx context.Context) error
}

ExporterLifecycleManager is the interface that Prometheus exporters must implement to be embedded in the OTel Collector.

type FactoryOption

type FactoryOption func(*factoryConfig)

FactoryOption is a function that configures a Factory.

func WithComponentDefaults

func WithComponentDefaults(defaults map[string]interface{}) FactoryOption

WithComponentDefaults sets the default configuration for the component.

type ReceiverConfig

type ReceiverConfig struct {
	// ScrapeInterval defines how often to collect metrics from the exporter.
	// Default: 30s
	ScrapeInterval time.Duration `mapstructure:"scrape_interval"`

	// ExporterConfig holds the exporter-specific configuration.
	// This will be unmarshaled by the exporter's ConfigUnmarshaler.
	ExporterConfig map[string]interface{} `mapstructure:"exporter_config"`
	// contains filtered or unexported fields
}

ReceiverConfig holds the common configuration for all Prometheus exporter receivers.

func (*ReceiverConfig) GetExporterConfig

func (cfg *ReceiverConfig) GetExporterConfig() Config

GetExporterConfig returns the unmarshaled exporter-specific configuration.

func (*ReceiverConfig) SetExporterConfig

func (cfg *ReceiverConfig) SetExporterConfig(exporterCfg Config)

SetExporterConfig sets the unmarshaled exporter-specific configuration.

func (*ReceiverConfig) Validate

func (cfg *ReceiverConfig) Validate() error

Validate checks if the ReceiverConfig is valid.

Jump to

Keyboard shortcuts

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