4
4
package main
5
5
6
6
import (
7
- "fmt"
8
- "io"
7
+ "context"
9
8
"log/slog"
10
9
"os"
10
+ "path/filepath"
11
11
12
12
"github.com/open-telemetry/opentelemetry-go-compile-instrumentation/tool/ex"
13
- "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/tool/internal/instrument"
14
- "github.com/open-telemetry/opentelemetry-go-compile-instrumentation/tool/internal/setup"
15
13
"github.com/open-telemetry/opentelemetry-go-compile-instrumentation/tool/util"
14
+ "github.com/urfave/cli/v3"
16
15
)
17
16
18
17
const (
19
- ActionSetup = "setup"
20
- ActionGo = "go"
21
- ActionIntoolexec = "toolexec"
22
- ActionVersion = "version"
23
- DebugLog = "debug.log"
18
+ exitCodeFailure = - 1
19
+
20
+ debugLogFilename = "debug.log"
24
21
)
25
22
26
- func cleanBuildTemp () {
27
- _ = os .RemoveAll (setup .OtelRuntimeFile )
23
+ func main () {
24
+ app := cli.Command {
25
+ Name : "otel" ,
26
+ Usage : "OpenTelemetry Go Compile-Time Instrumentation Tool" ,
27
+ HideVersion : true ,
28
+ Flags : []cli.Flag {
29
+ & cli.StringFlag {
30
+ Name : "work-dir" ,
31
+ Aliases : []string {"w" },
32
+ Usage : "The path to a directory where working files will be written" ,
33
+ TakesFile : true ,
34
+ Value : filepath .Join ("." , util .BuildTempDir ),
35
+ Sources : cli .NewValueSourceChain (cli .EnvVar (util .EnvOtelWorkDir )),
36
+ },
37
+ },
38
+ Commands : []* cli.Command {
39
+ & commandSetup ,
40
+ & commandGo ,
41
+ & commandToolexec ,
42
+ & commandVersion ,
43
+ },
44
+ Before : initLogger ,
45
+ }
46
+
47
+ err := app .Run (context .Background (), os .Args )
48
+ if err != nil {
49
+ ex .Fatal (err )
50
+ }
28
51
}
29
52
30
- func initLogger (phase string ) (* slog.Logger , error ) {
31
- var writer io.Writer
32
- switch phase {
33
- case ActionSetup , ActionGo :
34
- // Create .otel-build dir
35
- _ , err := os .Stat (util .BuildTempDir )
36
- if os .IsNotExist (err ) {
37
- err = os .MkdirAll (util .BuildTempDir , 0o755 )
38
- if err != nil {
39
- return nil , ex .Errorf (err , "failed to create .otel-build dir" )
40
- }
41
- }
42
- // Configure slog to write to the debug.log file
43
- path := util .GetBuildTemp (DebugLog )
44
- logFile , err := os .OpenFile (path , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0o777 )
45
- if err != nil {
46
- return nil , ex .Errorf (err , "failed to open log file" )
47
- }
48
- writer = logFile
49
- case ActionIntoolexec :
50
- path := util .GetBuildTemp (DebugLog )
51
- logFile , err := os .OpenFile (path , os .O_WRONLY | os .O_APPEND , 0o777 )
52
- if err != nil {
53
- return nil , ex .Errorf (err , "failed to open log file" )
54
- }
55
- writer = logFile
56
- default :
57
- return nil , ex .Errorf (nil , "invalid action: %s" , phase )
53
+ func initLogger (ctx context.Context , cmd * cli.Command ) (context.Context , error ) {
54
+ buildTempDir := cmd .String ("work-dir" )
55
+ err := os .MkdirAll (buildTempDir , 0o755 )
56
+ if err != nil {
57
+ return ctx , ex .Errorf (err , "failed to create work directory %q" , buildTempDir )
58
+ }
59
+
60
+ logFilename := filepath .Join (buildTempDir , debugLogFilename )
61
+ writer , err := os .OpenFile (logFilename , os .O_CREATE | os .O_APPEND | os .O_WRONLY , 0o644 )
62
+ if err != nil {
63
+ return ctx , ex .Errorf (err , "failed to open log file %q" , buildTempDir )
58
64
}
59
65
60
66
// Create a custom handler with shorter time format
61
67
// Remove time and level keys as they make no sense for debugging
62
- var handler slog.Handler
63
- handler = slog .NewTextHandler (writer , & slog.HandlerOptions {
68
+ handler := slog .NewTextHandler (writer , & slog.HandlerOptions {
64
69
ReplaceAttr : func (_ []string , a slog.Attr ) slog.Attr {
65
70
if a .Key == slog .TimeKey || a .Key == slog .LevelKey {
66
71
return slog.Attr {}
@@ -69,75 +74,14 @@ func initLogger(phase string) (*slog.Logger, error) {
69
74
},
70
75
Level : slog .LevelInfo ,
71
76
})
72
- handler = handler .WithAttrs ([]slog.Attr {
73
- {
74
- Key : "phase" ,
75
- Value : slog .StringValue (phase ),
76
- },
77
- })
78
77
logger := slog .New (handler )
79
- return logger , nil
80
- }
81
-
82
- func main () {
83
- if len (os .Args ) < 2 { //nolint:mnd // number of args
84
- println ("Usage: otel <action> <args...>" )
85
- println ("Actions:" )
86
- println (" setup Set up the environment for instrumentation." )
87
- println (" go Invoke the go command with toolexec mode." )
88
- println (" version Print the version of the tool." )
89
- os .Exit (1 )
90
- }
91
-
92
- action := os .Args [1 ]
93
- switch action {
94
- case ActionVersion :
95
- _ , _ = fmt .Printf ("otel version %s_%s_%s\n " , Version , CommitHash , BuildTime )
96
- case ActionSetup :
97
- // otel setup - This command is used to set up the environment for
98
- // instrumentation. It should be run before other commands.
99
- logger , err := initLogger (ActionSetup )
100
- if err != nil {
101
- ex .Fatal (err )
102
- }
103
-
104
- err = setup .Setup (logger )
105
- if err != nil {
106
- ex .Fatal (err )
107
- }
108
- case ActionGo :
109
- // otel go build - Invoke the go command with toolexec mode. If the setup
110
- // is not done, it will run the setup command first.
111
- defer cleanBuildTemp ()
112
- backup := []string {"go.mod" , "go.sum" , "go.work" , "go.work.sum" }
113
- util .BackupFile (backup )
114
- defer util .RestoreFile (backup )
78
+ ctx = util .ContextWithLogger (ctx , logger )
115
79
116
- logger , err := initLogger (ActionGo )
117
- if err != nil {
118
- ex .Fatal (err )
119
- }
120
- err = setup .Setup (logger )
121
- if err != nil {
122
- ex .Fatal (err )
123
- }
124
- err = setup .BuildWithToolexec (logger , os .Args [1 :])
125
- if err != nil {
126
- ex .Fatal (err )
127
- }
128
- case ActionIntoolexec :
129
- ex .Fatalf ("It should not be used directly" )
130
- default :
131
- // in -toolexec - This should not be used directly, but rather
132
- // invoked by the go command with toolexec mode.
133
- logger , err := initLogger (ActionIntoolexec )
134
- if err != nil {
135
- ex .Fatal (err )
136
- }
80
+ return ctx , nil
81
+ }
137
82
138
- err = instrument .Toolexec (logger , os .Args [1 :])
139
- if err != nil {
140
- ex .Fatal (err )
141
- }
142
- }
83
+ func addLoggerPhaseAttribute (ctx context.Context , cmd * cli.Command ) (context.Context , error ) {
84
+ logger := util .LoggerFromContext (ctx )
85
+ logger = logger .With ("phase" , cmd .Name )
86
+ return util .ContextWithLogger (ctx , logger ), nil
143
87
}
0 commit comments