heroku

package module
v0.0.0-...-280b18c Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2020 License: MIT Imports: 9 Imported by: 0

README

Sarama Heroku

GoDoc

Overview

sarama-heroku is a Go library that makes it easy to connect to Apache Kafka on Heroku. We handle all the certificate management and configuration so that you can start up your Kafka consumers and producers with minimal effort.

Installation

go get -u github.com/deadmanssnitch/sarama-heroku
Heroku

Make sure you have the Heroku CLI plugin installed:

heroku plugins:install heroku-kafka

Next, you'll need to provision a new Kafka add-on or attach an existing one to your app. To provision run:

heroku addons:create heroku-kafka:basic-0 -a [app]
heroku kafka:wait -a [app]

Consumers

Now you are ready to start using the library.

Create a cluster consumer config like the following:

kfkCfg, err := heroku.NewConfig()

config := cluster.NewConfig()
config.ClientID = "app-name." + os.Getenv("DYNO")
config.Net.TLS.Enable = kfkCfg.TLS()
config.Net.TLS.Config = kfkCfg.TLSConfig()

groupID := kfkCfg.Prefix("group-id")
topics := []string{kfkCfg.Prefix("topic")}

consumer, err := cluster.NewClusterConsumer(groupID, topics, config)

❗ Multi-tenant plans require creating the consumer groups before you can use them.

heroku kafka:consumer-groups:create 'group-id' -a [app]

Producers

Furthermore, a producer can be either Sync or Async. Read up on the differences here.

Creating an async producer from a custom config:

kfkCfg, err := heroku.NewConfig()

config := sarama.NewConfig()
config.Producer.Return.Errors = true
config.Producer.RequiredAcks = sarama.WaitForAll
config.Net.TLS.Enable = kfkCfg.TLS()
config.Net.TLS.Config = kfkCfg.TLSConfig()

producer, err := sarama.NewAsyncProducer(kfkCfg.Brokers(), config)

❗ Multi-tenant plans require adding the KAFKA_PREFIX when sending messages. You should use heroku.AppendPrefixTo("topic") to ensure it's set.

producer <- &sarama.ProducerMessage{
  Topic: heroku.AppendPrefixTo("events"),
  Key:   sarama.StringEncoder(key),
  Value: []byte("Message"),
}

For more information about how to set up a config see the sarama documentation.

Multiple Kafka Instances

Use heroku.NewConfigWithName to build a config for a named Kafka instance.

kfkCfg, err := heroku.NewConfigWithName("ONYX")

Environment

Sarama Heroku depends on the following environment variables that are set by the Heroku Kafka add-on:

  • KAFKA_CLIENT_CERT
  • KAFKA_CLIENT_CERT_KEY
  • KAFKA_TRUSTED_CERT
  • KAFKA_PREFIX (only multi-tenant plans)
  • KAFKA_URL

Contributing

Thank you so much for your interest in contributing to this repository. We appreciate you and the work you're doing on this SO much.

For details see CONTRIBUTING.md

Thanks

This package was extracted from Dead Man's Snitch, a dead simple monitoring service for Cron jobs and system liveness.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendPrefixTo

func AppendPrefixTo(name string) string

AppendPrefixTo adds the env variable KAFKA_PREFIX to the given string if necessary. Heroku requires prefixing topics and consumer group names with the prefix on multi-tenant plans. It is safe to use on dedicated clusters if KAFKA_PREFIX is not set.

func Brokers

func Brokers() ([]string, error)

Brokers returns a list of host:port addresses for the Kafka brokers set in KAFKA_URL.

func NewAsyncProducer

func NewAsyncProducer(cfg *sarama.Config) (sarama.AsyncProducer, error)

NewAsyncProducer creates a github.com/Shopify/sarama.AsyncProducer configured from the standard Heroku Kafka environment. When publishing messages to Multitenant Kafka all topics need to start with KAFKA_PREFIX which is best added using AppendPrefixTo.

func NewConsumer

func NewConsumer(cfg *sarama.Config) (sarama.Consumer, error)

NewConsumer creates a github.com/Shopify/sarama.Consumer configured from the standard Heroku Kafka environment.

func NewSyncProducer

func NewSyncProducer(cfg *sarama.Config) (sarama.SyncProducer, error)

NewSyncProducer creates a github.com/Shopify/sarama.SyncProducer configured from the standard Heroku Kafka environment. When publishing messages to Multitenant Kafka all topics need to start with KAFKA_PREFIX which is best added using AppendPrefixTo.

func NewTLSConfig

func NewTLSConfig(trustedCert string, clientCert string, clientKey string) (*tls.Config, error)

NewTLSConfig constructs a *tls.Config from the given certificates that is appropriate for connecting to a Heroku Kafka broker.

func TLSConfig

func TLSConfig() (*tls.Config, error)

Create the TLS context, using the key and certificates provided.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig() (*Config, error)

NewConfig creates a config based on settings in KAFKA_URL

func NewConfigWithName

func NewConfigWithName(name string) (*Config, error)

NewConfigWithName returns a Config pulling from HEROKU_KAFKA_[NAME] environment variables. Using an empty string for name will use the unnamed Kafka instance.

func (*Config) Brokers

func (c *Config) Brokers() []string

Brokers returns the list of Kafka brokers to connect to.

func (*Config) Prefix

func (c *Config) Prefix(name string) string

Prefix is used to add the Heroku prefix to topics and consumer group ids. It is safe to use when no prefix is set or when the given name is already prefixed.

func (*Config) TLS

func (c *Config) TLS() bool

TLS will be true if TLS is required for the connection.

func (*Config) TLSConfig

func (c *Config) TLSConfig() *tls.Config

TLSConfig returns the *tls.Config that is configured with the needed certificates for Kafka and custom certificate verification to work with Heroku's certificates.

type Error

type Error struct {
	// contains filtered or unexported fields
}

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

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