Skip to content

Commit 09acc47

Browse files
authored
Merge pull request #7 from INFURA/IPFS-170
IPFS 170
2 parents cd06c41 + 3c0cad1 commit 09acc47

File tree

3 files changed

+147
-10
lines changed

3 files changed

+147
-10
lines changed

README.md

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# go-ipfs-datadog-plugin
22

3-
Datadog tracing plugin for go-ipfs.
4-
3+
This repository contains the following `go-ipfs` plugins:
4+
- Datadog tracing plugin configures the Datadog tracer to collect the traces and relay them to the agent. `go-ipfs` tracing instrumentation is partial at the moment but should improve over time.
5+
- Datadog logger plugin allows users to set log levels for each `go-ipfs` subsystem.
56

67
## Caveats
78

8-
- This plugin doesn't implement any tracing, it simply configures the Datadog tracer to collect the traces and relay them to the agent. `go-ipfs` tracing instrumentation is partial at the moment but should improve over time.
9-
109
- Plugins only work on Linux and MacOS at the moment. You can track the progress of this issue here: https://github.com/golang/go/issues/19282
1110

1211
- If you are using go-ipfs 0.4.22 or older, some traces will be lost. See: https://github.com/ipfs/go-ipfs/pull/6672
@@ -36,6 +35,55 @@ To update the go-ipfs, run:
3635

3736
Copy `datadog-plugin.so` to `$IPFS_DIR/plugins/datadog-plugin.so` (or run `make install` if you are installing locally)
3837

38+
### Configuration
39+
40+
Define plugin configurations variables in the ipfs config file.
41+
42+
- datadog-logger config:
43+
```
44+
{
45+
...
46+
"Plugins": {
47+
"Plugins": {
48+
...
49+
"datadog-logger": {
50+
"Config": {
51+
"Levels": {
52+
"fatal": ["system1", "system2", ...],
53+
"error": [...]
54+
"warn": [...]
55+
...
56+
},
57+
"DefaultLevel": "info"
58+
},
59+
"Disabled": false
60+
},
61+
...
62+
}
63+
},
64+
...
65+
}
66+
```
67+
68+
- datadog-tracer config:
69+
```
70+
{
71+
...
72+
"Plugins": {
73+
"Plugins": {
74+
...
75+
"datadog-tracer": {
76+
"Config": {
77+
"TracerName": "go-ipfs-custom"
78+
},
79+
"Disabled": false
80+
}
81+
...
82+
}
83+
},
84+
...
85+
}
86+
```
3987

4088
## References
4189

plugin/datadog.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package plugin
22

33
import (
4-
"os"
4+
"encoding/json"
55

66
"github.com/ipfs/go-ipfs/plugin"
77
logging "github.com/ipfs/go-log"
@@ -15,28 +15,41 @@ var log = logging.Logger("datadog")
1515

1616
var Plugins = []plugin.Plugin{
1717
&DatadogPlugin{},
18+
&LoggerPlugin{},
1819
}
1920

2021
var _ plugin.PluginTracer = &DatadogPlugin{}
2122

2223
var tracerName = "go-ipfs"
2324

24-
const tracerEnv = "IPFS_TRACER_NAME"
25+
type datadogConfig struct {
26+
TracerName string
27+
}
2528

2629
type DatadogPlugin struct{}
2730

2831
func (d *DatadogPlugin) Name() string {
29-
return "datadog"
32+
return "datadog-tracer"
3033
}
3134

3235
func (d *DatadogPlugin) Version() string {
3336
return "0.0.1"
3437
}
3538

3639
func (d *DatadogPlugin) Init(env *plugin.Environment) error {
37-
maybeName := os.Getenv(tracerEnv)
38-
if maybeName != "" {
39-
tracerName = maybeName
40+
if env == nil || env.Config == nil {
41+
return nil
42+
}
43+
bytes, err := json.Marshal(env.Config)
44+
if err != nil {
45+
return err
46+
}
47+
config := datadogConfig{}
48+
if err := json.Unmarshal(bytes, &config); err != nil {
49+
return err
50+
}
51+
if config.TracerName != "" {
52+
tracerName = config.TracerName
4053
}
4154
return nil
4255
}

plugin/logger.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package plugin
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
logging "github.com/ipfs/go-log"
8+
9+
"github.com/ipfs/go-ipfs/plugin"
10+
)
11+
12+
const defaultLevel = "error"
13+
14+
var _ plugin.Plugin = &LoggerPlugin{}
15+
16+
type loggerConfig struct {
17+
Levels map[string][]string
18+
DefaultLevel string
19+
}
20+
21+
type LoggerPlugin struct{}
22+
23+
func (l LoggerPlugin) Name() string {
24+
return "datadog-logger"
25+
}
26+
27+
func (l LoggerPlugin) Version() string {
28+
return "0.0.1"
29+
}
30+
31+
// Set log levels for each system (logger)
32+
func (l LoggerPlugin) Init(env *plugin.Environment) error {
33+
// If no plugin config given, exit with default settings
34+
if env == nil || env.Config == nil {
35+
return nil
36+
}
37+
38+
config, err := l.loadConfig(env.Config)
39+
if err != nil {
40+
return err
41+
}
42+
43+
// set default log level for all loggers
44+
defaultLevel, err := logging.LevelFromString(config.DefaultLevel)
45+
if err != nil {
46+
return err
47+
}
48+
49+
logging.SetAllLoggers(defaultLevel)
50+
for level, subsystems := range config.Levels {
51+
for _, subsystem := range subsystems {
52+
if err := logging.SetLogLevel(subsystem, level); err != nil {
53+
return fmt.Errorf("set log level failed for subsystem: %s. Error: %s", subsystem, err.Error())
54+
}
55+
}
56+
}
57+
58+
return nil
59+
}
60+
61+
func (l LoggerPlugin) loadConfig(envConfig interface{}) (*loggerConfig, error) {
62+
// load config data
63+
bytes, err := json.Marshal(envConfig)
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
config := loggerConfig{
69+
DefaultLevel: defaultLevel,
70+
}
71+
if err = json.Unmarshal(bytes, &config); err != nil {
72+
return nil, err
73+
}
74+
75+
return &config, nil
76+
}

0 commit comments

Comments
 (0)