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 new telemetry related config to the config file #4061

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 18 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,16 @@ type Service struct {

// ServiceTelemetry defines the configurable settings for service telemetry.
type ServiceTelemetry struct {
Logs ServiceTelemetryLogs
Logs ServiceTelemetryLogs
Metrics ServiceTelemetryMetrics
}

func (srvT *ServiceTelemetry) validate() error {
return srvT.Logs.validate()
err := srvT.Logs.validate()
if err != nil {
return err
}
return srvT.Metrics.validate()
}

// ServiceTelemetryLogs defines the configurable settings for service telemetry logs.
Expand All @@ -184,13 +189,24 @@ type ServiceTelemetryLogs struct {
Encoding string
}

// ServiceTelemetryMetrics defines the configurable settings for service telemetry metrics.
// the collector uses mapstructure and not yaml tags.
type ServiceTelemetryMetrics struct {
// Add the collector version tag to the collector's telemetry metrics
AddVersionTag bool
}

func (srvTL *ServiceTelemetryLogs) validate() error {
if srvTL.Encoding != "json" && srvTL.Encoding != "console" {
return fmt.Errorf(`service telemetry logs invalid encoding: %q, valid values are "json" and "console"`, srvTL.Encoding)
}
return nil
}

func (srvTL *ServiceTelemetryMetrics) validate() error {
return nil
}

// Type is the component type as it is used in the config.
type Type string

Expand Down
3 changes: 2 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ func generateConfig() *Config {
},
},
Service: Service{
Telemetry: ServiceTelemetry{Logs: ServiceTelemetryLogs{Level: zapcore.DebugLevel, Development: true, Encoding: "console"}},
Telemetry: ServiceTelemetry{Logs: ServiceTelemetryLogs{Level: zapcore.DebugLevel, Development: true, Encoding: "console"},
Metrics: ServiceTelemetryMetrics{AddVersionTag: true}},
Extensions: []ComponentID{NewID("nop")},
Pipelines: map[string]*Pipeline{
"traces": {
Expand Down
14 changes: 13 additions & 1 deletion config/configunmarshaler/defaultunmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type serviceSettings struct {
}

type serviceTelemetrySettings struct {
Logs serviceTelemetryLogsSettings `mapstructure:"logs"`
Logs serviceTelemetryLogsSettings `mapstructure:"logs"`
Metric serviceTelemetryMetricsSettings `mapstructure:"metrics"`
}

type serviceTelemetryLogsSettings struct {
Expand All @@ -95,6 +96,10 @@ type serviceTelemetryLogsSettings struct {
Encoding string `mapstructure:"encoding"`
}

type serviceTelemetryMetricsSettings struct {
AddVersionTag bool `mapstructure:"add_version_tag"`
}

type pipelineSettings struct {
Receivers []string `mapstructure:"receivers"`
Processors []string `mapstructure:"processors"`
Expand Down Expand Up @@ -127,6 +132,9 @@ func (*defaultUnmarshaler) Unmarshal(v *configparser.ConfigMap, factories compon
Development: false,
Encoding: "console",
},
Metric: serviceTelemetryMetricsSettings{
AddVersionTag: true,
},
},
},
}
Expand Down Expand Up @@ -262,6 +270,10 @@ func unmarshalService(rawService serviceSettings) (config.Service, error) {
Encoding: rawService.Telemetry.Logs.Encoding,
}

ret.Telemetry.Metrics = config.ServiceTelemetryMetrics{
AddVersionTag: rawService.Telemetry.Metric.AddVersionTag,
}

ret.Extensions = make([]config.ComponentID, 0, len(rawService.Extensions))
for _, extIDStr := range rawService.Extensions {
id, err := config.NewIDFromString(extIDStr)
Expand Down
3 changes: 2 additions & 1 deletion config/configunmarshaler/defaultunmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func TestDecodeConfig(t *testing.T) {
"Did not load processor config correctly")

// Verify Service Telemetry
assert.Equal(t, config.ServiceTelemetry{Logs: config.ServiceTelemetryLogs{Level: zapcore.DebugLevel, Development: true, Encoding: "console"}}, cfg.Service.Telemetry)
assert.Equal(t, config.ServiceTelemetry{Logs: config.ServiceTelemetryLogs{Level: zapcore.DebugLevel, Development: true, Encoding: "console"},
Metrics: config.ServiceTelemetryMetrics{AddVersionTag: true}}, cfg.Service.Telemetry)

// Verify Service Extensions
assert.Equal(t, 2, len(cfg.Service.Extensions))
Expand Down
2 changes: 2 additions & 0 deletions config/configunmarshaler/testdata/valid-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ service:
level: "DEBUG"
development: true
encoding: "console"
metrics:
add_version_tag: true
extensions: [exampleextension/0, exampleextension/1]
pipelines:
traces:
Expand Down
2 changes: 1 addition & 1 deletion service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (col *Collector) execute(ctx context.Context) error {
return err
}

if err = collectorTelemetry.init(col.asyncErrorChannel, getBallastSize(col.service), col.logger); err != nil {
if err = collectorTelemetry.init(col.asyncErrorChannel, getBallastSize(col.service), col.logger, col.service.config.Telemetry); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/config/configunmarshaler"
"go.opentelemetry.io/collector/internal/testutil"
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestCollector_Start(t *testing.T) {

type mockColTelemetry struct{}

func (tel *mockColTelemetry) init(chan<- error, uint64, *zap.Logger) error {
func (tel *mockColTelemetry) init(chan<- error, uint64, *zap.Logger, config.ServiceTelemetry) error {
return nil
}

Expand Down
19 changes: 12 additions & 7 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"go.opencensus.io/stats/view"
"go.uber.org/zap"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/collector/telemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/version"
semconv "go.opentelemetry.io/collector/model/semconv/v1.5.0"
"go.opentelemetry.io/collector/processor/batchprocessor"
telemetry2 "go.opentelemetry.io/collector/service/internal/telemetry"
Expand All @@ -38,7 +40,7 @@ import (
var collectorTelemetry collectorTelemetryExporter = &colTelemetry{}

type collectorTelemetryExporter interface {
init(asyncErrorChannel chan<- error, ballastSizeBytes uint64, logger *zap.Logger) error
init(asyncErrorChannel chan<- error, ballastSizeBytes uint64, logger *zap.Logger, serviceTelemetry config.ServiceTelemetry) error
shutdown() error
}

Expand All @@ -48,11 +50,11 @@ type colTelemetry struct {
doInitOnce sync.Once
}

func (tel *colTelemetry) init(asyncErrorChannel chan<- error, ballastSizeBytes uint64, logger *zap.Logger) error {
func (tel *colTelemetry) init(asyncErrorChannel chan<- error, ballastSizeBytes uint64, logger *zap.Logger, serviceTelemetry config.ServiceTelemetry) error {
var err error
tel.doInitOnce.Do(
func() {
err = tel.initOnce(asyncErrorChannel, ballastSizeBytes, logger)
err = tel.initOnce(asyncErrorChannel, ballastSizeBytes, logger, serviceTelemetry)
},
)
if err != nil {
Expand All @@ -61,7 +63,7 @@ func (tel *colTelemetry) init(asyncErrorChannel chan<- error, ballastSizeBytes u
return nil
}

func (tel *colTelemetry) initOnce(asyncErrorChannel chan<- error, ballastSizeBytes uint64, logger *zap.Logger) error {
func (tel *colTelemetry) initOnce(asyncErrorChannel chan<- error, ballastSizeBytes uint64, logger *zap.Logger, serviceTelemetry config.ServiceTelemetry) error {
logger.Info("Setting up own telemetry...")

level := configtelemetry.GetMetricsLevelFlagValue()
Expand Down Expand Up @@ -93,14 +95,17 @@ func (tel *colTelemetry) initOnce(asyncErrorChannel chan<- error, ballastSizeByt
opts := prometheus.Options{
Namespace: telemetry.GetMetricsPrefix(),
}
opts.ConstLabels = make(map[string]string)

var instanceID string
if telemetry.GetAddInstanceID() {
instanceUUID, _ := uuid.NewRandom()
instanceID = instanceUUID.String()
opts.ConstLabels = map[string]string{
sanitizePrometheusKey(semconv.AttributeServiceInstanceID): instanceID,
}
opts.ConstLabels[sanitizePrometheusKey(semconv.AttributeServiceInstanceID)] = instanceID
}

if serviceTelemetry.Metrics.AddVersionTag {
opts.ConstLabels[sanitizePrometheusKey(semconv.AttributeServiceVersion)] = version.Version
}

pe, err := prometheus.NewExporter(opts)
Expand Down