Skip to content

Commit

Permalink
Support for syslog-based message and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Ludinard committed Oct 12, 2023
1 parent 12783a3 commit 35ae98e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 14 deletions.
Empty file added .charts/.keep
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build
.helmfile.yaml
.ca-bundle/*
.make.env
.charts
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -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")
}
70 changes: 59 additions & 11 deletions server/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
Expand All @@ -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 = "-"
}

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion server/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

var (
Version string
Commit string
)

func PrintVersion() {
fmt.Println(Version)
fmt.Printf("%s-%s", Version, Commit)
}

0 comments on commit 35ae98e

Please sign in to comment.