Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Prepare a method of a Provider #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions internal/sinks/provider/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package provider

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/hashicorp/nomad/api"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -63,6 +65,15 @@ func NewHTTP(opts HTTPOpts) (*HTTPManager, error) {
return httpMgr, nil
}

// Prepare takes batches of events and returns JSON encoding of the same.
func (m *HTTPManager) Prepare(events []api.Event) ([]byte, error) {
data, err := json.Marshal(events)
if err != nil {
return nil, err
}
return data, nil
}

// Push sends out events to an HTTP Endpoint.
func (m *HTTPManager) Push(data []byte) error {
req, err := http.NewRequest("POST", m.rootURL, bytes.NewBuffer(data))
Expand Down
4 changes: 4 additions & 0 deletions internal/sinks/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package provider

import "github.com/hashicorp/nomad/api"

type Provider interface {
// Name returns the name of the Provider.
Name() string
// Prepare returns a prepared payload as an array of bytes
Prepare([]api.Event) ([]byte, error)
// Push pushes a batch of event to upstream. The implementation varies across providers.
Push([]byte) error
// Ping implements a healthcheck.
Expand Down
20 changes: 5 additions & 15 deletions internal/sinks/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sink

import (
"context"
"encoding/json"
"time"

"github.com/hashicorp/nomad/api"
Expand Down Expand Up @@ -74,26 +73,17 @@ func (w *Worker) flush(batch []api.Event) {
return
}

data, err := prepareJSON(batch)
if err != nil {
w.log.WithField("batch_len", len(batch)).WithError(err).Error("error while json marshall")
}

w.log.WithField("batch_len", len(batch)).Info("pushing events to providers")
for _, prov := range w.providers {
data, err := prov.Prepare(batch)
if err != nil {
w.log.WithField("batch_len", len(batch)).WithError(err).Error("error while json marshall")
}

err = prov.Push(data)
if err != nil {
// TODO: Handle the error better.
w.log.WithError(err).Error("error while pushing to provider")
}
}
}

// prepareJSON takes batches of events and returns JSON encoding of the same.
func prepareJSON(events []api.Event) ([]byte, error) {
data, err := json.Marshal(events)
if err != nil {
return nil, err
}
return data, nil
}