Skip to content

Commit

Permalink
further address @Issif feedback, mv otlp_init*go outputs/
Browse files Browse the repository at this point in the history
Signed-off-by: JuanJo Ciarlante <juanjosec@gmail.com>
  • Loading branch information
jjo committed Sep 5, 2023
1 parent b664f27 commit 3ac1ad4
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,15 @@ dynatrace:
apiurl: "" # Dynatrace API url, use https://ENVIRONMENTID.live.dynatrace.com/api for Dynatrace SaaS and https://YOURDOMAIN/e/ENVIRONMENTID/api for Dynatrace Managed, more info : https://dt-url.net/ej43qge
# minimumpriority: "" # minimum priority of event for using this output, order is emergency|alert|critical|error|warning|notice|informational|debug or "" (default)
# checkcert: true # check if ssl certificate of the output is valid (default: true)

otlp:
traces:
# endpoint: "" # OTLP endpoint in the form of http://{domain or ip}:4318/
# synced: false # Set to true if you want traces to be sent synchronously
# minimumpriority: "" # minimum priority of event for using this output, order is emergency|alert|critical|error|warning|notice|informational|debug or "" (default)
# checkcert: false # Set if you want to skip TLS certificate validation
# duration: 1000 # Artificial span duration in milliseconds
# traceidhash: "" # Trace ID Hash "seed" as gotemplate, to select fields from falcopayload output fields (default: automatic to support Kubernetes' or just container.ID)
```

Usage :
Expand Down Expand Up @@ -1224,6 +1233,7 @@ order is
- **OTLP_TRACES_SYNCED**: synced OTLP traces, instead of batched (default: `false`)
- **OTLP_TRACES_CHECKCERT**: check if ssl certificate of the endpoint is valid (default: `false`)
- **OTLP_TRACES_DURATION**: artificial duration for trace spans in milliseconds (default: `1000`)
- **OTLP_TRACES_TRACEIDHASH**: trace ID Hash "seed" as gotemplate, to select fields from falcopayload output fields (default: automatic to support Kubernetes' or just container.ID)
#### Slack/Rocketchat/Mattermost/Googlechat Message Formatting

The `SLACK_MESSAGEFORMAT` environment variable and `slack.messageformat` YAML
Expand Down
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/spf13/viper"
kingpin "gopkg.in/alecthomas/kingpin.v2"

"github.com/falcosecurity/falcosidekick/outputs"
"github.com/falcosecurity/falcosidekick/types"
)

Expand All @@ -35,7 +36,7 @@ func getConfig() *types.Configuration {
OTLP: types.OTLPOutputConfig{},
}

otlpSetEnvs()
outputs.OtlpSetEnvs()
configFile := kingpin.Flag("config-file", "config file").Short('c').ExistingFile()
version := kingpin.Flag("version", "falcosidekick version").Short('v').Bool()
kingpin.Parse()
Expand Down Expand Up @@ -480,7 +481,7 @@ func getConfig() *types.Configuration {
v.SetDefault("OTLP.Traces.Endpoint", "")
v.SetDefault("OTLP.Traces.Synced", false)
v.SetDefault("OTLP.Traces.MinimumPriority", "")
v.SetDefault("OTLP.Traces.CheckCert", false)
v.SetDefault("OTLP.Traces.CheckCert", true)
v.SetDefault("OTLP.Traces.TraceIDHash", "")
// NB: Unfortunately falco events don't provide endtime, artificially set
// it to 1000ms by default, override-able via OTLP_DURATION environment variable.
Expand Down
2 changes: 1 addition & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,6 @@ func forwardEvent(falcopayload types.FalcoPayload) {
}

if config.OTLP.Traces.Endpoint != "" && (falcopayload.Priority >= types.Priority(config.OTLP.Traces.MinimumPriority)) {
go otlpClient.OTLPPost(falcopayload)
go otlpClient.OTLPTracesPost(falcopayload)
}
}
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var (
promStats *types.PromStatistics

regPromLabels *regexp.Regexp
shutDownFuncs []func()
)

func init() {
Expand Down Expand Up @@ -733,12 +734,12 @@ func init() {

if config.OTLP.Traces.Endpoint != "" {
var err error
otlpClient, err = outputs.NewClient("OTLP", config.OTLP.Traces.Endpoint, false, false, config, stats, promStats, statsdClient, dogstatsdClient)
otlpClient, err = outputs.NewOtlpTracesClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.OTLP.Traces.Endpoint = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "OTLPTraces")
otlpShutdown = otlpInit()
shutDownFuncs = append(shutDownFuncs, otlpClient.ShutDownFunc)
}
}

Expand All @@ -747,11 +748,9 @@ func init() {

}

var otlpShutdown func()

func main() {
if otlpShutdown != nil {
defer otlpShutdown()
for _, shutdown := range shutDownFuncs {
defer shutdown()
}
if config.Debug {
log.Printf("[INFO] : Debug mode : %v", config.Debug)
Expand Down
1 change: 1 addition & 0 deletions outputs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type Client struct {
CheckCert bool
HeaderList []Header
ContentType string
ShutDownFunc func()
Config *types.Configuration
Stats *types.Statistics
PromStats *types.PromStatistics
Expand Down
17 changes: 16 additions & 1 deletion outputs/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"text/template"
"time"

"github.com/DataDog/datadog-go/statsd"
"github.com/falcosecurity/falcosidekick/types"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -21,6 +22,20 @@ import (
// Unit-testing helper
var getTracerProvider = otel.GetTracerProvider

func NewOtlpTracesClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {
otlpClient, err := NewClient("OTLP.Traces", config.OTLP.Traces.Endpoint, false, false, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
return nil, err
}
shutDownFunc, err := otlpInit(config)
if err != nil {
return nil, err
}
log.Printf("[INFO] : OTLP.Traces=%+v\n", config.OTLP.Traces)
otlpClient.ShutDownFunc = shutDownFunc
return otlpClient, nil
}

// newTrace returns a new Trace object.
func (c *Client) newTrace(falcopayload types.FalcoPayload) *trace.Span {
traceID, _, err := generateTraceID(falcopayload, c.Config)
Expand Down Expand Up @@ -96,7 +111,7 @@ var (
func sanitizeOutputFields(falcopayload types.FalcoPayload) map[string]interface{} {
ret := make(map[string]interface{})
for k, v := range falcopayload.OutputFields {
k := strings.ReplaceAll(k, ".", "_")
k := strings.ReplaceAll(k, ".", "_")
ret[k] = v
}
return ret
Expand Down
30 changes: 16 additions & 14 deletions otlp_init.go → outputs/otlp_init.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main
package outputs

import (
"context"
"fmt"
"log"
"os"

"github.com/falcosecurity/falcosidekick/types"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
otelresource "go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
)
Expand All @@ -19,21 +20,21 @@ const (
OTLPinstrumentationVersion = "v0.1.0"
)

func newResource() *resource.Resource {
return resource.NewWithAttributes(
func newResource() *otelresource.Resource {
return otelresource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(OTLPinstrumentationName),
semconv.ServiceVersion(OTLPinstrumentationVersion),
)
}

func installExportPipeline(ctx context.Context) (func(context.Context) error, error) {
func installExportPipeline(config *types.Configuration, ctx context.Context) (func(context.Context) error, error) {
var client otlptrace.Client
switch config.OTLP.Traces.Insecure {
switch config.OTLP.Traces.CheckCert {
case true:
client = otlptracehttp.NewClient(otlptracehttp.WithInsecure())
case false:
client = otlptracehttp.NewClient()
case false:
client = otlptracehttp.NewClient(otlptracehttp.WithInsecure())
}

exporter, err := otlptrace.New(ctx, client)
Expand All @@ -54,19 +55,20 @@ func installExportPipeline(ctx context.Context) (func(context.Context) error, er
return tracerProvider.Shutdown, nil
}

func otlpInit() func() {
func otlpInit(config *types.Configuration) (func(), error) {
ctx := context.Background()
// Registers a tracer Provider globally.
shutdown, err := installExportPipeline(ctx)
shutdown, err := installExportPipeline(config, ctx)
if err != nil {
log.Fatal(err)
log.Println(err)
return nil, err
}
shutDownCallback := func() {
if err := shutdown(ctx); err != nil {
log.Fatal(err)
log.Println(err)
}
}
return shutDownCallback
return shutDownCallback, nil
}

type otlpEnv struct {
Expand Down Expand Up @@ -114,7 +116,7 @@ func otlpSetEnv(envs []otlpEnv) string {
// - OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TRACES_HEADERS
// - OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_TRACES_TIMEOUT
// - OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
func otlpSetEnvs() {
func OtlpSetEnvs() {
otlpSetEnv([]otlpEnv{
// Set OTLP_TRACES_ENDPOINT (used by config.OTLP.Traces) from SDK OTLP env vars
{Target: "OTLP_TRACES_ENDPOINT", EnvName: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", Path: ""},
Expand Down
4 changes: 2 additions & 2 deletions otlp_init_test.go → outputs/otlp_init_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package outputs

import (
"testing"
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestOtlpInit(t *testing.T) {
for _, c := range cases {
otlpOS = newMockOS()
otlpOS.Setenv(c.key, c.value)
otlpSetEnvs()
OtlpSetEnvs()
require.Equal(t, c.wantedValue, otlpOS.Getenv(c.wantedKey), c.msg)
}
}

0 comments on commit 3ac1ad4

Please sign in to comment.