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

adding otel attributes to config_telemetry #15084

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions core/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func initGlobals(cfgProm config.Prometheus, cfgTracing config.Tracing, cfgTeleme
for k, v := range cfgTelemetry.ResourceAttributes() {
attributes = append(attributes, attribute.String(k, v))
}

clientCfg := beholder.Config{
InsecureConnection: cfgTelemetry.InsecureConnection(),
CACertFile: cfgTelemetry.CACertFile(),
Expand Down
19 changes: 18 additions & 1 deletion core/services/chainlink/config_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chainlink

import (
"github.com/smartcontractkit/chainlink/v2/core/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/static"
)

type telemetryConfig struct {
Expand Down Expand Up @@ -31,8 +32,24 @@ func (b *telemetryConfig) OtelExporterGRPCEndpoint() string {
return *b.s.Endpoint
}

// ResourceAttributes returns the resource attributes set in the TOML config
// by the user, but first sets OTEL required attributes:
//
// service.name
// service.version
//
// These can be overridden by the TOML if the user so chooses
func (b *telemetryConfig) ResourceAttributes() map[string]string {
return b.s.ResourceAttributes
defaults := map[string]string{
"service.name": "chainlink",
"service.version": static.Version,
}

for k, v := range b.s.ResourceAttributes {
defaults[k] = v
}

return defaults
}

func (b *telemetryConfig) TraceSampleRatio() float64 {
Expand Down
143 changes: 143 additions & 0 deletions core/services/chainlink/config_telemetry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package chainlink

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/chainlink/v2/core/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/static"
)

func TestTelemetryConfig_Enabled(t *testing.T) {
trueVal := true
falseVal := false

tests := []struct {
name string
telemetry toml.Telemetry
expected bool
}{
{"EnabledTrue", toml.Telemetry{Enabled: &trueVal}, true},
{"EnabledFalse", toml.Telemetry{Enabled: &falseVal}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := telemetryConfig{s: tt.telemetry}
assert.Equal(t, tt.expected, tc.Enabled())
})
}
}

func TestTelemetryConfig_InsecureConnection(t *testing.T) {
trueVal := true
falseVal := false

tests := []struct {
name string
telemetry toml.Telemetry
expected bool
}{
{"InsecureConnectionTrue", toml.Telemetry{InsecureConnection: &trueVal}, true},
{"InsecureConnectionFalse", toml.Telemetry{InsecureConnection: &falseVal}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := telemetryConfig{s: tt.telemetry}
assert.Equal(t, tt.expected, tc.InsecureConnection())
})
}
}

func TestTelemetryConfig_CACertFile(t *testing.T) {
tests := []struct {
name string
telemetry toml.Telemetry
expected string
}{
{"CACertFileSet", toml.Telemetry{CACertFile: ptr("test.pem")}, "test.pem"},
{"CACertFileNil", toml.Telemetry{CACertFile: nil}, ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := telemetryConfig{s: tt.telemetry}
assert.Equal(t, tt.expected, tc.CACertFile())
})
}
}

func TestTelemetryConfig_OtelExporterGRPCEndpoint(t *testing.T) {
tests := []struct {
name string
telemetry toml.Telemetry
expected string
}{
{"EndpointSet", toml.Telemetry{Endpoint: ptr("localhost:4317")}, "localhost:4317"},
{"EndpointNil", toml.Telemetry{Endpoint: nil}, ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := telemetryConfig{s: tt.telemetry}
assert.Equal(t, tt.expected, tc.OtelExporterGRPCEndpoint())
})
}
}

func TestTelemetryConfig_ResourceAttributes(t *testing.T) {
tests := []struct {
name string
telemetry toml.Telemetry
expected map[string]string
}{
{
"DefaultAttributes",
toml.Telemetry{ResourceAttributes: nil},
map[string]string{
"service.name": "chainlink",
"service.version": static.Version,
},
},
{
"CustomAttributes",
toml.Telemetry{ResourceAttributes: map[string]string{"custom.key": "custom.value"}},
map[string]string{
"service.name": "chainlink",
"service.version": static.Version,
"custom.key": "custom.value",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := telemetryConfig{s: tt.telemetry}
assert.Equal(t, tt.expected, tc.ResourceAttributes())
})
}
}

func TestTelemetryConfig_TraceSampleRatio(t *testing.T) {
tests := []struct {
name string
telemetry toml.Telemetry
expected float64
}{
{"TraceSampleRatioSet", toml.Telemetry{TraceSampleRatio: ptrFloat(0.5)}, 0.5},
{"TraceSampleRatioNil", toml.Telemetry{TraceSampleRatio: nil}, 0.0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := telemetryConfig{s: tt.telemetry}
assert.Equal(t, tt.expected, tc.TraceSampleRatio())

Check failure on line 136 in core/services/chainlink/config_telemetry_test.go

View workflow job for this annotation

GitHub Actions / lint

float-compare: use assert.InEpsilon (or InDelta) (testifylint)
})
}
}

func ptrFloat(f float64) *float64 {
return &f
}
Loading