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

Support set flag and update log-level example usage #939

Merged
merged 1 commit into from
Nov 12, 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
29 changes: 28 additions & 1 deletion cmd/otelcol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package main

import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
Expand Down Expand Up @@ -113,6 +115,31 @@ func main() {
}
}

// required to support --set functionality no longer directly parsed by the core config loader.
// taken from https://github.com/open-telemetry/opentelemetry-collector/blob/48a2e01652fa679c89259866210473fc0d42ca95/service/flags.go#L39
type stringArrayValue struct {
mstumpfx marked this conversation as resolved.
Show resolved Hide resolved
values []string
}

func (s *stringArrayValue) Set(val string) error {
s.values = append(s.values, val)
return nil
}

func (s *stringArrayValue) String() string {
return "[" + strings.Join(s.values, ",") + "]"
}

func getSetProperties() []string {
properties := &stringArrayValue{}
flagSet := flag.NewFlagSet("", flag.ContinueOnError)
flagSet.SetOutput(io.Discard)
flagSet.Var(properties, "set", "")
// we are only interested in the --set option so ignore errors
_ = flagSet.Parse(os.Args[1:])
return properties.values
}

func hasFlag(flag string) bool {
return contains(os.Args[1:], flag)
}
Expand Down Expand Up @@ -357,7 +384,7 @@ func newBaseParserProvider() config.MapProvider {
return parserprovider.NewExpandMapProvider(parserprovider.NewInMemoryMapProvider(bytes.NewBufferString(configYaml)))
}

return parserprovider.NewDefaultMapProvider(configPath, nil)
return parserprovider.NewDefaultMapProvider(configPath, getSetProperties())
}

func runInteractive(settings service.CollectorSettings) error {
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/linux-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ docker run --rm -e SPLUNK_ACCESS_TOKEN=12345 -e SPLUNK_REALM=us0 \
-p 13133:13133 -p 14250:14250 -p 14268:14268 -p 4317:4317 -p 6060:6060 \
-p 8888:8888 -p 9080:9080 -p 9411:9411 -p 9943:9943 \
--name otelcol quay.io/signalfx/splunk-otel-collector:latest \
--log-level=DEBUG
--set=service.telemetry.logs.level=debug
```

> Use `--help` to see all available CLI arguments.
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/windows-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ service:
For older versions of the Collector you can alter the service `ImagePath` before restarting:

```sh
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\splunk-otel-collector" -name "ImagePath" -value "C:\Program Files\Splunk\OpenTelemetry Collector\otelcol.exe --log-level debug"
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\splunk-otel-collector" -name "ImagePath" -value "C:\Program Files\Splunk\OpenTelemetry Collector\otelcol.exe --set=service.telemetry.logs.level=debug"
Restart-Service splunk-otel-collector

# Reverting after observing logs:
Expand Down
2 changes: 1 addition & 1 deletion examples/prometheus-federation/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ services:
otelcollector:
image: quay.io/signalfx/splunk-otel-collector:0.29.0
container_name: otelcollector
command: ["--config=/etc/otel-collector-config.yml", "--log-level=DEBUG"]
command: ["--config=/etc/otel-collector-config.yml", "--set=service.telemetry.logs.level=debug"]
volumes:
- ./otel-collector-config.yml:/etc/otel-collector-config.yml
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion examples/splunk-hec-metrics/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ services:
otelcollector:
image: quay.io/signalfx/splunk-otel-collector:0.29.0
container_name: otelcollector
command: ["--config=/etc/otel-collector-config.yml", "--log-level=DEBUG"]
command: ["--config=/etc/otel-collector-config.yml", "--set=service.telemetry.logs.level=debug"]
volumes:
- ./otel-collector-config.yml:/etc/otel-collector-config.yml
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion examples/splunk-hec-traces/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
otelcollector:
image: quay.io/signalfx/splunk-otel-collector:0.29.0
container_name: otelcollector
command: ["--config=/etc/otel-collector-config.yml", "--log-level=DEBUG"]
command: ["--config=/etc/otel-collector-config.yml", "--set=service.telemetry.logs.level=debug"]
volumes:
- ./otel-collector-config.yml:/etc/otel-collector-config.yml
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion examples/splunk-hec/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ services:
otelcollector:
image: quay.io/signalfx/splunk-otel-collector:0.29.0
container_name: otelcollector
command: ["--config=/etc/otel-collector-config.yml", "--log-level=DEBUG"]
command: ["--config=/etc/otel-collector-config.yml", "--set=service.telemetry.logs.level=debug"]
volumes:
- ./otel-collector-config.yml:/etc/otel-collector-config.yml
depends_on:
Expand Down
4 changes: 2 additions & 2 deletions tests/testutils/collector_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (collector CollectorProcess) WithConfigPath(path string) Collector {
return &collector
}

// []string{"--log-level", collector.LogLevel, "--config", collector.ConfigPath, "--metrics-level", "none"} by default
// []string{"--set=service.telemetry.logs.level={collector.LogLevel}", "--config", collector.ConfigPath, "--metrics-level", "none"} by default
func (collector CollectorProcess) WithArgs(args ...string) Collector {
collector.Args = args
return &collector
Expand Down Expand Up @@ -110,7 +110,7 @@ func (collector CollectorProcess) Build() (Collector, error) {
}
if collector.Args == nil {
collector.Args = []string{
"--log-level", collector.LogLevel, "--config", collector.ConfigPath, "--metrics-level", "none",
fmt.Sprintf("--set=service.telemetry.logs.level=%s", collector.LogLevel), "--config", collector.ConfigPath, "--metrics-level", "none",
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testutils/collector_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestCollectorProcessBuildDefaults(t *testing.T) {
assert.Equal(t, "someconfigpath", collector.ConfigPath)
assert.NotNil(t, collector.Logger)
assert.Equal(t, "info", collector.LogLevel)
assert.Equal(t, []string{"--log-level", "info", "--config", "someconfigpath", "--metrics-level", "none"}, collector.Args)
assert.Equal(t, []string{"--set=service.telemetry.logs.level=info", "--config", "someconfigpath", "--metrics-level", "none"}, collector.Args)
}

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