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

f5cloudexporter: Implemented User-Agent header with version #2292

Merged
merged 2 commits into from
Feb 9, 2021
Merged
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
4 changes: 3 additions & 1 deletion exporter/f5cloudexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func TestLoadConfig(t *testing.T) {
ReadBufferSize: 123,
WriteBufferSize: 345,
Timeout: time.Second * 10,
Headers: map[string]string{},
Headers: map[string]string{
"User-Agent": "opentelemetry-collector-contrib {{version}}",
},
},
},
Source: "dev",
Expand Down
13 changes: 13 additions & 0 deletions exporter/f5cloudexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package f5cloudexporter
import (
"context"
"net/http"
"strings"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
Expand Down Expand Up @@ -58,6 +59,8 @@ func (f *f5cloudFactory) CreateMetricsExporter(
return nil, err
}

fillUserAgent(cfg, params.ApplicationStartInfo.Version)

return f.ExporterFactory.CreateMetricsExporter(ctx, params, &cfg.Config)
}

Expand All @@ -72,6 +75,8 @@ func (f *f5cloudFactory) CreateTracesExporter(
return nil, err
}

fillUserAgent(cfg, params.ApplicationStartInfo.Version)

return f.ExporterFactory.CreateTracesExporter(ctx, params, &cfg.Config)
}

Expand All @@ -86,6 +91,8 @@ func (f *f5cloudFactory) CreateLogsExporter(
return nil, err
}

fillUserAgent(cfg, params.ApplicationStartInfo.Version)

return f.ExporterFactory.CreateLogsExporter(ctx, params, &cfg.Config)
}

Expand All @@ -101,6 +108,8 @@ func (f *f5cloudFactory) CreateDefaultConfig() configmodels.Exporter {
cfg.TypeVal = typeStr
cfg.NameVal = typeStr

cfg.Headers["User-Agent"] = "opentelemetry-collector-contrib {{version}}"

cfg.HTTPClientSettings.CustomRoundTripper = func(next http.RoundTripper) (http.RoundTripper, error) {
ts, err := f.getTokenSource(cfg)
if err != nil {
Expand All @@ -122,3 +131,7 @@ func getTokenSourceFromConfig(config *Config) (oauth2.TokenSource, error) {

return ts, nil
}

func fillUserAgent(cfg *Config, version string) {
cfg.Headers["User-Agent"] = strings.ReplaceAll(cfg.Headers["User-Agent"], "{{version}}", version)
}
27 changes: 24 additions & 3 deletions exporter/f5cloudexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ func TestFactory_CreateMetricsExporter(t *testing.T) {
Audience: "tests",
}

creationParams := component.ExporterCreateParams{Logger: zap.NewNop()}
creationParams := component.ExporterCreateParams{
Logger: zap.NewNop(),
ApplicationStartInfo: component.ApplicationStartInfo{
Version: "0.0.0",
},
}
oexp, err := factory.CreateMetricsExporter(context.Background(), creationParams, cfg)
require.Nil(t, err)
require.NotNil(t, oexp)

require.Equal(t, "opentelemetry-collector-contrib 0.0.0", cfg.Headers["User-Agent"])
}

func TestFactory_CreateMetricsExporterInvalidConfig(t *testing.T) {
Expand All @@ -86,10 +93,17 @@ func TestFactory_CreateTraceExporter(t *testing.T) {
Audience: "tests",
}

creationParams := component.ExporterCreateParams{Logger: zap.NewNop()}
creationParams := component.ExporterCreateParams{
Logger: zap.NewNop(),
ApplicationStartInfo: component.ApplicationStartInfo{
Version: "0.0.0",
},
}
oexp, err := factory.CreateTracesExporter(context.Background(), creationParams, cfg)
require.Nil(t, err)
require.NotNil(t, oexp)

require.Equal(t, "opentelemetry-collector-contrib 0.0.0", cfg.Headers["User-Agent"])
}

func Test_Factory_CreateTraceExporterInvalidConfig(t *testing.T) {
Expand All @@ -112,10 +126,17 @@ func TestFactory_CreateLogsExporter(t *testing.T) {
Audience: "tests",
}

creationParams := component.ExporterCreateParams{Logger: zap.NewNop()}
creationParams := component.ExporterCreateParams{
Logger: zap.NewNop(),
ApplicationStartInfo: component.ApplicationStartInfo{
Version: "0.0.0",
},
}
oexp, err := factory.CreateLogsExporter(context.Background(), creationParams, cfg)
require.Nil(t, err)
require.NotNil(t, oexp)

require.Equal(t, "opentelemetry-collector-contrib 0.0.0", cfg.Headers["User-Agent"])
}

func TestFactory_CreateLogsExporterInvalidConfig(t *testing.T) {
Expand Down