micro

package module
v0.0.0-...-1db7dc6 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

Go2sky with go-micro (v3.5.0)

Installation

go get -u github.com/SkyAPM/go2sky-plugins/micro

Usage

package main

import (
	"context"
	"log"
	"sync"
	"time"

	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
	microv3 "github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/client"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, name *string, msg *string) error {
	*msg = "Hello " + *name
	return nil
}

func main() {
	//Use gRPC reporter for production
	r, err := reporter.NewLogReporter()
	if err != nil {
		log.Fatalf("new reporter error %v \n", err)
	}
	defer r.Close()

	tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r))
	if err != nil {
		log.Fatalf("create tracer error %v \n", err)
	}

	go func() {
		//create test server
		service := microv3.NewService(
			microv3.Name("greeter"),
			//Use go2sky middleware with tracing
			microv3.WrapHandler(NewHandlerWrapper(tracer, "User-Agent")),
		)
		// initialise command line
		// set the handler
		if err := microv3.RegisterHandler(service.Server(), new(Greeter)); err != nil {
			log.Fatalf("Registe service error: %v \n", err)
		}

		// run service
		if err := service.Run(); err != nil {
			log.Fatalf("Run server error: %v \n", err)
		}
	}()
	// wait server to start
	time.Sleep(time.Second * 5)

	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		cli := microv3.NewService(
			microv3.Name("micro_client"),
			//Use go2sky middleware with tracing
			microv3.WrapClient(NewClientWrapper(tracer, WithClientWrapperReportTags("Micro-From-Service"))),
		)
		c := cli.Client()
		request := c.NewRequest("greeter", "Greeter.Hello", "john", client.WithContentType("application/json"))
		var response string
		if err := c.Call(context.TODO(), request, &response); err != nil {
			log.Fatalf("call service err %v \n", err)
		}
		log.Printf("reseponse: %v \n", response)
	}()
	wg.Wait()
}

See more.

Documentation

Overview

Package micro is a plugin that can be used to trace Go-micro framework.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCallWrapper

func NewCallWrapper(sw *go2sky.Tracer, reportTags ...string) client.CallWrapper

NewCallWrapper accepts an go2sky Tracer and returns a Call Wrapper

func NewClientWrapper

func NewClientWrapper(sw *go2sky.Tracer, options ...ClientOption) client.Wrapper

NewClientWrapper accepts a go2sky Tracer and returns a Client Wrapper

func NewHandlerWrapper

func NewHandlerWrapper(sw *go2sky.Tracer, reportTags ...string) server.HandlerWrapper

NewHandlerWrapper accepts a go2sky Tracer and returns a Subscriber Wrapper

Example
package main

import (
	"context"
	"log"
	"sync"
	"time"

	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
	microv3 "github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/client"
	"github.com/asim/go-micro/v3/logger"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, name *string, msg *string) error {
	*msg = "Hello " + *name
	return nil
}

func main() {
	//Use gRPC reporter for production
	r, err := reporter.NewLogReporter()
	if err != nil {
		log.Fatalf("new reporter error %v \n", err)
	}
	defer r.Close()

	tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r))
	if err != nil {
		log.Fatalf("create tracer error %v \n", err)
	}

	go func() {
		//create test server
		service := microv3.NewService(
			microv3.Name("greeter"),
			//Use go2sky middleware with tracing
			microv3.WrapHandler(NewHandlerWrapper(tracer, "User-Agent")),
		)
		_ = logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel))
		// initialise command line
		// set the handler
		if err := microv3.RegisterHandler(service.Server(), new(Greeter)); err != nil {
			log.Fatalf("Registe service error: %v \n", err)
		}

		// run service
		if err := service.Run(); err != nil {
			log.Fatalf("Run server error: %v \n", err)
		}
	}()
	// wait server to start
	time.Sleep(time.Second * 5)

	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		cli := microv3.NewService(
			microv3.Name("micro_client"),
			//Use go2sky middleware with tracing
			microv3.WrapClient(NewClientWrapper(tracer, WithClientWrapperReportTags("Micro-From-Service"))),
		)
		c := cli.Client()
		request := c.NewRequest("greeter", "Greeter.Hello", "john", client.WithContentType("application/json"))
		var response string
		if err := c.Call(context.TODO(), request, &response); err != nil {
			log.Fatalf("call service err %v \n", err)
		}
		log.Printf("reseponse: %v \n", response)
	}()
	wg.Wait()
}
Output:

func NewSubscriberWrapper

func NewSubscriberWrapper(sw *go2sky.Tracer, reportTags ...string) server.SubscriberWrapper

NewSubscriberWrapper accepts a go2sky Tracer and returns a Handler Wrapper

Types

type ClientOption

type ClientOption func(*clientWrapper)

ClientOption allow optional configuration of Client

func WithClientWrapperReportTags

func WithClientWrapperReportTags(reportTags ...string) ClientOption

WithClientWrapperReportTags customize span tags

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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