Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
FROM umputun/baseimage:buildgo-latest as build
FROM umputun/baseimage:buildgo-latest AS build

ARG GIT_BRANCH
ARG GITHUB_SHA
ARG CI

ENV CGO_ENABLED=0


ADD . /build
WORKDIR /build

Expand All @@ -19,7 +20,7 @@ RUN \

FROM umputun/baseimage:app-latest
LABEL org.opencontainers.image.source="https://github.com/umputun/docker-logger"

ENV APP_UID=995
COPY --from=build /build/docker-logger /srv/docker-logger
RUN \
chown -R app:app /srv && \
Expand Down
34 changes: 29 additions & 5 deletions app/discovery/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type DockerClient interface {
}

var reGroup = regexp.MustCompile(`/(.*?)/`)
var reSwarm = regexp.MustCompile(`(?m)(.*)\.(\d+)\.(.*)`)

// NewEventNotif makes EventNotif publishing all changes to eventsCh
func NewEventNotif(dockerClient DockerClient, excludes, includes []string, includesPattern, excludesPattern string) (*EventNotif, error) {
Expand Down Expand Up @@ -106,8 +107,8 @@ func (e *EventNotif) activate(client DockerClient) {
}

log.Printf("[DEBUG] api event %+v", dockerEvent)
containerName := strings.TrimPrefix(dockerEvent.Actor.Attributes["name"], "/")

containerName := buildContainerName(dockerEvent.Actor.Attributes, strings.TrimPrefix(dockerEvent.Actor.Attributes["name"], "/"))
groupName := buildGroupName(dockerEvent.Actor.Attributes, e.group(dockerEvent.From))
if !e.isAllowed(containerName) {
log.Printf("[INFO] container %s excluded", containerName)
continue
Expand All @@ -118,7 +119,7 @@ func (e *EventNotif) activate(client DockerClient) {
ContainerName: containerName,
Status: contains(dockerEvent.Status, upStatuses),
TS: time.Unix(dockerEvent.Time/1000, dockerEvent.TimeNano),
Group: e.group(dockerEvent.From),
Group: groupName,
}
log.Printf("[INFO] new event %+v", event)
e.eventsCh <- event
Expand All @@ -135,7 +136,8 @@ func (e *EventNotif) emitRunningContainers() error {
log.Printf("[DEBUG] total containers = %d", len(containers))

for _, c := range containers {
containerName := strings.TrimPrefix(c.Names[0], "/")
containerName := buildContainerName(c.Labels, strings.TrimPrefix(c.Names[0], "/"))
groupName := buildGroupName(c.Labels, e.group(c.Image))
if !e.isAllowed(containerName) {
log.Printf("[INFO] container %s excluded", containerName)
continue
Expand All @@ -145,7 +147,7 @@ func (e *EventNotif) emitRunningContainers() error {
ContainerName: containerName,
ContainerID: c.ID,
TS: time.Unix(c.Created/1000, 0),
Group: e.group(c.Image),
Group: groupName,
}
log.Printf("[DEBUG] running container added, %+v", event)
e.eventsCh <- event
Expand Down Expand Up @@ -187,3 +189,25 @@ func contains(e string, s []string) bool {
}
return false
}

func buildContainerName(labels map[string]string, containerName string) string {
result := []string{}
if r := reSwarm.FindStringSubmatch(containerName); len(r) == 4 {
result = append(result, r[1]) // service name
result = append(result, r[2]) // replica number
} else if labelName, ok := labels["logger.container.name"]; ok && labelName != "" {
result = append(result, labelName)
}
if len(result) > 0 {
return strings.Join(result, "-")
}
return containerName
}

func buildGroupName(labels map[string]string, defaultValue string) string {
if labelGroup, ok := labels["logger.group.name"]; ok && labelGroup != "" {
return labelGroup
}

return defaultValue
}