Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Add go-micro plugins #4

Merged
merged 29 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
name: Build & Test
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.12
uses: actions/setup-go@v1
- name: Set up Go 1.13
uses: actions/setup-go@v2
with:
go-version: 1.12
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand All @@ -58,4 +58,4 @@ jobs:
uses: actions/checkout@v2

- name: Check license header
run: make license
run: make license
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ deps:

LINTER := bin/golangci-lint
$(LINTER):
curl -L https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.20.1
curl -L https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.31.0

.PHONY: lint
lint: $(LINTER)
Expand Down
89 changes: 89 additions & 0 deletions micro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Go2sky with go-micro (v1.18)

## Applicable version
<= v1.18

go-micro v2.0 had changed a lot and is not compatible with go2sky, so only 1.x version is supported.

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

## Usage
```go
package main

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

"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/reporter"
"github.com/micro/go-micro"
"github.com/micro/go-micro/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 := micro.NewService(
micro.Name("greeter"),
//Use go2sky middleware with tracing
micro.WrapHandler(NewHandlerWrapper(tracer, "User-Agent")),
)
// initialise command line
// set the handler
if err := micro.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 := micro.NewService(
micro.Name("micro_client"),
//Use go2sky middleware with tracing
micro.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()
}
```
19 changes: 19 additions & 0 deletions micro/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to SkyAPM org under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. SkyAPM org licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Package micro is a plugin that can be used to trace Go-micro framework.
package micro
92 changes: 92 additions & 0 deletions micro/example_micro_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to SkyAPM org under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. SkyAPM org licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package micro

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

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

type Greeter struct{}

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

func ExampleNewHandlerWrapper() {
//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 := micro.NewService(
micro.Name("greeter"),
//Use go2sky middleware with tracing
micro.WrapHandler(NewHandlerWrapper(tracer, "User-Agent")),
)
// initialise command line
// set the handler
if err := micro.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 := micro.NewService(
micro.Name("micro_client"),
//Use go2sky middleware with tracing
micro.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:
}
10 changes: 10 additions & 0 deletions micro/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/SkyAPM/go2sky-plugins/micro

go 1.13

require (
github.com/SkyAPM/go2sky v0.5.0
github.com/micro/go-micro v1.18.0
)

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
Loading