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

[component] Add namespace parameter to component.BuildInfo #12508

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

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

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add namespace field to component.BuildInfo to support service.namespace semantic convention

# One or more tracking issues or pull requests related to the change
issues: [12505]

# (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:

# 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, api]
1 change: 1 addition & 0 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ConfResolver struct {
type Distribution struct {
Module string `mapstructure:"module"`
Name string `mapstructure:"name"`
Namespace string `mapstructure:"namespace"`
Go string `mapstructure:"go"`
Description string `mapstructure:"description"`
// Deprecated: [v0.113.0] only here to return a detailed error and not failing during unmarshalling.
Expand Down
11 changes: 11 additions & 0 deletions cmd/builder/internal/builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func TestNewDefaultConfig(t *testing.T) {
require.NoError(t, cfg.Validate())
assert.False(t, cfg.Distribution.DebugCompilation)
assert.Empty(t, cfg.Distribution.BuildTags)
assert.Empty(t, cfg.Distribution.Namespace)
assert.False(t, cfg.LDSet)
assert.Empty(t, cfg.LDFlags)
assert.False(t, cfg.GCSet)
Expand Down Expand Up @@ -364,6 +365,16 @@ func TestDebugOptionSetConfig(t *testing.T) {
assert.True(t, cfg.Distribution.DebugCompilation)
}

func TestNamespaceConfig(t *testing.T) {
cfg := Config{
Distribution: Distribution{
Namespace: "customNamespace",
},
}
require.NoError(t, cfg.Validate())
assert.Equal(t, "customNamespace", cfg.Distribution.Namespace)
}

func TestAddsDefaultProviders(t *testing.T) {
cfg, err := NewDefaultConfig()
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions cmd/builder/internal/builder/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
func main() {
info := component.BuildInfo{
Command: "{{ .Distribution.Name }}",
Namespace: "{{ .Distribution.Namespace }}",
Description: "{{ .Distribution.Description }}",
Version: "{{ .Distribution.Version }}",
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/builder/internal/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
dist:
module: go.opentelemetry.io/collector/cmd/otelcorecol
name: otelcorecol
namespace: opentelemetry-core
description: Local OpenTelemetry Collector binary, testing only.
version: 0.120.0-dev

Expand All @@ -35,4 +36,3 @@ providers:
- gomod: go.opentelemetry.io/collector/confmap/provider/httpprovider v1.26.0
- gomod: go.opentelemetry.io/collector/confmap/provider/httpsprovider v1.26.0
- gomod: go.opentelemetry.io/collector/confmap/provider/yamlprovider v1.26.0

1 change: 1 addition & 0 deletions cmd/otelcorecol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
dist:
module: go.opentelemetry.io/collector/cmd/otelcorecol
name: otelcorecol
namespace: opentelemetry-core
description: Local OpenTelemetry Collector binary, testing only.
version: 0.120.0-dev

Expand Down
1 change: 1 addition & 0 deletions cmd/otelcorecol/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added cmd/otelcorecol/otelcorecol
Binary file not shown.
4 changes: 4 additions & 0 deletions component/build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type BuildInfo struct {
// Command is the executable file name, e.g. "otelcol".
Command string

// Namespace is the namespace of the collector, e.g. "opentelemetry".
Namespace string

// Description is the full name of the collector, e.g. "OpenTelemetry Collector".
Description string

Expand All @@ -20,6 +23,7 @@ type BuildInfo struct {
func NewDefaultBuildInfo() BuildInfo {
return BuildInfo{
Command: "otelcol",
Namespace: "opentelemetry",
Description: "OpenTelemetry Collector",
Version: "latest",
}
Expand Down
18 changes: 18 additions & 0 deletions component/build_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package component

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewDefaultBuildInfo(t *testing.T) {
buildInfo := NewDefaultBuildInfo()
assert.Equal(t, "otelcol", buildInfo.Command)
assert.Equal(t, "opentelemetry", buildInfo.Namespace)
assert.Equal(t, "OpenTelemetry Collector", buildInfo.Description)
assert.Equal(t, "latest", buildInfo.Version)
}
2 changes: 1 addition & 1 deletion internal/e2e/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Test_ComponentStatusReporting_SharedInstance(t *testing.T) {
component.MustNewType("watcher"): newExtensionFactory(),
},
}
set.BuildInfo = component.BuildInfo{Version: "test version", Command: "otelcoltest"}
set.BuildInfo = component.BuildInfo{Version: "test version", Command: "otelcoltest", Namespace: "opentelemetry"}

cfg := service.Config{
Telemetry: telemetry.Config{
Expand Down
1 change: 1 addition & 0 deletions otelcol/testdata/components-output.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
buildinfo:
command: otelcol
namespace: opentelemetry
description: OpenTelemetry Collector
version: latest
receivers:
Expand Down
18 changes: 9 additions & 9 deletions service/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ func TestAttributes(t *testing.T) {
{
name: "no build info and no resource config",
cfg: telemetry.Config{},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: ""}, {Name: "service.version", Value: ""}, {Name: "service.instance.id", Value: ""}},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: ""}, {Name: "service.version", Value: ""}, {Name: "service.namespace", Value: ""}, {Name: "service.instance.id", Value: ""}},
},
{
name: "build info and no resource config",
cfg: telemetry.Config{},
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: "otelcoltest"}, {Name: "service.version", Value: "0.0.0-test"}, {Name: "service.instance.id", Value: ""}},
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test", Namespace: "opentelemetry"},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: "otelcoltest"}, {Name: "service.version", Value: "0.0.0-test"}, {Name: "service.namespace", Value: "opentelemetry"}, {Name: "service.instance.id", Value: ""}},
},
{
name: "no build info and resource config",
cfg: telemetry.Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: "resource.name"}, {Name: "service.version", Value: "resource.version"}, {Name: "test", Value: "test"}, {Name: "service.instance.id", Value: ""}},
cfg: telemetry.Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "service.namespace": ptr("resource.namespace"), "test": ptr("test")}},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: "resource.name"}, {Name: "service.version", Value: "resource.version"}, {Name: "service.namespace", Value: "resource.namespace"}, {Name: "test", Value: "test"}, {Name: "service.instance.id", Value: ""}},
},
{
name: "build info and resource config",
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"},
cfg: telemetry.Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: "resource.name"}, {Name: "service.version", Value: "resource.version"}, {Name: "test", Value: "test"}, {Name: "service.instance.id", Value: ""}},
cfg: telemetry.Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "service.namespace": ptr("resource.namespace"), "test": ptr("test")}},
wantAttributes: []config.AttributeNameValue{{Name: "service.name", Value: "resource.name"}, {Name: "service.version", Value: "resource.version"}, {Name: "service.namespace", Value: "resource.namespace"}, {Name: "test", Value: "test"}, {Name: "service.instance.id", Value: ""}},
},
{
name: "deleting a nil value",
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"},
cfg: telemetry.Config{Resource: map[string]*string{"service.name": nil, "service.version": ptr("resource.version"), "test": ptr("test")}},
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test", Namespace: "opentelemetry"},
cfg: telemetry.Config{Resource: map[string]*string{"service.name": nil, "service.version": ptr("resource.version"), "service.namespace": nil, "test": ptr("test")}},
wantAttributes: []config.AttributeNameValue{{Name: "service.version", Value: "resource.version"}, {Name: "test", Value: "test"}, {Name: "service.instance.id", Value: ""}},
},
}
Expand Down
5 changes: 5 additions & 0 deletions service/internal/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func New(buildInfo component.BuildInfo, resourceCfg map[string]*string) *resourc
telAttrs = append(telAttrs, attribute.String(semconv.AttributeServiceName, buildInfo.Command))
}

