ocsns

package
v0.0.0-...-9ac8be7 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ocsns provides a drop in replacement for your exisitng SNS client providing methods for persisiting spans to SNS topics.

client := ocsqs.New(sqs.New(session))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*SNS)

An Option function customizes a clients configuration

func WithPropagator

func WithPropagator(p propagation.Propagator) Option

WithPropagator sets the clients propagator

type SNS

type SNS struct {
	*sns.SNS

	// Propagator defines how traces will be propagated, if not specified this
	// will be B3
	Propagator propagation.Propagator
}

SNS embeds the AWS SDK SNS client allowing to be used as a drop in replacement for your existing SNS client.

func New

func New(client *sns.SNS, opts ...Option) *SNS

New constructs a new SNS client with default configuration values. Use Option functions to customise configuration. By default the propagator used is B3.

func (*SNS) PublishWithContext

func (sns *SNS) PublishWithContext(ctx aws.Context, input *sns.PublishInput, opts ...request.Option) (*sns.PublishOutput, error)

PublishWithContext wraps the AWS SDK SNS PublishWithContext method applying span context to the input message attributes according the given propagator

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sns"
	"go.krak3n.codes/ocaws"
	"go.krak3n.codes/ocaws/ocsns"
	"go.krak3n.codes/ocaws/propagation/b3"
	"go.opencensus.io/trace"
)

func main() {
	cfg := &aws.Config{
		Region: aws.String("eu-west-1"),
	}

	if v := os.Getenv("AWS_DEFAULT_REGION"); v != "" {
		cfg.Region = aws.String(v)
	}

	if v := os.Getenv("AWS_SNS_ENDPOINT"); v != "" {
		cfg.Endpoint = aws.String(v)
	}

	session, err := session.NewSession(cfg)
	if err != nil {
		log.Fatal(err)
	}

	ctx, span := trace.StartSpan(context.Background(), "sns/ExampleSNS_PublishWithContext")
	defer span.End()

	// Create SNS Client
	c := ocsns.New(sns.New(session))

	// Create Topic
	t, err := c.CreateTopic(&sns.CreateTopicInput{
		Name: aws.String("foo"),
	})
	if err != nil {
		log.Fatal(err)
	}

	// Publish message with span context message attributes
	in := &sns.PublishInput{
		TopicArn: t.TopicArn,
		Message:  aws.String(`{"foo":"bar"}`),
	}

	if _, err := c.PublishWithContext(ctx, in); err != nil {
		log.Fatal(err)
	}

	fmt.Println("TraceID:", *in.MessageAttributes[b3.TraceIDKey].StringValue)
	fmt.Println("SpanID:", *in.MessageAttributes[b3.SpanIDKey].StringValue)
	fmt.Println("Span Sampled:", *in.MessageAttributes[b3.SpanSampledKey].StringValue)
	fmt.Println("Trace Topic Name:", *in.MessageAttributes[ocaws.TraceTopicName].StringValue)

}
Output:

TraceID: 616263646566676869676b6c6d6e6f71
SpanID: 6162636465666768
Span Sampled: 0
Trace Topic Name: foo

Jump to

Keyboard shortcuts

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