Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chenchao dev #424

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions examples/OpentelemetryServer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module OpentelemetryServer
go 1.19

require (
github.com/TarsCloud/TarsGo v1.3.10
github.com/TarsCloud/TarsGo v1.4.4
github.com/TarsCloud/TarsGo/contrib/middleware/opentelemetry v0.0.0
github.com/prometheus/client_golang v1.14.0
go.opentelemetry.io/otel v1.15.0-rc.1
Expand Down Expand Up @@ -52,7 +52,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/otel/metric v1.15.0-rc.1 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/automaxprocs v1.5.1 // indirect
go.uber.org/automaxprocs v1.5.2 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand Down
92 changes: 92 additions & 0 deletions examples/OpentelemetryServer/tracer/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package tracer

import (
"context"
"log"
"os"
"sync"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/exporters/zipkin"
"go.opentelemetry.io/otel/propagation"
sdkresource "go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)

var resource *sdkresource.Resource
var initResourcesOnce sync.Once

func initResource(serviceName string) *sdkresource.Resource {
initResourcesOnce.Do(func() {
extraResources, _ := sdkresource.New(
context.Background(),
sdkresource.WithOS(),
sdkresource.WithProcess(),
sdkresource.WithContainer(),
sdkresource.WithHost(),
sdkresource.WithFromEnv(),
sdkresource.WithAttributes(semconv.ServiceName(serviceName)),
)
resource, _ = sdkresource.Merge(
sdkresource.Default(),
extraResources,
)
})
return resource
}

func newOtlpExporter() (sdktrace.SpanExporter, error) {
ctx := context.Background()
return otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure())
}

func newStdoutExporter() (sdktrace.SpanExporter, error) {
return stdouttrace.New(
// Use human-readable output.
stdouttrace.WithPrettyPrint(),
// Do not print timestamps for the demo.
stdouttrace.WithoutTimestamps(),
)
}

func newZipkinExporter(serviceNameKey string) (sdktrace.SpanExporter, error) {
url := "http://localhost:19411/api/v2/spans"
return zipkin.New(url, zipkin.WithLogger(log.New(os.Stderr, serviceNameKey, log.Ldate|log.Ltime|log.Llongfile)))
}

func newJaegerExporter() (sdktrace.SpanExporter, error) {
url := "http://localhost:14268/api/traces"
return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
}

func NewTracerProvider(serviceName, exporterTyp string) *sdktrace.TracerProvider {
var (
exporter sdktrace.SpanExporter
err error
)
switch exporterTyp {
case "stdout":
exporter, err = newStdoutExporter()
case "zipkin":
exporter, err = newZipkinExporter(serviceName)
case "jaeger":
exporter, err = newJaegerExporter()
default: // otlp
exporter, err = newOtlpExporter()
}
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
//sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(0.5))), // 控制采样
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(initResource(serviceName)),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}
95 changes: 95 additions & 0 deletions examples/TarsPushServer/client/DemoClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"TarPushServer/demo"
"fmt"
"github.com/TarsCloud/TarsGo/tars"
"time"
)

var prx *demo.DemoObj

type Callback struct {
start int64
cost int64
count int64
}

func (c *Callback) Reg_Callback(ret *demo.Result, rsp *demo.RegRsp, opt ...map[string]string) {
//TODO implement me
panic("implement me")
}

func (c *Callback) Notify_Callback(ret *demo.Result, opt ...map[string]string) {
//TODO implement me
panic("implement me")
}

func (c *Callback) Notify_ExceptionCallback(err error) {
//TODO implement me
panic("implement me")
}

func (c *Callback) Push_Callback(msg *string, opt ...map[string]string) {
/* if c.count == 0 {
c.start = time.Now().UnixMicro()
}
c.count++
if c.count == 500000 {
c.cost = time.Now().UnixMicro() - c.start
fmt.Printf("cost--->%vus\n", c.cost)
}*/

//if c.start == 0 {
// c.start = time.Now().UnixMicro()
//} else {
// c.cost = time.Now().UnixMicro() - c.start
//}
//fmt.Printf("cost--->%vus|Push---->:%s======%v\n", c.cost, *msg, opt)
fmt.Printf("%v|Push---->:%s======%v\n", time.Now().UnixMilli(), *msg, opt)
}

func (c Callback) Push_ExceptionCallback(err error) {
panic("implement me")
}

func (c Callback) Reg_ExceptionCallback(err error) {
//TODO implement me
panic("implement me")
}

func TestReg() {
req := &demo.RegReq{Msg: "reg"}
rsp := &demo.RegRsp{}
prx.Reg(req, rsp)
}

func main() {
com := tars.GetCommunicator()
obj := "Base.DemoServer.DemoObj@tcp -h 127.0.0.1 -p 8888 -t 60000"
prx = &demo.DemoObj{}
com.StringToProxy(obj, prx)
prx.SetOnConnectCallback(func(s string) {
fmt.Println("<-----------onConnect--------->")
TestReg()
})
prx.SetOnCloseCallback(func(s string) {
fmt.Println("<-----------onClose----------->")
})
TarsCb := new(Callback)
prx.SetTarsCallback(TarsCb)
go func() {
ticker := time.NewTicker(time.Second * 10)
defer ticker.Stop()
for {
select {
case <-ticker.C:
prx.TarsPing()
}
}
}()
prx.TarsPing()
for true {
time.Sleep(time.Second * 10)
}
}
30 changes: 30 additions & 0 deletions examples/TarsPushServer/demo.tars
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module demo
{
struct Result
{
0 optional int code;
1 optional string msg;
};

struct RegReq
{
0 optional string msg;
};

struct RegRsp
{
0 optional string msg;
};

struct Notify
{
0 optional string msg;
};

interface DemoObj
{
void Push(out string msg);
Result Reg(RegReq req, out RegRsp rsp);
Result Notify(Notify notify);
};
};
Loading