diff --git a/.charts/.keep b/.charts/.keep new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index 9d3806a..5021124 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build .helmfile.yaml .ca-bundle/* .make.env +.charts diff --git a/Makefile b/Makefile index fb359b1..8183a58 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ VERSION := 1.0.0 COMMIT := $(shell git rev-parse --short HEAD) # Default to RHEL bundle. Override it in .make.env -export CA_BUNDLE=/etc/pki/ca-trust/source/anchors/ +export CA_BUNDLE="${CA_BUNDLE:/etc/pki/ca-trust/source/anchors/}" # Override local dev environment ifeq ($(shell test -e .make.env && echo yes), yes) diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100644 index 0000000..7d22b52 --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +BASEDIR="$(realpath "${BASH_SOURCE%/*}/..")" + +if [[ -f "${BASEDIR}/.env" ]]; then + # shellcheck source=/dev/null + source "${BASEDIR}/.env" +fi + +CA_PATHS=( + "/etc/pki/ca-trust/source/anchors" # RHEL + "/usr/local/share/ca-certificates" # Ubuntu / Alpine + "/usr/share/pki/trust/anchors" # OpenSUSE + "/etc/pki/trust/anchors" # OpenSUSE +) + +function sync_local_ca () { + for path in "${CA_PATHS[@]}"; do + if [[ -d "${path}" ]] && find "${path}" -mindepth 1 -maxdepth 1 | read -r; then + rsync -ahP "${path}/" "${BASEDIR}/.ca-bundle/" + return + fi + done + (>&2 echo "Could not find any standard system CA directory") +} diff --git a/server/convert.go b/server/convert.go index 22ec19e..7471c79 100644 --- a/server/convert.go +++ b/server/convert.go @@ -16,6 +16,9 @@ type SnoozeAlertV1 struct { Host string `json:"host"` Process string `json:"process"` Severity string `json:"severity"` + ExceptionType string `json:"exception_type,omitempty"` + ExceptionMessage string `json:"exception_message,omitempty"` + ExceptionStack string `json:"exception_trace,omitempty"` Message string `json:"message"` Attributes map[string]string `json:"attributes"` } @@ -47,8 +50,9 @@ func populateKubernetes(alert *SnoozeAlertV1, ra map[string]string) { alert.Attributes["k8s_kind"] = "job" alert.Attributes["k8s_job_name"] = name alert.Process = fmt.Sprintf("job/%s", name) - - } + } else if svc, ok := ra["service.name"]; ok { + alert.Process = svc + } var cluster string var ns string @@ -60,7 +64,9 @@ func populateKubernetes(alert *SnoozeAlertV1, ra map[string]string) { } if ns, ok = ra["k8s.namespace.name"]; ok { alert.Attributes["k8s_namespace_name"] = ns - } else { + } else if ns, ok := ra["service.namespace"]; ok { + alert.Attributes["k8s_namespace_name"] = ns + } else { ns = "-" } @@ -76,6 +82,42 @@ func populateKubernetes(alert *SnoozeAlertV1, ra map[string]string) { alert.Attributes["k8s_container_name"] = name } + alert.Source = "otel/k8s" +} + +// Populate the alert with syslog metadata +func populateSyslog(alert *SnoozeAlertV1, ra, la map[string]string) { + + if hostname, ok := ra["host.name"]; ok { + alert.Host = hostname + } + + if svc, ok := ra["service.name"]; ok { + alert.Process = svc + } else if cmd, ok := la["process.executable.name"]; ok { + alert.Process = cmd + } + + for k, v := range la { + alert.Attributes[k] = v + } + + alert.Source = "otel/syslog" + +} + +func populateException(alert *SnoozeAlertV1, la map[string]string) { + + if etype, ok := la["exception.type"]; ok { + alert.ExceptionType = etype + } + if msg, ok := la["exception.message"]; ok { + alert.ExceptionMessage = msg + } + if stack, ok := la["exception.stacktrace"]; ok { + alert.ExceptionStack = stack + } + } // Convert an opentelemetry record log (with resource and scope contexts) to a snooze alert @@ -87,23 +129,29 @@ func convertAlert(resource *resv1.Resource, scope *commonv1.InstrumentationScope // Building attributes ra := kvToMap(resource.Attributes) //sa := kvToMap(scope.Attributes) - //la := kvToMap(lr.Attributes) + la := kvToMap(lr.Attributes) + + alert.Source = "otel" if hasPrefixedKey(ra, "k8s.") { populateKubernetes(&alert, ra) - } - - alert.Source = "otel" + } else if hasPrefixedKey(ra, "host.") { + populateSyslog(&alert, ra, la) + } else { + if name, ok := ra["service.name"]; ok { + alert.Process = name + } + } + + if hasPrefixedKey(la, "exception.") { + populateException(&alert, la) + } alert.Timestamp = formatTime(lr.TimeUnixNano) if lr.ObservedTimeUnixNano != 0 { alert.Attributes["observed_time"] = formatTime(lr.ObservedTimeUnixNano) } - if name, ok := ra["service.name"]; ok { - alert.Process = name - } - alert.Attributes["trace_id"] = Hex(lr.TraceId) alert.Attributes["span_id"] = Hex(lr.SpanId) alert.Severity = lr.SeverityText diff --git a/server/main.go b/server/main.go index 01a5ebb..55de989 100644 --- a/server/main.go +++ b/server/main.go @@ -75,7 +75,7 @@ func initLogging() { } func Run() { - log.Infof("Starting snooze-otlp %s", Version) + log.Infof("Starting snooze-otlp %s-%s", Version, Commit) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", Config.GrpcListeningPort)) if err != nil { diff --git a/server/version.go b/server/version.go index cc971bc..72d4763 100644 --- a/server/version.go +++ b/server/version.go @@ -6,8 +6,9 @@ import ( var ( Version string + Commit string ) func PrintVersion() { - fmt.Println(Version) + fmt.Printf("%s-%s", Version, Commit) }