writer

package
v0.2.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2024 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package writer implements Sink which writes the []byte respresentation of an Event to an io.Writer as a string. Sink allows you to define sinks for any io.Writer which includes os.Stdout and os.Stderr

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sink

type Sink struct {

	// Format specifies the format the []byte representation is formatted in
	// Defaults to JSONFormat
	Format string

	// Writer is the io.Writer used when writing Events
	Writer io.Writer
	// contains filtered or unexported fields
}

Sink writes the []byte respresentation of an Event to an io.Writer as a string. Sink allows you to define sinks for any io.Writer which includes os.Stdout and os.Stderr

Example
package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/hashicorp/eventlogger"
	"github.com/hashicorp/eventlogger/sinks/writer"
)

func main() {
	then := time.Date(
		2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
	// Create a broker
	b, _ := eventlogger.NewBroker()

	b.StopTimeAt(then) // setting this so the output timestamps are predictable for testing.

	// Marshal to JSON
	jsonFmt := &eventlogger.JSONFormatter{}

	// Send the output to stdout
	stdoutSink := &writer.Sink{
		Writer: os.Stdout,
	}

	// Register the nodes with the broker
	nodes := []eventlogger.Node{jsonFmt, stdoutSink}
	nodeIDs := make([]eventlogger.NodeID, len(nodes))
	for i, node := range nodes {
		id := eventlogger.NodeID(fmt.Sprintf("node-%d", i))
		err := b.RegisterNode(id, node)
		if err != nil {
			// handle error
		}
		nodeIDs[i] = id
	}

	et := eventlogger.EventType("test-event")
	// Register a pipeline for our event type
	err := b.RegisterPipeline(eventlogger.Pipeline{
		EventType:  et,
		PipelineID: "writer-sink-pipeline",
		NodeIDs:    nodeIDs,
	})
	if err != nil {
		// handle error
	}

	p := map[string]interface{}{
		"name":      "bob",
		"role":      "user",
		"pronouns":  []string{"they", "them"},
		"coworkers": []string{"alice", "eve"},
	}
	// Send an event
	if status, err := b.Send(context.Background(), et, p); err != nil {
		// handle err and status.Warnings
		fmt.Println("err: ", err)
		fmt.Println("warnings: ", status.Warnings)
	}

}
Output:

{"created_at":"2009-11-17T20:34:58.651387237Z","event_type":"test-event","payload":{"coworkers":["alice","eve"],"name":"bob","pronouns":["they","them"],"role":"user"}}

func (*Sink) Process

func (fs *Sink) Process(ctx context.Context, e *eventlogger.Event) (*eventlogger.Event, error)

Process will Write the event to the Sink

func (*Sink) Reopen

func (fs *Sink) Reopen() error

Reopen does nothing for this type of Sink. They cannot be rotated.

func (*Sink) Type

func (fs *Sink) Type() eventlogger.NodeType

Type defines the Sink as a NodeTypeSink

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL