fluentdpub

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2020 License: Apache-2.0 Imports: 11 Imported by: 1

README

fluentdpub - fluentd backend for gocloud.dev's pubsub

GoDoc

fluentdpub is a backend for gocloud.dev. It only supports publisher (topic) side.

It supports two style API like gocloud.dev, common URL constructor and Fluentd specific constructor.

Common URL Constructor

After importing fluentdpub package, you can open bia pubsub.OpenTopic() function. fluentd:// URL only contains tag prefix.

Upstream Fluentd server is specified by FLUENTD_UPSTREAM_URL env var.

import (
	"context"

	"gocloud.dev/pubsub"
	_ "github.com/future-architect/fluentdpub"
)

// pubsub.OpenTopic creates a *pubsub.Topic from a URL.
// This URL will Dial the Fluentd server at the URL in the environment variable
// FLUENTD_UPSTREAM_URL and send messages with tag "example.tag".
topic, err := pubsub.OpenTopic(ctx, "fluentd://stg.my-app")
if err != nil {
	return err
}
err = topic.Send(ctx, &pubsub.Message{
    // Fluentd doesn't have main contant named body
    // fluentdpub embeds body into metadata.
    // Default key of body is "message"
	Body: []byte("Hello, World!\n"),
	// Metadata is optional and can be nil.
	Metadata: map[string]string{
		// These are examples of metadata.
		// There is nothing special about the key names.
		"language":   "en",
		"importance": "high",
	},
})

defer topic.Shutdown(ctx)
Option

URL can have two queries:

  • bodykey (default: "message"): Message.Body content is stored into Metadata of this key.
  • tagkey (default: "tag"): The Value in this key of Metadata is used as tag prefix.
Tag Name

There three locations you can specify tag.

  • FLUENTD_UPSTREAM_URL's path name
  • URL's host name
  • Metadata's tag value (specified by tagkey parameter or "tag" by default)

If FLUENTD_UPSTREAM_URL is "tcp://localhost:24224/prod" URL is fluentd://my-app and the message to send contains "tag" Metadata with the value "error", log will be sent with prod.my-app.error tag. Empty element will be ignored (if FLUENTD_UPSTREAM_URL is "tcp://localhost:24224", final tag will be my-app.error).

Fluentd Specific Constructor

The fluentdpub.OpenTopic constructor opens a Fluentd subject as a topic. You must first create an *fluent.Fluent to your Fluentd server instance.

import (
	"context"

	"github.com/future-architect/fluentdpub"
)

// pubsub.OpenTopic creates a *pubsub.Topic from a URL.
// This URL will Dial the Fluentd server at the URL in the environment variable
// FLUENTD_UPSTREAM_URL and send messages with tag "example.tag".
f, err := fluent.New(fluent.Config{
	FluentHost: "fluentd.example.com",
})
topic, err := fluentdpub.OpenTopic(f, "prod.myapp", fluentdpub.TopicOptions{})
if err != nil {
	return err
}
err = topic.Send(context.Background(), &pubsub.Message{
    // Fluentd doesn't have main contant named body
    // fluentdpub embeds body into metadata.
    // Default key of body is "message"
	Body: []byte("Hello, World!\n"),
	// Metadata is optional and can be nil.
	Metadata: map[string]string{
		// These are examples of metadata.
		// There is nothing special about the key names.
		"language":   "en",
		"importance": "high",
	},
})
Option

TopicOptions has two fields:

  • BodyKey (default: "message"): Message.Body content is stored into Metadata of this key.
  • TagKey (default: "tag"): The Value in this key of Metadata is used as tag prefix.
Tag Name

There two location you can specify tag.

  • URL's host name
  • Metadata's tag value (specified by tagkey parameter or "tag" by default)

If URL is fluentd://prod.my-app and the message to send contains "tag" Metadata with the value "error", log will be sent with prod.my-app.error tag. If one of them is empty, non empty one is used.

License

Apache 2

Documentation

Index

Constants

View Source
const Scheme = "fluentd"

Scheme is the URL scheme fluentdpub registers its URLOpeners under on pubsub.DefaultMux.

Variables

This section is empty.

Functions

func OpenTopic

func OpenTopic(f *fluent.Fluent, tagPrefix string, opt TopicOptions) (*pubsub.Topic, error)

OpenTopic returns a *pubsub.Topic for use with Fluentd. The subject is the Fluentd's tag; for more info, see https://github.com/fluent/fluent-logger-golang

func OpenTopicURL added in v0.0.4

func OpenTopicURL(ctx context.Context, topicUrl *url.URL, upstreamUrl string) (*pubsub.Topic, error)

Types

type TopicOptions

type TopicOptions struct {
	// BodyKey is a key name to store message body. Default is "message".
	BodyKey string
	// TagKey is a key name of additional tag. Default is "tag".
	TagKey string
}

TopicOptions sets options for constructing a *pubsub.Topic backed by Fluentd.

There is no option now.

type URLOpener

type URLOpener struct {
	// Connection to use for communication with the server.
	Connection *fluent.Fluent
	// TagPrefix is prefix of tags of all topic of this URLOpener
	TagPrefix string
}

URLOpener opens Fluentd URLs like "fluentd://myapp.tag".

Host part is used as a tag prefix

No query parameters are supported.

func (*URLOpener) OpenTopicURL

func (o *URLOpener) OpenTopicURL(ctx context.Context, u *url.URL) (*pubsub.Topic, error)

OpenTopicURL opens a pubsub.Topic based on u.

Jump to

Keyboard shortcuts

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