nrcontext

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2017 License: MIT Imports: 2 Imported by: 0

README

nrcontext

Build Status Go Report Card GoDoc License MIT

The go-agent from New Relic currently does not have support for storing newrelic.Application and newrelic.Transaction in context.Context.

This package is an experiment in doing just that.

Status

Still under active development. Expect some additions but few if any changes to the public API.

Installation

go get -u github.com/TV4/nrcontext

Background

Initially based on ideas in the article Using Go’s ‘context’ library for performance monitoring by Sam Vilain.

An alternative to this package is newrelic-context by Maxim Sukharev.

License (MIT)

Copyright (c) 2017 TV4

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package nrcontext allows you to use the New Relic go-agent with context.Context

Description

The go-agent from New Relic currently (as of 2017-08-30) does not have support for storing newrelic.Application and newrelic.Transaction in context.Context.

This package is an experiment in doing just that.

Installation

Just go get the package:

go get -u github.com/TV4/nrcontext

Or more likely: use dep to vendor nrcontext

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Application

func Application(ctx context.Context) newrelic.Application

Application returns newrelic.Application stored in ctx, returns nil if missing

Example
package main

import (
	"context"
	"net/http/httptest"

	"github.com/TV4/nrcontext"
	"github.com/TV4/nrcontext/nrmock"
)

func main() {
	// Get a context containing a newrelic.Application (a mock in this example)
	ctx := nrcontext.WithApplication(context.Background(), nrmock.NewApplication())

	// Now you can retrieve the newrelic.Application from
	// ctx using the Application function
	app := nrcontext.Application(ctx)

	// Setup a http.ResponseWriter and a *http.Request for use in this example
	w, r := httptest.NewRecorder(), httptest.NewRequest("GET", "/foo", nil)

	// Start a transaction
	txn := app.StartTransaction("name", w, r)

	// End the transaction
	txn.End()
}
Output:

func Transaction

func Transaction(ctx context.Context) newrelic.Transaction

Transaction returns newrelic.Transaction stored in ctx, returns nil if missing

Example
package main

import (
	"context"
	"errors"

	"github.com/TV4/nrcontext"
	"github.com/TV4/nrcontext/nrmock"
)

func main() {
	// Get a context containing a newrelic.Transaction (a mock in this example)
	ctx := nrcontext.WithTransaction(context.Background(), nrmock.NewTransaction("name"))

	// Now you can retrieve the newrelic.Transaction from the context
	txn := nrcontext.Transaction(ctx)

	// You can now use the transaction to notice errors
	txn.NoticeError(errors.New("some error you want to notice"))

	// And to add attributes
	txn.AddAttribute("foo", "bar")
}
Output:

func WithApplication

func WithApplication(parent context.Context, app newrelic.Application) context.Context

WithApplication returns a copy of parent in which the value associated with appKey is app

Example
package main

import (
	"context"

	"github.com/TV4/nrcontext"
	"github.com/TV4/nrcontext/nrmock"
)

func main() {
	// Get a context containing a newrelic.Application (a mock in this example)
	ctx := nrcontext.WithApplication(context.Background(), nrmock.NewApplication())

	ctx.Done()
}
Output:

func WithTransaction

func WithTransaction(parent context.Context, txn newrelic.Transaction) context.Context

WithTransaction returns a copy of parent in which the value associated with txnKey is txn

Example
package main

import (
	"context"

	"github.com/TV4/nrcontext"
	"github.com/TV4/nrcontext/nrmock"
)

func main() {
	// Get a context containing a newrelic.Transaction (a mock in this example)
	ctx := nrcontext.WithTransaction(context.Background(), nrmock.NewTransaction("name"))

	ctx.Done()
}
Output:

Types

This section is empty.

Directories

Path Synopsis
Package nrmock contains mock implementations of newrelic.Application and newrelic.Transaction Links The newrelic.Application interface is defined here https://github.com/newrelic/go-agent/blob/master/application.go And the newrelic.Transaction interface is defined here https://github.com/newrelic/go-agent/blob/master/transaction.go
Package nrmock contains mock implementations of newrelic.Application and newrelic.Transaction Links The newrelic.Application interface is defined here https://github.com/newrelic/go-agent/blob/master/application.go And the newrelic.Transaction interface is defined here https://github.com/newrelic/go-agent/blob/master/transaction.go

Jump to

Keyboard shortcuts

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