Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ A collection of runnable Dubbo-go examples covering configuration, registries, o
* `java_interop`: Demonstrates interoperability between Java and Go Dubbo implementations.
* `llm`: Example of integrating Large Language Models (LLMs) with Dubbo-go.
* `logger`: Logging examples for Dubbo-go applications.
* `logger/default`: Print to console by default.
* `logger/level`: Set log isolation level.
* `logger/rolling`: Output to file.
* `logger/custom`: Custom logger.
* `logger/trace-integration`: Integrate OpenTelemetry trace information, automatically injecting trace_id, span_id and other information into logs.
* `metrics`: Shows how to collect and expose metrics from Dubbo-go services, supporting both Prometheus Push and Pull modes. Also includes the `pgw-cleaner` tool for cleaning zombie metrics in Push mode.
* `mesh`: Proxy-based service mesh example showing how to deploy Dubbo-go services with Envoy on Kubernetes.
* `online_boutique`: Microservices “online boutique” demo built with Dubbo-go.
Expand Down
5 changes: 5 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
* `java_interop`:展示 Java 与 Go Dubbo 实现之间的互操作能力。
* `llm`:将大模型(LLM)集成到 Dubbo-go 中的示例。
* `logger`:Dubbo-go 应用的日志使用示例。
* `logger/default`:默认打印到控制台。
* `logger/level`:设置日志隔离级别。
* `logger/rolling`:输出到文件。
* `logger/custom`:自定义 logger。
* `logger/trace-integration`:集成 OpenTelemetry trace 信息,自动将 trace_id、span_id 等信息注入日志。
* `metrics`:展示如何采集并暴露 Dubbo-go 服务的指标,支持 Prometheus Push 和 Pull 两种模式;同时包含用于清理 Push 模式僵尸指标的 `pgw-cleaner` 工具。
* `mesh`:基于代理的服务网格示例,展示如何在 Kubernetes 上结合 Envoy 部署 Dubbo-go 服务。
* `online_boutique`:基于 Dubbo-go 构建的微服务 “在线商城” 演示项目。
Expand Down
52 changes: 52 additions & 0 deletions logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The samples demonstrate how to configure dubbo-go logger using lumberjack
* level: set the isolation level of the log
* rolling: output to file
* custom: set custom logger
* trace-integration: integrate OpenTelemetry trace information

#### print to the console by default

Expand All @@ -26,6 +27,9 @@ zap log format and level settings
max-age: 3
max-backups: 5
compress: false
trace-integration:
enabled: false
record-error-to-span: false
```

#### set isolation level
Expand Down Expand Up @@ -74,3 +78,51 @@ Then call SetLogger method to set logger
```go
logger.SetLogger(&customLogger{})
```

#### Integrate OpenTelemetry trace information

logger supports OpenTelemetry trace integration, automatically injecting trace information into logs.

##### Method 1: New API (Recommended)

```go
ins, err := dubbo.NewInstance(
dubbo.WithProtocol(
protocol.WithTriple(),
protocol.WithPort(20000),
),
dubbo.WithLogger(
log.WithZap(),
log.WithTraceIntegration(true),
log.WithRecordErrorToSpan(true),
),
)
```

##### Method 2: Old API

```go
loggerConfig := config.NewLoggerConfigBuilder().
SetDriver("zap").
SetTraceIntegrationEnabled(true).
SetRecordErrorToSpan(true).
Build()
loggerConfig.Init()
```

##### Using CtxLogger

```go
rawLogger := gostLogger.GetLogger()
ctxLog := rawLogger.(logger.CtxLogger)

ctxLog.CtxInfo(ctx, "hello dubbogo this is info log")
ctxLog.CtxDebug(ctx, "hello dubbogo this is debug log")
ctxLog.CtxWarn(ctx, "hello dubbogo this is warn log")
ctxLog.CtxError(ctx, "hello dubbogo this is error log")

ctxLog.CtxInfof(ctx, "user: %s", "alice")
ctxLog.CtxDebugf(ctx, "value: %d", 42)
ctxLog.CtxWarnf(ctx, "latency: %dms", 150)
ctxLog.CtxErrorf(ctx, "failed: %v", err)
```
54 changes: 53 additions & 1 deletion logger/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* level: 设置日志的隔离级别
* rolling: 输出到文件
* custom: 自定义 logger
* trace-integration: 集成 OpenTelemetry trace 信息

#### 默认配置

Expand All @@ -26,6 +27,9 @@ zap 日志格式和级别设置
max-age: 3
max-backups: 5
compress: false
trace-integration:
enabled: false
record-error-to-span: false
```

