Skip to content

Commit

Permalink
Merge pull request #65 from lyt122/fix-issue-54
Browse files Browse the repository at this point in the history
Feature:add Jaeger to provide distributed tracing
  • Loading branch information
CocaineCong authored Feb 26, 2024
2 parents 854026c + 043a7c9 commit c179e10
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 25 deletions.
22 changes: 18 additions & 4 deletions app/favorite/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package main

import (
"context"
"net"

"github.com/pkg/errors"
"github.com/CocaineCong/tangseng/pkg/tracing"

"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/pkg/errors"

"github.com/CocaineCong/tangseng/app/favorite/internal/service"
"github.com/CocaineCong/tangseng/config"
Expand All @@ -32,6 +32,9 @@ import (
"github.com/CocaineCong/tangseng/loading"
"github.com/CocaineCong/tangseng/pkg/discovery"
logs "github.com/CocaineCong/tangseng/pkg/logger"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
)

func main() {
Expand All @@ -46,7 +49,18 @@ func main() {
Name: config.Conf.Domain[consts.FavoriteServiceName].Name,
Addr: grpcAddress,
}
server := grpc.NewServer()
//注册tracer
provider := tracing.InitTracerProvider(config.Conf.Jaeger.Addr, consts.FavoriteServiceName)
defer func() {
if provider == nil {
return
}
if err := provider(context.Background()); err != nil {
logs.LogrusObj.Errorf("Failed to shutdown: %v", err)
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
defer server.Stop()
// 绑定service
favoritePb.RegisterFavoritesServiceServer(server, service.GetFavoriteSrv())
Expand Down
16 changes: 16 additions & 0 deletions app/gateway/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
package main

import (
"context"
"fmt"
"net/http"
"time"

logs "github.com/CocaineCong/tangseng/pkg/logger"

"github.com/CocaineCong/tangseng/consts"
"github.com/CocaineCong/tangseng/pkg/tracing"

"github.com/sirupsen/logrus"
"google.golang.org/grpc/resolver"

Expand All @@ -35,6 +41,16 @@ import (
func main() {
loading.Loading()
rpc.Init()
////注册tracer
provider := tracing.InitTracerProvider(config.Conf.Jaeger.Addr, consts.ServiceName)
defer func() {
if provider == nil {
return
}
if err := provider(context.Background()); err != nil {
logs.LogrusObj.Errorf("Failed to shutdown: %v", err)
}
}()
// etcd注册
etcdAddress := []string{config.Conf.Etcd.Address}
etcdRegister := discovery.NewResolver(etcdAddress, logrus.New())
Expand Down
4 changes: 3 additions & 1 deletion app/gateway/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import (

"github.com/CocaineCong/tangseng/app/gateway/http"
"github.com/CocaineCong/tangseng/app/gateway/middleware"
"github.com/CocaineCong/tangseng/consts"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

func NewRouter() *gin.Engine {
r := gin.Default()
r.Use(middleware.Cors(), middleware.ErrorMiddleware())
r.Use(middleware.Cors(), middleware.ErrorMiddleware(), otelgin.Middleware(consts.ServiceName))
store := cookie.NewStore([]byte("something-very-secret"))
r.Use(sessions.Sessions("mysession", store))
v1 := r.Group("/api/v1")
Expand Down
16 changes: 15 additions & 1 deletion app/index_platform/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

"github.com/pkg/errors"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -55,7 +58,18 @@ func main() {
Name: config.Conf.Domain[consts.IndexPlatformName].Name,
Addr: grpcAddress,
}
server := grpc.NewServer()
//注册tracer
provider := tracing.InitTracerProvider(config.Conf.Jaeger.Addr, consts.IndexPlatformName)
defer func() {
if provider == nil {
return
}
if err := provider(context.Background()); err != nil {
logs.LogrusObj.Errorf("Failed to shutdown: %v", err)
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
defer server.Stop()

index_platform.RegisterIndexPlatformServiceServer(server, service.GetIndexPlatformSrv())
Expand Down
16 changes: 15 additions & 1 deletion app/search_engine/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

logs "github.com/CocaineCong/tangseng/pkg/logger"
"github.com/pkg/errors"

Expand Down Expand Up @@ -56,7 +59,18 @@ func main() {
Name: config.Conf.Domain[consts.SearchServiceName].Name,
Addr: grpcAddress,
}
server := grpc.NewServer()
//注册tracer
provider := tracing.InitTracerProvider(config.Conf.Jaeger.Addr, consts.SearchServiceName)
defer func() {
if provider == nil {
return
}
if err := provider(context.Background()); err != nil {
logs.LogrusObj.Errorf("Failed to shutdown: %v", err)
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
defer server.Stop()
// 绑定service
pb.RegisterSearchEngineServiceServer(server, service.GetSearchEngineSrv())
Expand Down
17 changes: 16 additions & 1 deletion app/user/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
package main

import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

logs "github.com/CocaineCong/tangseng/pkg/logger"
"github.com/pkg/errors"

Expand All @@ -46,7 +50,18 @@ func main() {
Name: config.Conf.Domain[consts.UserServiceName].Name,
Addr: grpcAddress,
}
server := grpc.NewServer()
//注册tracer
provider := tracing.InitTracerProvider(config.Conf.Jaeger.Addr, consts.UserServiceName)
defer func() {
if provider == nil {
return
}
if err := provider(context.Background()); err != nil {
logs.LogrusObj.Errorf("Failed to shutdown: %v", err)
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
defer server.Stop()
// 绑定service
pb.RegisterUserServiceServer(server, service.GetUserSrv())
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type Config struct {
StarRocks *StarRocks `yaml:"starrock"`
Vector *VectorConfig `yaml:"vector"`
Milvus *MilvusConfig `yaml:"milvus"`
Jaeger *Jaeger `yaml:"jaeger"`
}

type Jaeger struct {
Addr string `yaml:"addr"`
}

type VectorConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ milvus:
default_milvus_table_name: milvus_table_name
metric_type: L2
timeout: 3
jaeger:
addr: 127.0.0.1:4317

3 changes: 3 additions & 0 deletions config/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,6 @@ milvus:
metric_type: L2
timeout: 3

jaeger:
addr: 127.0.0.1:4317

22 changes: 22 additions & 0 deletions consts/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF 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 consts

const (
ServiceName = "tangseng"
)
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ services:
image: jaegertracing/all-in-one:latest
ports:
- "16686:16686"
- "4317:4317"
restart: always
networks:
- search_engine
Expand Down
7 changes: 2 additions & 5 deletions pkg/clone/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"context"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"
)

Expand All @@ -43,9 +41,8 @@ func NewContextWithoutDeadline() *ContextWithoutDeadline {
}

func (c *ContextWithoutDeadline) Clone(ctx context.Context, keys ...interface{}) {
_, span := otel.GetTracerProvider().Tracer("tangseng"+"/clone").
Start(ctx, "clone", oteltrace.WithAttributes(attribute.Int("sync", 1)))
defer span.End()

span := oteltrace.SpanFromContext(ctx)

c.ctx = oteltrace.ContextWithSpan(c.ctx, span)

Expand Down
31 changes: 20 additions & 11 deletions pkg/ctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
package ctl

import (
"time"

"github.com/CocaineCong/tangseng/pkg/tracing"
"github.com/gin-gonic/gin"

e2 "github.com/CocaineCong/tangseng/consts/e"
)

// Response 基础序列化器
type Response struct {
Status int `json:"status"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Error string `json:"error"`
Status int `json:"status"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Error string `json:"error"`
Timestamp int64 `json:"timestamps"`
TraceID string `json:"traceID"`
}

// RespSuccess 带data成功返回
Expand All @@ -43,9 +48,11 @@ func RespSuccess(ctx *gin.Context, data interface{}, code ...int) *Response {
}

r := &Response{
Status: status,
Data: data,
Msg: e2.GetMsg(status),
Status: status,
Data: data,
Msg: e2.GetMsg(status),
Timestamp: time.Now().Unix(),
TraceID: tracing.GetTraceID(ctx),
}

return r
Expand All @@ -58,10 +65,12 @@ func RespError(ctx *gin.Context, err error, data string, code ...int) *Response
}

r := &Response{
Status: status,
Data: data,
Msg: e2.GetMsg(status),
Error: err.Error(),
Status: status,
Data: data,
Msg: e2.GetMsg(status),
Error: err.Error(),
Timestamp: time.Now().Unix(),
TraceID: tracing.GetTraceID(ctx),
}

return r
Expand Down
Loading

0 comments on commit c179e10

Please sign in to comment.