Skip to content

Commit e523dc2

Browse files
author
ffffwh
committed
use a standalone logger instead of nomad logger #723
1 parent 411d21d commit e523dc2

File tree

11 files changed

+111
-93
lines changed

11 files changed

+111
-93
lines changed

api/handler/common.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ import (
1515
"net/url"
1616
"strings"
1717

18+
dtle "github.com/actiontech/dtle/driver"
1819
"github.com/actiontech/dtle/g"
1920

2021
"github.com/labstack/echo/v4"
2122
)
2223

24+
var DtleDriver *dtle.Driver
25+
2326
var NomadHost, ApiAddr, ConsulAddr string
2427

2528
// decodeBody is used to decode a JSON request body

api/handler/v2/log.go

+5-44
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
package v2
22

33
import (
4-
"bytes"
54
"fmt"
65
"net/http"
7-
"os"
8-
"os/exec"
9-
"strconv"
106
"strings"
117

128
"github.com/actiontech/dtle/api/handler"
13-
149
"github.com/actiontech/dtle/api/models"
15-
16-
"github.com/actiontech/dtle/g"
17-
hclog "github.com/hashicorp/go-hclog"
1810
"github.com/labstack/echo/v4"
19-
"github.com/shirou/gopsutil/v3/process"
2011
)
2112

2213
// @Id UpdateLogLevelV2
@@ -35,44 +26,14 @@ func UpdateLogLevelV2(c echo.Context) error {
3526
return c.JSON(http.StatusInternalServerError, models.BuildBaseResp(err))
3627
}
3728

38-
// verify
39-
logLevelStr := reqParam.DtleLogLevel
40-
logLevel := hclog.LevelFromString(logLevelStr)
41-
if logLevel == hclog.NoLevel {
42-
return c.String(http.StatusInternalServerError, fmt.Sprintf("dtle log level should be one of these value[\"TRACE\",\"DEBUG\",\"INFO\",\"WARN\",\"ERROR\"], got %v", logLevelStr))
43-
}
44-
45-
// reload nomad log level
46-
logger.Info("reload nomad log level")
47-
ppid := os.Getppid()
48-
p, err := process.NewProcess(int32(ppid))
49-
if nil != err {
50-
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to get parent(pid=%v) process info: %v", ppid, err))
51-
}
52-
pn, err := p.Name()
53-
if nil != err {
54-
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to get parent(pid=%v) process name: %v", ppid, err))
29+
err := handler.DtleDriver.SetLogLevel(reqParam.DtleLogLevel)
30+
if err != nil {
31+
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to set log level to %v. err: %v",
32+
reqParam.DtleLogLevel, err))
5533
}
5634

57-
if pn != "nomad" {
58-
return c.String(http.StatusInternalServerError, fmt.Sprintf("canot reload log level because the parent(pid=%v) process is not nomad", ppid))
59-
}
60-
61-
cmd := exec.Command("kill", "-SIGHUP", strconv.Itoa(ppid))
62-
var stderr bytes.Buffer
63-
cmd.Stderr = &stderr
64-
if err := cmd.Run(); nil != err {
65-
return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to reload log level of nomad(pid=%v): %v stderr: %v", ppid, err, stderr.String()))
66-
}
67-
68-
// reload dtle log level
69-
logger.Info("reload dtle log level")
70-
g.Logger.SetLevel(logLevel)
71-
72-
logger.Info("update log level", "dtle log_level", logLevelStr)
73-
7435
return c.JSON(http.StatusOK, &models.UpdataLogLevelRespV2{
75-
DtleLogLevel: strings.ToUpper(logLevelStr),
36+
DtleLogLevel: strings.ToUpper(reqParam.DtleLogLevel),
7637
BaseResp: models.BuildBaseResp(nil),
7738
})
7839
}

api/route.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
"github.com/actiontech/dtle/g"
13-
14-
report "github.com/actiontech/golang-live-coverage-report/pkg"
15-
16-
mysql "github.com/actiontech/dtle/driver"
17-
18-
"github.com/actiontech/dtle/api/models"
19-
20-
middleware "github.com/labstack/echo/v4/middleware"
21-
22-
"github.com/actiontech/dtle/driver/common"
23-
2412
_ "github.com/actiontech/dtle/api/docs"
2513
"github.com/actiontech/dtle/api/handler"
2614
v1 "github.com/actiontech/dtle/api/handler/v1"
2715
v2 "github.com/actiontech/dtle/api/handler/v2"
16+
"github.com/actiontech/dtle/api/models"
17+
dtle "github.com/actiontech/dtle/driver"
18+
"github.com/actiontech/dtle/driver/common"
19+
"github.com/actiontech/dtle/g"
20+
report "github.com/actiontech/golang-live-coverage-report/pkg"
21+
2822
metrics "github.com/armon/go-metrics"
2923
"github.com/armon/go-metrics/prometheus"
3024
hclog "github.com/hashicorp/go-hclog"
3125
"github.com/labstack/echo/v4"
26+
middleware "github.com/labstack/echo/v4/middleware"
3227
"github.com/prometheus/client_golang/prometheus/promhttp"
3328
echoSwagger "github.com/swaggo/echo-swagger"
3429
)
@@ -41,7 +36,7 @@ import (
4136
// @name Authorization
4237
// @BasePath /
4338

44-
func SetupApiServer(logger g.LoggerType, driverConfig *mysql.DriverConfig) (err error) {
39+
func SetupApiServer(logger g.LoggerType, driverConfig *dtle.DriverConfig) (err error) {
4540
logger.Debug("Begin Setup api server", "addr", driverConfig.ApiAddr)
4641
e := echo.New()
4742

cmd/nomad-plugin/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/shirou/gopsutil/v3/mem"
1212

1313
"github.com/actiontech/dtle/api"
14+
"github.com/actiontech/dtle/api/handler"
1415

1516
dtle "github.com/actiontech/dtle/driver"
1617
"github.com/actiontech/dtle/g"
@@ -58,9 +59,11 @@ func main() {
5859
logger.Info("dtle starting", "version", versionStr, "pid", pid)
5960
logger.Info("env", "GODEBUG", os.Getenv("GODEBUG"), "GOMAXPROCS", runtime.GOMAXPROCS(0))
6061
logger.Debug("plugins.Serve Factory called.")
61-
go g.DumpLoop(logger)
62+
go g.DumpLoop()
6263

63-
dtle.RegisterSetupApiServerFn(api.SetupApiServer)
64-
return dtle.NewDriver(logger)
64+
d := dtle.NewDriver(logger)
65+
handler.DtleDriver = d // TODO avoid global var
66+
d.SetSetupApiServerFn(api.SetupApiServer)
67+
return d
6568
})
6669
}

docker-images/example/nomad-docker.hcl

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ consul {
4747
plugin "dtle" {
4848
config {
4949
log_level = "INFO"
50+
log_file = "/dtle/var/log/dtle/"
5051
data_dir = "/dtle/var/lib/nomad"
5152
nats_bind = "127.0.0.1:8193"
5253
nats_advertise = "127.0.0.1:8193"

driver/driver.go

+74-23
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"net"
9+
"net/http"
810
"os"
911
"path"
12+
"path/filepath"
1013
"strings"
1114
"time"
1215

13-
"net"
14-
"net/http"
15-
1616
"github.com/actiontech/dtle/driver/common"
1717
"github.com/actiontech/dtle/g"
18-
"github.com/pkg/errors"
1918

2019
"github.com/hashicorp/go-hclog"
2120
"github.com/hashicorp/nomad/drivers/shared/eventer"
@@ -26,6 +25,9 @@ import (
2625
"github.com/julienschmidt/httprouter"
2726
gnatsd "github.com/nats-io/nats-server/v2/server"
2827
stand "github.com/nats-io/nats-streaming-server/server"
28+
"github.com/pkg/errors"
29+
30+
"gopkg.in/natefinch/lumberjack.v2"
2931
)
3032

3133
const (
@@ -80,6 +82,8 @@ var (
8082
hclspec.NewLiteral(`""`)),
8183
"memory": hclspec.NewAttr("memory", "string", false),
8284
"big_tx_max_jobs": hclspec.NewAttr("big_tx_max_jobs", "number", false),
85+
"log_file": hclspec.NewDefault(hclspec.NewAttr("log_file", "string", false),
86+
hclspec.NewLiteral(`"/var/log/dtle"`)),
8387
})
8488

8589
// taskConfigSpec is the hcl specification for the driver config section of
@@ -227,13 +231,14 @@ type Driver struct {
227231

228232
stand *stand.StanServer
229233
apiServer *httprouter.Router
234+
setupApiServerFn func(logger g.LoggerType, driverConfig *DriverConfig) error
230235

231236
config *DriverConfig
232237

233238
storeManager *common.StoreManager
234239
}
235240

236-
func NewDriver(logger g.LoggerType) drivers.DriverPlugin {
241+
func NewDriver(logger g.LoggerType) *Driver {
237242
logger = logger.Named(g.PluginName)
238243
logger.Info("dtle NewDriver")
239244

@@ -299,41 +304,79 @@ type DriverConfig struct {
299304
StatsCollectionInterval int `codec:"stats_collection_interval"`
300305
PublishMetrics bool `codec:"publish_metrics"`
301306
LogLevel string `codec:"log_level"`
307+
LogFile string `codec:"log_file"`
302308
UiDir string `codec:"ui_dir"`
303309
RsaPrivateKeyPath string `codec:"rsa_private_key_path"`
304310
CertFilePath string `codec:"cert_file_path"`
305311
KeyFilePath string `codec:"key_file_path"`
306312
Memory string `codec:"memory"`
307313
}
308314

315+
func (d *Driver) setupLogger() (err error) {
316+
err = d.SetLogLevel(d.config.LogLevel)
317+
if err != nil {
318+
return err
319+
}
320+
321+
if d.config.LogFile == "" {
322+
d.logger.Info("use nomad logger", "level", d.config.LogLevel)
323+
} else {
324+
err = os.MkdirAll(filepath.Dir(d.config.LogFile), 0755)
325+
if err != nil {
326+
return err
327+
}
328+
329+
logFileName := d.config.LogFile
330+
if strings.HasSuffix(logFileName, "/") {
331+
logFileName += "dtle.log"
332+
}
333+
334+
rotateFile := &lumberjack.Logger{
335+
Filename: logFileName,
336+
MaxSize: 512, // MB
337+
Compress: true,
338+
}
339+
340+
d.logger.Info("switching to dtle logger", "file", d.config.LogFile, "level", d.config.LogLevel)
341+
342+
d.logger = hclog.New(&hclog.LoggerOptions{
343+
Name: "",
344+
Level: hclog.Info,
345+
Output: rotateFile,
346+
})
347+
g.Logger = d.logger
348+
349+
err = d.SetLogLevel(d.config.LogLevel)
350+
if err != nil {
351+
return err
352+
}
353+
}
354+
355+
return nil
356+
}
357+
309358
func (d *Driver) SetConfig(c *base.Config) (err error) {
310359
if c != nil && c.AgentConfig != nil {
311360
d.nomadConfig = c.AgentConfig.Driver
312361
d.logger.Info("SetConfig 1", "DriverConfig", c.AgentConfig.Driver)
313362
}
314363

315-
var dconfig DriverConfig
364+
d.config = new(DriverConfig)
316365
if len(c.PluginConfig) != 0 {
317-
if err := base.MsgPackDecode(c.PluginConfig, &dconfig); err != nil {
366+
if err := base.MsgPackDecode(c.PluginConfig, d.config); err != nil {
318367
return err
319368
}
320369
}
321370

322-
d.config = &dconfig
323371
d.logger.Info("SetConfig 2", "config", d.config)
324372

325-
logLevel := hclog.LevelFromString(d.config.LogLevel)
326-
if logLevel == hclog.NoLevel {
327-
return fmt.Errorf("invalid log level %v", d.config.LogLevel)
328-
}
329-
d.logger.SetLevel(logLevel)
330-
d.logger.Info("log level was set", "level", logLevel.String())
373+
err = d.setupLogger()
331374

332375
//if dconfig.Memory != "" {
333376
// g.MemAvailable = TODO
334377
//}
335-
if dconfig.BigTxMaxJobs != 0 {
336-
g.BigTxMaxJobs = dconfig.BigTxMaxJobs
378+
if d.config.BigTxMaxJobs != 0 {
379+
g.BigTxMaxJobs = d.config.BigTxMaxJobs
337380
}
338381
d.logger.Info("BigTxMaxJobs is set", "value", g.BigTxMaxJobs)
339382

@@ -369,7 +412,7 @@ func (d *Driver) SetConfig(c *base.Config) (err error) {
369412
}
370413
}()
371414
} else {
372-
apiErr := setupApiServerFn(d.logger, d.config)
415+
apiErr := d.setupApiServerFn(d.logger, d.config)
373416
if apiErr != nil {
374417
d.logger.Error("error in SetupApiServer", "err", err,
375418
"apiAddr", d.config.ApiAddr, "nomadAddr", d.config.NomadAddr)
@@ -420,12 +463,6 @@ func (d *Driver) loopCleanRelayDir() {
420463
}
421464
}
422465

423-
var setupApiServerFn func(logger g.LoggerType, driverConfig *DriverConfig) error
424-
425-
func RegisterSetupApiServerFn(fn func(logger g.LoggerType, driverConfig *DriverConfig) error) {
426-
setupApiServerFn = fn
427-
}
428-
429466
func (d *Driver) TaskConfigSchema() (*hclspec.Spec, error) {
430467
return taskConfigSpec, nil
431468
}
@@ -812,3 +849,17 @@ func (d *Driver) Shutdown() {
812849
d.logger.Info("Driver.Shutdown")
813850
d.signalShutdown()
814851
}
852+
853+
func (d *Driver) SetLogLevel(level string) error {
854+
logLevel := hclog.LevelFromString(level)
855+
if logLevel == hclog.NoLevel {
856+
return fmt.Errorf("log level should be TRACE, DEBUG or INFO, got %v", level)
857+
}
858+
d.logger.SetLevel(logLevel)
859+
860+
return nil
861+
}
862+
863+
func (d *Driver) SetSetupApiServerFn(fn func(logger g.LoggerType, driverConfig *DriverConfig) (err error)) {
864+
d.setupApiServerFn = fn
865+
}

etc/dtle/nomad.hcl

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ telemetry {
6161
plugin "dtle" {
6262
config {
6363
log_level = "Info" # Repeat nomad log level here.
64+
log_file = "INSTALL_PREFIX_MAGIC/var/log/dtle/"
6465
data_dir = "INSTALL_PREFIX_MAGIC/var/lib/nomad"
6566
nats_bind = "127.0.0.1:8193"
6667
nats_advertise = "127.0.0.1:8193"

g/dump.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ import (
1313
hclog "github.com/hashicorp/go-hclog"
1414
)
1515

16-
func DumpLoop(logger hclog.Logger) {
16+
func DumpLoop() {
1717
c := make(chan os.Signal, 10)
1818
signal.Notify(c, syscall.SIGTTIN)
1919

2020
for {
2121
sig := <-c
2222
switch sig {
2323
case syscall.SIGTTIN:
24-
go CreateDump(logger)
24+
go CreateDump()
2525
default:
2626
}
2727
}
2828
}
2929

30-
func CreateDump(logger hclog.Logger) {
31-
logger.Debug("begin to create dtle dump")
30+
func CreateDump() {
31+
Logger.Debug("begin to create dtle dump")
3232
dumpPath := fmt.Sprintf("%s_%s/", "/tmp/dtle_dump", time.Now().Format("2006_01_02_15_04_05"))
3333

3434
if err := os.Mkdir(dumpPath, 0755); nil != err {
35-
logger.Error("create dump info failed", "dump path", dumpPath, "error", err)
35+
Logger.Error("create dump info failed", "dump path", dumpPath, "error", err)
3636
return
3737
}
38-
createDump(logger, dumpPath)
39-
logger.Debug("create dtle dump finished")
38+
createDump(Logger, dumpPath)
39+
Logger.Debug("create dtle dump finished")
4040
}
4141

4242
func createDump(logger hclog.Logger, path string) {

0 commit comments

Comments
 (0)