update as per the PR comment

This commit is contained in:
Manendra Pal Singh
2025-12-02 23:00:28 +05:30
parent 8fd7afb54a
commit 045f29da13
11 changed files with 128 additions and 187 deletions

View File

@@ -22,8 +22,8 @@ func (m metricsProvider) New(ctx context.Context, config map[string]string) (*te
return nil, nil, errors.New("context cannot be nil")
}
// Convert map[string]string to telemetry.Config
telemetryConfig := &telemetry.Config{
// Convert map[string]string to otelsetup.Config
telemetryConfig := &otelsetup.Config{
ServiceName: config["serviceName"],
ServiceVersion: config["serviceVersion"],
Environment: config["environment"],
@@ -44,13 +44,13 @@ func (m metricsProvider) New(ctx context.Context, config map[string]string) (*te
// Apply defaults if fields are empty
if telemetryConfig.ServiceName == "" {
telemetryConfig.ServiceName = telemetry.DefaultConfig().ServiceName
telemetryConfig.ServiceName = otelsetup.DefaultConfig().ServiceName
}
if telemetryConfig.ServiceVersion == "" {
telemetryConfig.ServiceVersion = telemetry.DefaultConfig().ServiceVersion
telemetryConfig.ServiceVersion = otelsetup.DefaultConfig().ServiceVersion
}
if telemetryConfig.Environment == "" {
telemetryConfig.Environment = telemetry.DefaultConfig().Environment
telemetryConfig.Environment = otelsetup.DefaultConfig().Environment
}
log.Debugf(ctx, "Telemetry config mapped: %+v", telemetryConfig)

View File

@@ -4,7 +4,7 @@ import (
"context"
"testing"
"github.com/beckn-one/beckn-onix/pkg/telemetry"
"github.com/beckn-one/beckn-onix/pkg/plugin/implementation/otelsetup"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -18,8 +18,8 @@ func TestMetricsProviderNew_Success(t *testing.T) {
config map[string]string
}{
{
name: "Valid config with all fields",
ctx: context.Background(),
name: "Valid config with all fields",
ctx: context.Background(),
config: map[string]string{
"serviceName": "test-service",
"serviceVersion": "1.0.0",
@@ -33,15 +33,15 @@ func TestMetricsProviderNew_Success(t *testing.T) {
config: map[string]string{},
},
{
name: "Valid config with enableMetrics false",
ctx: context.Background(),
name: "Valid config with enableMetrics false",
ctx: context.Background(),
config: map[string]string{
"enableMetrics": "false",
},
},
{
name: "Valid config with partial fields",
ctx: context.Background(),
name: "Valid config with partial fields",
ctx: context.Background(),
config: map[string]string{
"serviceName": "custom-service",
"serviceVersion": "2.0.0",
@@ -105,7 +105,7 @@ func TestMetricsProviderNew_ConfigConversion(t *testing.T) {
tests := []struct {
name string
config map[string]string
expectedConfig *telemetry.Config
expectedConfig *otelsetup.Config
}{
{
name: "All fields provided",
@@ -115,7 +115,7 @@ func TestMetricsProviderNew_ConfigConversion(t *testing.T) {
"enableMetrics": "true",
"environment": "production",
},
expectedConfig: &telemetry.Config{
expectedConfig: &otelsetup.Config{
ServiceName: "my-service",
ServiceVersion: "3.0.0",
EnableMetrics: true,
@@ -125,11 +125,11 @@ func TestMetricsProviderNew_ConfigConversion(t *testing.T) {
{
name: "Empty config uses defaults",
config: map[string]string{},
expectedConfig: &telemetry.Config{
ServiceName: telemetry.DefaultConfig().ServiceName,
ServiceVersion: telemetry.DefaultConfig().ServiceVersion,
expectedConfig: &otelsetup.Config{
ServiceName: otelsetup.DefaultConfig().ServiceName,
ServiceVersion: otelsetup.DefaultConfig().ServiceVersion,
EnableMetrics: true, // Default when not specified
Environment: telemetry.DefaultConfig().Environment,
Environment: otelsetup.DefaultConfig().Environment,
},
},
{
@@ -137,11 +137,11 @@ func TestMetricsProviderNew_ConfigConversion(t *testing.T) {
config: map[string]string{
"enableMetrics": "false",
},
expectedConfig: &telemetry.Config{
ServiceName: telemetry.DefaultConfig().ServiceName,
ServiceVersion: telemetry.DefaultConfig().ServiceVersion,
expectedConfig: &otelsetup.Config{
ServiceName: otelsetup.DefaultConfig().ServiceName,
ServiceVersion: otelsetup.DefaultConfig().ServiceVersion,
EnableMetrics: false,
Environment: telemetry.DefaultConfig().Environment,
Environment: otelsetup.DefaultConfig().Environment,
},
},
{
@@ -149,11 +149,11 @@ func TestMetricsProviderNew_ConfigConversion(t *testing.T) {
config: map[string]string{
"enableMetrics": "invalid",
},
expectedConfig: &telemetry.Config{
ServiceName: telemetry.DefaultConfig().ServiceName,
ServiceVersion: telemetry.DefaultConfig().ServiceVersion,
expectedConfig: &otelsetup.Config{
ServiceName: otelsetup.DefaultConfig().ServiceName,
ServiceVersion: otelsetup.DefaultConfig().ServiceVersion,
EnableMetrics: true, // Defaults to true on parse error
Environment: telemetry.DefaultConfig().Environment,
Environment: otelsetup.DefaultConfig().Environment,
},
},
}
@@ -308,4 +308,3 @@ func TestMetricsProviderNew_DefaultValues(t *testing.T) {
assert.NoError(t, err, "cleanup() should not return error")
}
}

View File

@@ -22,8 +22,26 @@ import (
// behind the MetricsProvider interface.
type Setup struct{}
// ToPluginConfig converts telemetry.Config to plugin.Config format.
func ToPluginConfig(cfg *telemetry.Config) *plugin.Config {
// Config represents OpenTelemetry related configuration.
type Config struct {
ServiceName string `yaml:"serviceName"`
ServiceVersion string `yaml:"serviceVersion"`
EnableMetrics bool `yaml:"enableMetrics"`
Environment string `yaml:"environment"`
}
// DefaultConfig returns sensible defaults for telemetry configuration.
func DefaultConfig() *Config {
return &Config{
ServiceName: "beckn-onix",
ServiceVersion: "dev",
EnableMetrics: true,
Environment: "development",
}
}
// ToPluginConfig converts Config to plugin.Config format.
func ToPluginConfig(cfg *Config) *plugin.Config {
return &plugin.Config{
ID: "otelsetup",
Config: map[string]string{
@@ -38,20 +56,20 @@ func ToPluginConfig(cfg *telemetry.Config) *plugin.Config {
// New initializes the underlying telemetry provider. The returned provider
// exposes the HTTP handler and shutdown hooks that the core application can
// manage directly.
func (Setup) New(ctx context.Context, cfg *telemetry.Config) (*telemetry.Provider, error) {
func (Setup) New(ctx context.Context, cfg *Config) (*telemetry.Provider, error) {
if cfg == nil {
return nil, fmt.Errorf("telemetry config cannot be nil")
}
// Apply defaults if fields are empty
if cfg.ServiceName == "" {
cfg.ServiceName = telemetry.DefaultConfig().ServiceName
cfg.ServiceName = DefaultConfig().ServiceName
}
if cfg.ServiceVersion == "" {
cfg.ServiceVersion = telemetry.DefaultConfig().ServiceVersion
cfg.ServiceVersion = DefaultConfig().ServiceVersion
}
if cfg.Environment == "" {
cfg.Environment = telemetry.DefaultConfig().Environment
cfg.Environment = DefaultConfig().Environment
}
if !cfg.EnableMetrics {

View File

@@ -6,7 +6,6 @@ import (
"net/http/httptest"
"testing"
"github.com/beckn-one/beckn-onix/pkg/telemetry"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -17,11 +16,11 @@ func TestSetup_New_Success(t *testing.T) {
tests := []struct {
name string
cfg *telemetry.Config
cfg *Config
}{
{
name: "Valid config with all fields",
cfg: &telemetry.Config{
cfg: &Config{
ServiceName: "test-service",
ServiceVersion: "1.0.0",
EnableMetrics: true,
@@ -30,7 +29,7 @@ func TestSetup_New_Success(t *testing.T) {
},
{
name: "Valid config with metrics disabled",
cfg: &telemetry.Config{
cfg: &Config{
ServiceName: "test-service",
ServiceVersion: "1.0.0",
EnableMetrics: false,
@@ -39,7 +38,7 @@ func TestSetup_New_Success(t *testing.T) {
},
{
name: "Config with empty fields uses defaults",
cfg: &telemetry.Config{
cfg: &Config{
ServiceName: "",
ServiceVersion: "",
EnableMetrics: true,
@@ -82,7 +81,7 @@ func TestSetup_New_Failure(t *testing.T) {
tests := []struct {
name string
cfg *telemetry.Config
cfg *Config
wantErr bool
}{
{
@@ -112,7 +111,7 @@ func TestSetup_New_DefaultValues(t *testing.T) {
ctx := context.Background()
// Test with empty fields - should use defaults
cfg := &telemetry.Config{
cfg := &Config{
ServiceName: "",
ServiceVersion: "",
EnableMetrics: true,
@@ -142,7 +141,7 @@ func TestSetup_New_MetricsDisabled(t *testing.T) {
setup := Setup{}
ctx := context.Background()
cfg := &telemetry.Config{
cfg := &Config{
ServiceName: "test-service",
ServiceVersion: "1.0.0",
EnableMetrics: false,
@@ -165,13 +164,13 @@ func TestSetup_New_MetricsDisabled(t *testing.T) {
func TestToPluginConfig_Success(t *testing.T) {
tests := []struct {
name string
cfg *telemetry.Config
cfg *Config
expectedID string
expectedConfig map[string]string
}{
{
name: "Valid config with all fields",
cfg: &telemetry.Config{
cfg: &Config{
ServiceName: "test-service",
ServiceVersion: "1.0.0",
EnableMetrics: true,
@@ -187,7 +186,7 @@ func TestToPluginConfig_Success(t *testing.T) {
},
{
name: "Config with enableMetrics false",
cfg: &telemetry.Config{
cfg: &Config{
ServiceName: "my-service",
ServiceVersion: "2.0.0",
EnableMetrics: false,
@@ -203,7 +202,7 @@ func TestToPluginConfig_Success(t *testing.T) {
},
{
name: "Config with empty fields",
cfg: &telemetry.Config{
cfg: &Config{
ServiceName: "",
ServiceVersion: "",
EnableMetrics: true,
@@ -259,7 +258,7 @@ func TestToPluginConfig_BooleanConversion(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &telemetry.Config{
cfg := &Config{
ServiceName: "test",
ServiceVersion: "1.0.0",
EnableMetrics: tt.enableMetrics,