Skip to content

Instantly share code, notes, and snippets.

@BenHall
Created May 9, 2017 19:56
Show Gist options
  • Save BenHall/f42fc3c684895edf107a272f4a1df5f4 to your computer and use it in GitHub Desktop.
Save BenHall/f42fc3c684895edf107a272f4a1df5f4 to your computer and use it in GitHub Desktop.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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.
package cmd
import (
"net"
"strconv"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/uber/jaeger/examples/hotrod/pkg/log"
"github.com/uber/jaeger/examples/hotrod/pkg/tracing"
"github.com/uber/jaeger/examples/hotrod/services/frontend"
)
// frontendCmd represents the frontend command
var frontendCmd = &cobra.Command{
Use: "frontend",
Short: "Starts Frontend service",
Long: `Starts Frontend service.`,
RunE: func(cmd *cobra.Command, args []string) error {
logger := log.NewFactory(logger.With(zap.String("service", "frontend")))
server := frontend.NewServer(
net.JoinHostPort(frontendOptions.serverInterface, strconv.Itoa(frontendOptions.serverPort)),
tracing.Init("frontend", metricsFactory.Namespace("frontend", nil), logger),
logger,
)
return server.Run()
},
}
var (
frontendOptions struct {
serverInterface string
serverPort int
}
)
func init() {
RootCmd.AddCommand(frontendCmd)
frontendCmd.Flags().StringVarP(&frontendOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the frontend server will bind")
frontendCmd.Flags().IntVarP(&frontendOptions.serverPort, "port", "p", 8080, "port on which the frontend server will listen")
}
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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.
package tracing
import (
"fmt"
"time"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-client-go/rpcmetrics"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"github.com/uber/jaeger/examples/hotrod/pkg/log"
)
// Init creates a new instance of Jaeger tracer.
func Init(serviceName string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer {
cfg := config.Configuration{
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &config.ReporterConfig{
LocalAgentHostPort: "docker:5775",
LogSpans: false,
BufferFlushInterval: 1 * time.Second,
},
}
tracer, _, err := cfg.New(
serviceName,
config.Logger(jaegerLoggerAdapter{logger.Bg()}),
config.Observer(rpcmetrics.NewObserver(metricsFactory, rpcmetrics.DefaultNameNormalizer)),
)
if err != nil {
logger.Bg().Fatal("cannot initialize Jaeger Tracer", zap.Error(err))
}
return tracer
}
type jaegerLoggerAdapter struct {
logger log.Logger
}
func (l jaegerLoggerAdapter) Error(msg string) {
l.logger.Error(msg)
}
func (l jaegerLoggerAdapter) Infof(msg string, args ...interface{}) {
l.logger.Info(fmt.Sprintf(msg, args...))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment