Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/bmchelix] Second PR of New component: BMC Helix Exporter #37350

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .chloggen/bmchelixexporter-metrics-implementation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: new_component

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: bmchelixexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: metrics implementation

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36773]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
10 changes: 5 additions & 5 deletions exporter/bmchelixexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ package bmchelixexporter // import "github.com/open-telemetry/opentelemetry-coll

import (
"errors"
"time"

"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
)

// Config struct is used to store the configuration of the exporter
type Config struct {
Endpoint string `mapstructure:"endpoint"`
APIKey string `mapstructure:"api_key"`
Timeout time.Duration `mapstructure:"timeout"`
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"`
confighttp.ClientConfig `mapstructure:",squash"`
APIKey configopaque.String `mapstructure:"api_key"`
RetryConfig configretry.BackOffConfig `mapstructure:"retry_on_failure"`
}

// validate the configuration
Expand Down
38 changes: 21 additions & 17 deletions exporter/bmchelixexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/confmap/confmaptest"

Expand All @@ -31,18 +32,16 @@ func TestLoadConfig(t *testing.T) {
{
id: component.NewIDWithName(metadata.Type, "helix1"),
expected: &Config{
Endpoint: "https://helix1:8080",
APIKey: "api_key",
Timeout: 10 * time.Second,
RetryConfig: configretry.NewDefaultBackOffConfig(),
ClientConfig: createDefaultClientConfig("https://helix1:8080", 10*time.Second),
APIKey: "api_key",
RetryConfig: configretry.NewDefaultBackOffConfig(),
},
},
{
id: component.NewIDWithName(metadata.Type, "helix2"),
expected: &Config{
Endpoint: "https://helix2:8080",
APIKey: "api_key",
Timeout: 20 * time.Second,
ClientConfig: createDefaultClientConfig("https://helix2:8080", 20*time.Second),
APIKey: "api_key",
RetryConfig: configretry.BackOffConfig{
Enabled: true,
InitialInterval: 5 * time.Second,
Expand Down Expand Up @@ -79,9 +78,8 @@ func TestValidateConfig(t *testing.T) {
{
name: "valid_config",
config: &Config{
Endpoint: "https://helix:8080",
APIKey: "api_key",
Timeout: 10 * time.Second,
ClientConfig: createDefaultClientConfig("https://helix:8080", 10*time.Second),
APIKey: "api_key",
},
},
{
Expand All @@ -94,25 +92,23 @@ func TestValidateConfig(t *testing.T) {
{
name: "invalid_config2",
config: &Config{
Endpoint: "https://helix:8080",
ClientConfig: createDefaultClientConfig("https://helix:8080", 10*time.Second),
},
err: "api key is required",
},
{
name: "invalid_config3",
config: &Config{
Endpoint: "https://helix:8080",
APIKey: "api_key",
Timeout: -1,
ClientConfig: createDefaultClientConfig("https://helix:8080", -1),
APIKey: "api_key",
},
err: "timeout must be a positive integer",
},
{
name: "invalid_config4",
config: &Config{
Endpoint: "https://helix:8080",
APIKey: "api_key",
Timeout: 0,
ClientConfig: createDefaultClientConfig("https://helix:8080", 0),
APIKey: "api_key",
},
err: "timeout must be a positive integer",
},
Expand All @@ -130,3 +126,11 @@ func TestValidateConfig(t *testing.T) {
})
}
}

// createDefaultClientConfig creates a default client config for testing
func createDefaultClientConfig(endpoint string, timeout time.Duration) confighttp.ClientConfig {
cfg := confighttp.NewDefaultClientConfig()
cfg.Endpoint = endpoint
cfg.Timeout = timeout
return cfg
}
74 changes: 74 additions & 0 deletions exporter/bmchelixexporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package bmchelixexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/bmchelixexporter"

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)

// BmcHelixExporter is responsible for exporting metrics to BMC Helix
type BmcHelixExporter struct {
config *Config
logger *zap.Logger
version string
telemetrySettings component.TelemetrySettings
metricsProducer *MetricsProducer
metricsClient *MetricsClient
}

// newBmcHelixExporter instantiates a new BMC Helix Exporter
func newBmcHelixExporter(config *Config, createSettings exporter.Settings) (*BmcHelixExporter, error) {
if config == nil {
return nil, errors.New("nil config")
}

return &BmcHelixExporter{
config: config,
version: createSettings.BuildInfo.Version,
logger: createSettings.Logger,
telemetrySettings: createSettings.TelemetrySettings,
}, nil
}

// pushMetrics is invoked by the OpenTelemetry Collector to push metrics to BMC Helix
func (be *BmcHelixExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error {
helixMetrics, err := be.metricsProducer.ProduceHelixPayload(md)
if err != nil {
be.logger.Error("Failed to build BMC Helix payload", zap.Error(err))
return err
}

err = be.metricsClient.SendHelixPayload(ctx, helixMetrics)
if err != nil {
be.logger.Error("Failed to send BMC Helix payload", zap.Error(err))
return err
}

return nil
}

// start is invoked during service start
func (be *BmcHelixExporter) start(ctx context.Context, host component.Host) error {
be.logger.Info("Starting BMC Helix Exporter")

// Initialize and store the MetricsProducer
be.metricsProducer = newMetricsProducer(be.logger)

// Initialize and store the MetricsClient
metricsClient, err := newMetricsClient(ctx, be.config, host, be.telemetrySettings, be.logger)
if err != nil {
be.logger.Error("Failed to create MetricsClient", zap.Error(err))
return err
}
be.metricsClient = metricsClient

be.logger.Info("Initialized BMC Helix Exporter")
return nil
}
28 changes: 28 additions & 0 deletions exporter/bmchelixexporter/exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package bmchelixexporter

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/exporter/exportertest"
)

func TestNewBmcHelixExporterWithNilConfig(t *testing.T) {
t.Parallel()

exp, err := newBmcHelixExporter(nil, exportertest.NewNopSettings())
assert.Nil(t, exp)
assert.Error(t, err)
}

func TestNewBmcHelixExporterWithDefaultConfig(t *testing.T) {
t.Parallel()

cfg := createDefaultConfig().(*Config)
exp, err := newBmcHelixExporter(cfg, exportertest.NewNopSettings())
assert.NotNil(t, exp)
assert.NoError(t, err)
}
24 changes: 17 additions & 7 deletions exporter/bmchelixexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/bmchelixexporter/internal/metadata"
)
Expand All @@ -27,20 +27,30 @@ func NewFactory() exporter.Factory {

// creates the default configuration for the BMC Helix exporter
func createDefaultConfig() component.Config {
httpClientConfig := confighttp.NewDefaultClientConfig()
httpClientConfig.Timeout = 10 * time.Second

return &Config{
Timeout: 10 * time.Second,
RetryConfig: configretry.NewDefaultBackOffConfig(),
ClientConfig: httpClientConfig,
RetryConfig: configretry.NewDefaultBackOffConfig(),
}
}

// creates an exporter.Metrics that records observability metrics for BMC Helix
func createMetricsExporter(ctx context.Context, set exporter.Settings, cfg component.Config) (exporter.Metrics, error) {
config := cfg.(*Config)
exporter, err := newBmcHelixExporter(config, set)
if err != nil {
return nil, err
}

return exporterhelper.NewMetrics(
ctx,
set,
cfg,
func(_ context.Context, _ pmetric.Metrics) error {
return nil
},
config,
exporter.pushMetrics,
exporterhelper.WithTimeout(exporterhelper.TimeoutConfig{Timeout: 0}),
exporterhelper.WithRetry(config.RetryConfig),
exporterhelper.WithStart(exporter.start),
)
}
19 changes: 17 additions & 2 deletions exporter/bmchelixexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,70 @@ require (
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/component v0.118.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/component/componenttest v0.118.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/config/confighttp v0.118.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/config/configopaque v1.24.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/config/configretry v1.24.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/confmap v1.24.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/exporter v0.118.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/exporter/exportertest v0.118.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/pdata v1.24.1-0.20250123125445-24f88da7b583
go.opentelemetry.io/collector/semconv v0.118.1-0.20250123125445-24f88da7b583
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
)

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.11.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/client v1.24.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/config/configauth v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/config/configcompression v1.24.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/config/configtls v1.24.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/consumer v1.24.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/consumer/consumererror v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/consumer/consumertest v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/consumer/xconsumer v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/exporter/xexporter v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/extension v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/extension/auth v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/extension/xextension v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/featuregate v1.24.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/pipeline v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/receiver v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/receiver/receivertest v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/collector/receiver/xreceiver v0.118.1-0.20250123125445-24f88da7b583 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
Expand Down
Loading
Loading