- commit
- 585081ea09e404da31f3fb9c5e6a2f17c6b73e92
- parent
- 666f75ec271ade6245e294ae7f9fb9e261f83157
- author
- Dami <dami.etoile@gmail.com>
| M | cmd/ | 4 | ++-- |
| M | cmd/ | 30 | +++++++++++++++++------------- |
| A | cmd/ | 22 | ++++++++++++++++++++++ |
@@ -24,9 +24,9 @@ func main() {
2424 }2525 2626 func run(args []string) error {27- tc, args, err := logFlag(args)27+ tc, args, err := telemetryFlags(args)2828 if err != nil || len(args) > 1 {29- return errors.New("usage: 012 [--log file.jsonl] [file]: a " + sheet.FileExt + " sheet, or a .csv, .tsv, .xlsx, .sqlite, .parquet or .wk1 file to import")29+ return errors.New("usage: 012 [--log file.jsonl] [--otlp http://localhost:4318] [file]: a " + sheet.FileExt + " sheet, or a .csv, .tsv, .xlsx, .sqlite, .parquet or .wk1 file to import")3030 }3131 stopTelemetry, err := startTelemetry(tc)3232 if err != nil {@@ -10,24 +10,28 @@ import (
1010 "github.com/FelineStateMachine/012/internal/telemetry"1111 )1212 13-// logFlag takes --log path (or --log=path) out of args. Without it the14-// log file comes from O12_LOG, and telemetry stays off when neither is15-// set.16-func logFlag(args []string) (telemetry.Config, []string, error) {13+// telemetryFlags takes --log path and --otlp url (or --log=path,14+// --otlp=url) out of args. Without them the log file comes from O12_LOG15+// and the OTLP endpoint from OTEL_EXPORTER_OTLP_ENDPOINT (and the other16+// OTEL_* variables), and telemetry stays off when none is set.17+func telemetryFlags(args []string) (telemetry.Config, []string, error) {1718 c := telemetry.ConfigFromEnv()19+ flags := map[string]*string{"--log": &c.LogPath, "--otlp": &c.OTLP.Endpoint}1820 var rest []string1921 for i := 0; i < len(args); i++ {20- switch a := args[i]; {21- case a == "--log":22- if i+1 >= len(args) {23- return c, nil, errors.New("--log needs a file")24- }25- c.LogPath = args[i+1]22+ a := args[i]23+ name, value, hasValue := strings.Cut(a, "=")24+ dst, ok := flags[name]25+ switch {26+ case !ok:27+ rest = append(rest, a)28+ case hasValue:29+ *dst = value30+ case i+1 < len(args):31+ *dst = args[i+1]2632 i++27- case strings.HasPrefix(a, "--log="):28- c.LogPath = strings.TrimPrefix(a, "--log=")2933 default:30- rest = append(rest, a)34+ return c, nil, errors.New(name + " needs a value")3135 }3236 }3337 return c, rest, nil@@ -0,0 +1,22 @@
1+package main2+3+import (4+ "slices"5+ "testing"6+)7+8+func TestTelemetryFlags(t *testing.T) {9+ t.Setenv("O12_LOG", "")10+ t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://env:4318")11+ c, rest, err := telemetryFlags([]string{"--log", "a.jsonl", "--otlp=http://localhost:4318", "b.csv"})12+ if err != nil || c.LogPath != "a.jsonl" || c.OTLP.Endpoint != "http://localhost:4318" || !slices.Equal(rest, []string{"b.csv"}) {13+ t.Errorf("got %+v %v %v", c, rest, err)14+ }15+ c, rest, err = telemetryFlags([]string{"b.csv"})16+ if err != nil || c.OTLP.Endpoint != "http://env:4318" || len(rest) != 1 {17+ t.Errorf("env endpoint: %+v %v %v", c, rest, err)18+ }19+ if _, _, err := telemetryFlags([]string{"--otlp"}); err == nil {20+ t.Error("--otlp without a value")21+ }22+}