#### 设置隔离级别
Expand Down Expand Up @@ -68,4 +72,52 @@ type Logger interface {
然后调用 SetLogger 方法设置 logger
```go
logger.SetLogger(&customLogger{})
```
```

#### 集成 OpenTelemetry trace 信息

logger 支持 OpenTelemetry trace 集成,自动将 trace 信息注入日志。

##### 方式1:New API(推荐)

```go
ins, err := dubbo.NewInstance(
dubbo.WithProtocol(
protocol.WithTriple(),
protocol.WithPort(20000),
),
dubbo.WithLogger(
log.WithZap(),
log.WithTraceIntegration(true),
log.WithRecordErrorToSpan(true),
),
)
```

##### 方式2:Old API

```go
loggerConfig := config.NewLoggerConfigBuilder().
SetDriver("zap").
SetTraceIntegrationEnabled(true).
SetRecordErrorToSpan(true).
Build()
loggerConfig.Init()
```

##### 使用 CtxLogger 记录日志

```go
rawLogger := gostLogger.GetLogger()
ctxLog := rawLogger.(logger.CtxLogger)

ctxLog.CtxInfo(ctx, "hello dubbogo this is info log")
ctxLog.CtxDebug(ctx, "hello dubbogo this is debug log")
ctxLog.CtxWarn(ctx, "hello dubbogo this is warn log")
ctxLog.CtxError(ctx, "hello dubbogo this is error log")

ctxLog.CtxInfof(ctx, "user: %s", "alice")
ctxLog.CtxDebugf(ctx, "value: %d", 42)
ctxLog.CtxWarnf(ctx, "latency: %dms", 150)
ctxLog.CtxErrorf(ctx, "failed: %v", err)
```
81 changes: 81 additions & 0 deletions logger/trace-integration/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 main

import (
"context"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"

"dubbo.apache.org/dubbo-go/v3"
_ "dubbo.apache.org/dubbo-go/v3/imports"
log "dubbo.apache.org/dubbo-go/v3/logger"
"dubbo.apache.org/dubbo-go/v3/protocol"

"github.com/dubbogo/gost/log/logger"
)

func main() {
ins, err := dubbo.NewInstance(
dubbo.WithProtocol(
protocol.WithTriple(),
protocol.WithPort(20000),
),
dubbo.WithLogger(
log.WithZap(),
log.WithLevel("debug"),
log.WithTraceIntegration(true),

Check failure on line 44 in logger/trace-integration/cmd/main.go

View workflow job for this annotation

GitHub Actions / lint (1.24)

undefined: log.WithTraceIntegration
log.WithRecordErrorToSpan(true),

Check failure on line 45 in logger/trace-integration/cmd/main.go

View workflow job for this annotation

GitHub Actions / lint (1.24)

undefined: log.WithRecordErrorToSpan
),
)
if err != nil {
panic(err)
}

server, err := ins.NewServer()
if err != nil {
panic(err)
}
go server.Serve()

// setup OpenTelemetry tracer
tp := trace.NewTracerProvider()
otel.SetTracerProvider(tp)
tracer := tp.Tracer("demo")

traceCtx, span := tracer.Start(context.Background(), "demo-operation")
defer span.End()
rawLogger := logger.GetLogger()
ctxLog := rawLogger.(log.CtxLogger)

Check failure on line 66 in logger/trace-integration/cmd/main.go

View workflow job for this annotation

GitHub Actions / lint (1.24)

undefined: log.CtxLogger (typecheck)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
for {
select {
case <-ctx.Done():
return
default:
ctxLog.CtxInfo(traceCtx, "hello dubbogo this is info log")
ctxLog.CtxDebug(traceCtx, "hello dubbogo this is debug log")
ctxLog.CtxWarn(traceCtx, "hello dubbogo this is warn log")
ctxLog.CtxError(traceCtx, "hello dubbogo this is error log")
time.Sleep(time.Second * 1)
}
}
}
Loading