github.com/thiagoyeds/go-cloud@v0.26.0/server/xrayserver/server.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package xrayserver provides the diagnostic hooks for a server using
    16  // AWS X-Ray.
    17  package xrayserver // import "gocloud.dev/server/xrayserver"
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	exporter "contrib.go.opencensus.io/exporter/aws"
    24  	"github.com/aws/aws-sdk-go/aws/client"
    25  	"github.com/aws/aws-sdk-go/service/xray"
    26  	"github.com/aws/aws-sdk-go/service/xray/xrayiface"
    27  	"github.com/google/wire"
    28  	"go.opencensus.io/trace"
    29  	"gocloud.dev/server"
    30  	"gocloud.dev/server/requestlog"
    31  )
    32  
    33  // Set is a Wire provider set that provides the diagnostic hooks for
    34  // *server.Server. This set includes ServiceSet.
    35  var Set = wire.NewSet(
    36  	server.Set,
    37  	ServiceSet,
    38  	NewExporter,
    39  	wire.Bind(new(trace.Exporter), new(*exporter.Exporter)),
    40  	NewRequestLogger,
    41  	wire.Bind(new(requestlog.Logger), new(*requestlog.NCSALogger)),
    42  )
    43  
    44  // ServiceSet is a Wire provider set that provides the AWS X-Ray service
    45  // client given an AWS session.
    46  var ServiceSet = wire.NewSet(
    47  	NewXRayClient,
    48  	wire.Bind(new(xrayiface.XRayAPI), new(*xray.XRay)),
    49  )
    50  
    51  // NewExporter returns a new X-Ray exporter.
    52  //
    53  // The second return value is a Wire cleanup function that calls Close
    54  // on the exporter, ignoring the error.
    55  func NewExporter(api xrayiface.XRayAPI) (*exporter.Exporter, func(), error) {
    56  	e, err := exporter.NewExporter(exporter.WithAPI(api))
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  	return e, func() { e.Close() }, nil
    61  }
    62  
    63  // NewXRayClient returns a new AWS X-Ray client.
    64  func NewXRayClient(p client.ConfigProvider) *xray.XRay {
    65  	return xray.New(p)
    66  }
    67  
    68  // NewRequestLogger returns a request logger that sends entries to stdout.
    69  func NewRequestLogger() *requestlog.NCSALogger {
    70  	return requestlog.NewNCSALogger(os.Stdout, func(e error) { fmt.Fprintln(os.Stderr, e) })
    71  }