You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.7 KiB
113 lines
2.7 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"os"
|
|
"time"
|
|
|
|
v1 "github.com/go-kratos/kratos/examples/blog/api/blog/v1"
|
|
"github.com/go-kratos/kratos/examples/blog/internal/conf"
|
|
"github.com/go-kratos/kratos/v2"
|
|
"github.com/go-kratos/kratos/v2/config"
|
|
"github.com/go-kratos/kratos/v2/config/file"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
|
"github.com/go-kratos/kratos/v2/transport/http"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/exporters/trace/jaeger"
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
|
"go.opentelemetry.io/otel/semconv"
|
|
)
|
|
|
|
// go build -ldflags "-X main.Version=x.y.z"
|
|
var (
|
|
// Name is the name of the compiled software.
|
|
Name string
|
|
// Version is the version of the compiled software.
|
|
Version string
|
|
// flagconf is the config flag.
|
|
flagconf string
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml")
|
|
}
|
|
|
|
func newApp(logger log.Logger, hs *http.Server, gs *grpc.Server) *kratos.App {
|
|
return kratos.New(
|
|
kratos.Name(Name),
|
|
kratos.Version(Version),
|
|
kratos.Metadata(map[string]string{}),
|
|
kratos.Logger(logger),
|
|
kratos.Server(
|
|
hs,
|
|
gs,
|
|
),
|
|
)
|
|
}
|
|
|
|
func tracerProvider(url string) (*tracesdk.TracerProvider, error) {
|
|
// Create the Jaeger exporter
|
|
exp, err := jaeger.NewRawExporter(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tp := tracesdk.NewTracerProvider(
|
|
// Always be sure to batch in production.
|
|
tracesdk.WithBatcher(exp),
|
|
// Record information about this application in an Resource.
|
|
tracesdk.WithResource(resource.NewWithAttributes(
|
|
semconv.ServiceNameKey.String(v1.BlogService_ServiceDesc.ServiceName),
|
|
attribute.String("environment", "development"),
|
|
attribute.Int64("ID", 1),
|
|
)),
|
|
)
|
|
return tp, nil
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
logger := log.NewStdLogger(os.Stdout)
|
|
|
|
config := config.New(
|
|
config.WithSource(
|
|
file.NewSource(flagconf),
|
|
),
|
|
)
|
|
if err := config.Load(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var bc conf.Bootstrap
|
|
if err := config.Scan(&bc); err != nil {
|
|
panic(err)
|
|
}
|
|
tp, err := tracerProvider(bc.Trace.Endpoint)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
defer func(ctx context.Context) {
|
|
// Do not make the application hang when it is shutdown.
|
|
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
|
|
defer cancel()
|
|
if err := tp.Shutdown(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}(ctx)
|
|
|
|
app, cleanup, err := initApp(bc.Server, bc.Data, tp, logger)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer cleanup()
|
|
|
|
// start and wait for stop signal
|
|
if err := app.Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|