Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/testexporter/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testexporter

import "fmt"

// Config holds the configuration for the test exporter.
type Config struct {
// ExporterName is a simple test config field
ExporterName string `mapstructure:"exporter_name"`
}

// Validate checks if the configuration is valid.
func (c *Config) Validate() error {
if c.ExporterName == "" {
return fmt.Errorf("exporter_name cannot be empty")
}
return nil
}
79 changes: 79 additions & 0 deletions examples/testexporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testexporter

import (
"context"
"fmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/exporter-toolkit/otlpreceiver"
)

// Exporter is a minimal test exporter that exports a few test gauge metrics.
type Exporter struct {
config *Config
registry *prometheus.Registry
testGauge1 prometheus.Gauge
testGauge2 prometheus.Gauge
}

// NewExporter creates a new test exporter instance.
func NewExporter(config *Config) *Exporter {
return &Exporter{
config: config,
registry: prometheus.NewRegistry(),
}
}

// Initialize sets up the exporter and returns its registry.
func (e *Exporter) Initialize(ctx context.Context, cfg otlpreceiver.Config) (*prometheus.Registry, error) {
exporterCfg, ok := cfg.(*Config)
if !ok {
return nil, fmt.Errorf("invalid config type: expected *Config, got %T", cfg)
}

e.config = exporterCfg

fmt.Printf("Test exporter initialized with name: %s\n", e.config.ExporterName)

// Create test gauge metrics
e.testGauge1 = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "test_exporter_gauge_1",
Help: "First test gauge metric",
})

e.testGauge2 = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "test_exporter_gauge_2",
Help: "Second test gauge metric with labels",
}, []string{"label1", "label2"}).WithLabelValues("value1", "value2")

// Register metrics
e.registry.MustRegister(e.testGauge1)
e.registry.MustRegister(e.testGauge2.(prometheus.Collector))

// Set initial values
e.testGauge1.Set(42.0)
e.testGauge2.Set(123.45)

fmt.Printf("Test exporter registered %d gauge metrics\n", 2)

return e.registry, nil
}

// Shutdown cleanly stops the exporter.
func (e *Exporter) Shutdown(ctx context.Context) error {
fmt.Printf("Test exporter shutting down: %s\n", e.config.ExporterName)
return nil
}
32 changes: 32 additions & 0 deletions examples/testexporter/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testexporter

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver"

"github.com/prometheus/exporter-toolkit/otlpreceiver"
)

// NewFactory creates a factory for the test exporter receiver.
func NewFactory() receiver.Factory {
receiverType := component.MustNewType("prometheus_testexporter")

return otlpreceiver.NewFactory(
otlpreceiver.WithType(receiverType),
otlpreceiver.WithInitializer(NewExporter(&Config{})),
otlpreceiver.WithConfigUnmarshaler(&ConfigUnmarshaler{}),
)
}
48 changes: 48 additions & 0 deletions examples/testexporter/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module github.com/prometheus/exporter-toolkit/examples/testexporter

go 1.24.0

require (
github.com/prometheus/client_golang v1.23.2
github.com/prometheus/exporter-toolkit v0.0.0
go.opentelemetry.io/collector/component v1.44.0
go.opentelemetry.io/collector/receiver v1.44.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.66.1 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/consumer v1.44.0 // indirect
go.opentelemetry.io/collector/featuregate v1.44.0 // indirect
go.opentelemetry.io/collector/internal/telemetry v0.138.0 // indirect
go.opentelemetry.io/collector/pdata v1.44.0 // indirect
go.opentelemetry.io/collector/pipeline v1.44.0 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.13.0 // indirect
go.opentelemetry.io/otel v1.38.0 // indirect
go.opentelemetry.io/otel/log v0.14.0 // indirect
go.opentelemetry.io/otel/metric v1.38.0 // indirect
go.opentelemetry.io/otel/trace v1.38.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/net v0.45.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.30.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250804133106-a7a43d27e69b // indirect
google.golang.org/grpc v1.76.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect
)

replace github.com/prometheus/exporter-toolkit => ../..
Loading