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 1 commit
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]
9 changes: 4 additions & 5 deletions exporter/bmchelixexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ package bmchelixexporter // import "github.com/open-telemetry/opentelemetry-coll

import (
"errors"
"time"

"go.opentelemetry.io/collector/config/confighttp"
"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 string `mapstructure:"api_key"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to use configopaque.String avoid leaking the key within logs or other marshalling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea, thank you!

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
}
84 changes: 84 additions & 0 deletions exporter/bmchelixexporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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"
"os"

"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 {
be.logger.Info("Building BMC Helix payload")
helixMetrics, err := be.metricsProducer.ProduceHelixPayload(md)
if err != nil {
be.logger.Error("Failed to build BMC Helix payload", zap.Error(err))
return err
}

be.logger.Info("Sending BMC Helix payload")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it common practice to log all this? I would have expected such messages ("Building", "Sending", etc.) to be in debug level, rather than Info. (but I'm not familiar with the logging in other components!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove these two logs. I don't see what value they add, and after reviewing a few exporters, I didn't notice a similar practice to what I implemented here...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be a noisey log and potentially provide little value, can you remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will remove it.

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")

// Get the hostname reported by the kernel
osHostname, err := os.Hostname()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on this starts up, it could fetch the container hostname which is just a hash, is that going to be an issue?

Copy link
Contributor Author

@NassimBtk NassimBtk Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good question! This shouldn't cause issues for the user, as they should be able to configure the hostname of their container or pod. If that's not feasible, indeed, the hash will change with each new deployment. In such cases, I would recommend using OTTL to override the host.name on either the resource or the metric itself, though this approach could become cumbersome in scenarios where the collector processes a large volume of metrics. Alternatively, we could introduce an entity_hostname configuration option to override the OS hostname. Currently, the code defaults to the OS hostname only if it cannot find a host.name attribute on the datapoint or the associated resource. What do you think, @bertysentry?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch. I recommend we don't use os.Hostname() as the default entity hostname in BMC Helix. Instead, if the user wants to use the local host details, we will recommend to simply use the resourcedetectionprocessor in their otelcol config.

And if metrics don't have a host.name (at the end of the processors chain), then such metrics won't be exported to BMC Helix, and it's okay. Maybe we should log the number of metrics that are dropped so the end user knows what is going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, os.Hostname() will not be used in that case.

if err != nil {
be.logger.Error("Failed to get OS hostname", zap.Error(err))
return err
}

// Initialize and store the MetricsProducer
be.metricsProducer = newMetricsProducer(osHostname, 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),
)
}
21 changes: 18 additions & 3 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.117.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/component/componenttest v0.117.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/config/confighttp v0.117.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/config/configretry v1.23.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/confmap v1.23.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/exporter v0.117.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/exporter/exportertest v0.117.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/pdata v1.23.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/semconv v0.117.1-0.20250119231113-f07ebc3afb51
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/collector/client v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/config/configauth v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/config/configcompression v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/config/configopaque v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/config/configtls v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/consumer v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/consumer/consumererror v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/consumer/consumertest v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/consumer/xconsumer v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/exporter/xexporter v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/extension v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/extension/auth v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/extension/xextension v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/featuregate v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/pipeline v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/receiver v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/receiver/receivertest v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/collector/receiver/xreceiver v0.117.1-0.20250119231113-f07ebc3afb51 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.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/sys v0.28.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
google.golang.org/grpc v1.69.4 // indirect
Expand Down
Loading
Loading