Skip to content

update http output pugin - #957

Open
lixoi wants to merge 1 commit into
ozontech:masterfrom
lixoi:update-http-output-plugin
Open

update http output pugin#957
lixoi wants to merge 1 commit into
ozontech:masterfrom
lixoi:update-http-output-plugin

Conversation

@lixoi

@lixoi lixoi commented Mar 18, 2026

Copy link
Copy Markdown

Description

Когда не было http output плагина, мне пришлось реализовать свой. Я взял за основу output плагин elasticsearch.
Http плагин был необходим для передачи логов Куба (kube api) в CS (container security) по http. Т.e. мне надо было через file.d имитировать работу kube api audit-log webhooks.

Базовая настройка kube-api через webhook предполагает такую работу:
kube-api может разово передавать пачку событий формата

Batch = {
    {event 1},
    {event 2}, 
    ... 
    {event n} 
}

где: |Batch| = webhookMaxBatchSize, |event i| = maxEventSize.

А filed (да и все другие агенты) отправляет пачку событий формата:

Batch = {event 1},{event 2},...,{event n}

Таким образом, массив не упакован в { }, а это не валидный json и функция json.Unmarshal в плагине CS валится с ошибкой.

Поэтому filed был настроен так, чтобы отправлять одним постом один event: |Batch| = |event|.

Настройка заключается в том, что я в свой http output добавил (помимо сплитования) флаг поэлементной отправки данных массива логов (событий).

Чтобы перейти на ваш плагин, нам нужен параметр itemize_batch. В текущем PR добавлен флаг itemize_batch и функция обработки массива логов.

Fixes # (issue)

Comment on lines +438 to +442
// can't save even one log
if right-i == 1 {
statusCode = sc
err = r
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch / right-i == 1 handling here is unnecessary. Since each POST is a single event, right-i == 1 is only true on the last iteration, so a http.StatusRequestEntityTooLarge on any earlier event is dropped silently while a http.StatusRequestEntityTooLarge on the last one is returned.

But the whole error branch is redundant: if we just return (statusCode, err) on the first failing request, out() already increments the metric, logs it, and decides retry vs. drop by status code. And continuing the loop after a failure is pointless - on a retryable error the whole batch is resent anyway, and on http.StatusRequestEntityTooLarge out() drops the batch. So SendItemize can be represented like this:

func (p *Plugin) sendItemize(left int, right int, begin []int, data []byte) (int, error) {
    for i := left; i < right; i++ {
        if statusCode, err := p.send(data[begin[i]:begin[i+1]]); err != nil {
            return statusCode, err
        }
    }
    return http.StatusOK, nil
}

Comment on lines 147 to +157
// > @3@4@5@6
// >
// > Enable split big batches
SplitBatch bool `json:"split_batch" default:"false"` // *

// > @3@4@5@6
// >
// > Enable partial shipment big batches
// > Only one flag out of two (SplitBatch or ItemizeBatch) can be initialized
ItemizeBatch bool `json:"itemize_batch" default:"false"` // *

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making an enum is better than keeping two bool flags. We already validate string enums via options:"..." (e.g. gzip_compression_level).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants