Skip to content

Commit

Permalink
Move telemetry flags to the same file as config flags (open-telemetry…
Browse files Browse the repository at this point in the history
…#4154)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Oct 1, 2021
1 parent d193b03 commit 506090b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 81 deletions.
73 changes: 0 additions & 73 deletions internal/collector/telemetry/telemetry.go

This file was deleted.

44 changes: 41 additions & 3 deletions service/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ import (
"strings"

"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/collector/telemetry"
"go.opentelemetry.io/collector/service/internal/telemetrylogs"
)

var (
defaultConfig = ""
defaultMetricsAddr = ":8888"
defaultMetricsPrefix = "otelcol"
defaultAddInstanceID = true
defaultConfig = ""

// Command-line flag that control the configuration file.
configFlag = &defaultConfig
setFlag = new(stringArrayValue)
// Command-line flags that control publication of telemetry data.
metricsAddrPtr = &defaultMetricsAddr
metricsPrefixPtr = &defaultMetricsPrefix
addInstanceIDPtr = &defaultAddInstanceID
)

type stringArrayValue struct {
Expand All @@ -47,17 +54,36 @@ func flags() *flag.FlagSet {
flagSet := new(flag.FlagSet)
addFlagsFns := []func(*flag.FlagSet){
configtelemetry.Flags,
telemetry.Flags,
telemetrylogs.Flags,
}
for _, addFlags := range addFlagsFns {
addFlags(flagSet)
}

// At least until we can use a generic, i.e.: OpenCensus, metrics exporter
// we default to Prometheus at port 8888, if not otherwise specified.
metricsAddrPtr = flagSet.String(
"metrics-addr",
defaultMetricsAddr,
"[address]:port for exposing collector telemetry.")

metricsPrefixPtr = flagSet.String(
"metrics-prefix",
defaultMetricsPrefix,
"Prefix to the metrics generated by the collector.")

addInstanceIDPtr = flagSet.Bool(
"add-instance-id",
defaultAddInstanceID,
"Flag to control the addition of 'service.instance.id' to the collector metrics.")

configFlag = flagSet.String("config", defaultConfig, "Path to the config file")

flagSet.Var(setFlag, "set",
"Set arbitrary component config property. The component has to be defined in the config file and the flag"+
" has a higher precedence. Array config properties are overridden and maps are joined, note that only a single"+
" (first) array property can be set e.g. -set=processors.attributes.actions.key=some_key. Example --set=processors.batch.timeout=2s")

return flagSet
}

Expand All @@ -68,3 +94,15 @@ func getConfigFlag() string {
func getSetFlag() []string {
return setFlag.values
}

func getAddInstanceID() bool {
return *addInstanceIDPtr
}

func getMetricsAddr() string {
return *metricsAddrPtr
}

func getMetricsPrefix() string {
return *metricsPrefixPtr
}
9 changes: 4 additions & 5 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"go.uber.org/zap"

"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"
Expand Down Expand Up @@ -76,15 +75,15 @@ func (tel *colTelemetry) initOnce(asyncErrorChannel chan<- error, ballastSizeByt
logger.Info("Setting up own telemetry...")

level := configtelemetry.GetMetricsLevelFlagValue()
metricsAddr := telemetry.GetMetricsAddr()
metricsAddr := getMetricsAddr()

if level == configtelemetry.LevelNone || metricsAddr == "" {
return nil
}

var instanceID string

if telemetry.GetAddInstanceID() {
if getAddInstanceID() {
instanceUUID, _ := uuid.NewRandom()
instanceID = instanceUUID.String()
}
Expand Down Expand Up @@ -151,12 +150,12 @@ func (tel *colTelemetry) initOpenCensus(level configtelemetry.Level, instanceID

// Until we can use a generic metrics exporter, default to Prometheus.
opts := prometheus.Options{
Namespace: telemetry.GetMetricsPrefix(),
Namespace: getMetricsPrefix(),
}

opts.ConstLabels = make(map[string]string)

if telemetry.GetAddInstanceID() {
if getAddInstanceID() {
opts.ConstLabels[sanitizePrometheusKey(semconv.AttributeServiceInstanceID)] = instanceID
}

Expand Down

0 comments on commit 506090b

Please sign in to comment.