if _, ok := resourceCfg[semconv.AttributeServiceNamespace]; !ok {
// AttributeServiceNamespace is not specified in the config. Use the default namespace.
telAttrs = append(telAttrs, attribute.String(semconv.AttributeServiceNamespace, buildInfo.Namespace))
}

if _, ok := resourceCfg[semconv.AttributeServiceInstanceID]; !ok {
// AttributeServiceInstanceID is not specified in the config. Auto-generate one.
instanceUUID, _ := uuid.NewRandom()
Expand Down
19 changes: 15 additions & 4 deletions service/internal/resource/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const (
)

var buildInfo = component.BuildInfo{
Command: "otelcol",
Version: "1.0.0",
Command: "otelcol",
Version: "1.0.0",
Namespace: "opentelemetry",
}

func ptr[T any](v T) *T {
Expand All @@ -41,6 +42,7 @@ func TestNew(t *testing.T) {
want: map[string]string{
"service.name": "otelcol",
"service.version": "1.0.0",
"service.namespace": "opentelemetry",
"service.instance.id": randomUUIDSpecialValue,
},
},
Expand All @@ -49,11 +51,13 @@ func TestNew(t *testing.T) {
resourceCfg: map[string]*string{
"service.name": ptr("my-service"),
"service.version": ptr("1.2.3"),
"service.namespace": ptr("my-namespace"),
"service.instance.id": ptr("123"),
},
want: map[string]string{
"service.name": "my-service",
"service.version": "1.2.3",
"service.namespace": "my-namespace",
"service.instance.id": "123",
},
},
Expand All @@ -62,6 +66,7 @@ func TestNew(t *testing.T) {
resourceCfg: map[string]*string{
"service.name": nil,
"service.version": nil,
"service.namespace": nil,
"service.instance.id": nil,
},
want: map[string]string{},
Expand All @@ -74,6 +79,7 @@ func TestNew(t *testing.T) {
want: map[string]string{
"service.name": "otelcol",
"service.version": "1.0.0",
"service.namespace": "opentelemetry",
"service.instance.id": randomUUIDSpecialValue,
"host.name": "my-host",
},
Expand Down Expand Up @@ -124,7 +130,7 @@ func TestBuildResource(t *testing.T) {
otelRes := New(buildInfo, resMap)
res := pdataFromSdk(otelRes)

assert.Equal(t, 3, res.Attributes().Len())
assert.Equal(t, 4, res.Attributes().Len())
value, ok := res.Attributes().Get(semconv.AttributeServiceName)
assert.True(t, ok)
assert.Equal(t, buildInfo.Command, value.AsString())
Expand All @@ -140,6 +146,7 @@ func TestBuildResource(t *testing.T) {
semconv.AttributeServiceName: nil,
semconv.AttributeServiceVersion: nil,
semconv.AttributeServiceInstanceID: nil,
semconv.AttributeServiceNamespace: nil,
}
otelRes = New(buildInfo, resMap)
res = pdataFromSdk(otelRes)
Expand All @@ -153,11 +160,12 @@ func TestBuildResource(t *testing.T) {
semconv.AttributeServiceName: strPtr("a"),
semconv.AttributeServiceVersion: strPtr("b"),
semconv.AttributeServiceInstanceID: strPtr("c"),
semconv.AttributeServiceNamespace: strPtr("d"),
}
otelRes = New(buildInfo, resMap)
res = pdataFromSdk(otelRes)

assert.Equal(t, 3, res.Attributes().Len())
assert.Equal(t, 4, res.Attributes().Len())
value, ok = res.Attributes().Get(semconv.AttributeServiceName)
assert.True(t, ok)
assert.Equal(t, "a", value.AsString())
Expand All @@ -167,4 +175,7 @@ func TestBuildResource(t *testing.T) {
value, ok = res.Attributes().Get(semconv.AttributeServiceInstanceID)
assert.True(t, ok)
assert.Equal(t, "c", value.AsString())
value, ok = res.Attributes().Get(semconv.AttributeServiceNamespace)
assert.True(t, ok)
assert.Equal(t, "d", value.AsString())
}
Loading
Loading