-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
144 lines (115 loc) · 3.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// similar to https://github.com/MrAlias/otlpr/blob/main/example/main.go
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"strconv"
"strings"
"time"
"go.opentelemetry.io/otel/sdk/resource"
sdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
zapotlp "github.com/SigNoz/zap_otlp"
zapotlpencoder "github.com/SigNoz/zap_otlp/zap_otlp_encoder"
zapotlpsync "github.com/SigNoz/zap_otlp/zap_otlp_sync"
)
var targetPtr = flag.String("target", "127.0.0.1:4317", "OTLP target")
var grpcInsecure = os.Getenv("OTEL_EXPORTER_OTLP_INSECURE")
const (
lib = "github.com/SigNoz/zap_otlp/example"
libVer = "v0.1.0"
)
type App struct {
logger *zap.Logger
tracer trace.Tracer
}
func NewApp(tracer trace.Tracer, logger *zap.Logger) App {
return App{tracer: tracer, logger: logger}
}
func (a App) Hello(ctx context.Context, user string) error {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "Hello")
defer span.End()
data := map[string]string{
"hello": "world",
}
a.logger.Info("unamed: hello from the function to user: "+user, zap.Any("rand struct",
struct {
Field1 string
Field2 int
}{
Field1: "asadadas",
Field2: 10,
}), zap.Any("test", data), zap.String("user", user), zapotlp.SpanCtx(ctx), zap.Duration("duration", time.Second*2))
a.logger.Named("my").Info("my1: hello from the function to user: "+user, zap.String("user", user), zapotlp.SpanCtx(ctx), zap.Duration("duration", time.Second*2))
a.logger.Named("my1").Info("my2: hello from the function to user: "+user, zap.String("user", user), zapotlp.SpanCtx(ctx), zap.Duration("duration", time.Second*2))
return nil
}
func setup(ctx context.Context, conn *grpc.ClientConn) (trace.Tracer, *zap.Logger, *zapotlpsync.OtelSyncer, error) {
// exp, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
// if err != nil {
// return nil, zap.NewNop(), err
// }
res := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("example application"),
)
// Use a syncer for demo purposes only.
// add sdk.WithSyncer(exp) for exporting traces
tp := sdk.NewTracerProvider(sdk.WithResource(res))
tracer := tp.Tracer(lib, trace.WithInstrumentationVersion(libVer))
config := zap.NewProductionEncoderConfig()
config.EncodeDuration = zapcore.StringDurationEncoder
otlpEncoder := zapotlpencoder.NewOTLPEncoder(config)
consoleEncoder := zapcore.NewConsoleEncoder(config)
defaultLogLevel := zapcore.DebugLevel
otlpSync := zapotlpsync.NewOtlpSyncer(conn, zapotlpsync.Options{
BatchSize: 2,
ResourceSchema: semconv.SchemaURL,
Resource: res,
})
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, os.Stdout, defaultLogLevel),
zapcore.NewCore(otlpEncoder, otlpSync, defaultLogLevel),
)
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
return tracer, logger, otlpSync, nil
}
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
var secureOption grpc.DialOption
if strings.ToLower(grpcInsecure) == "false" || grpcInsecure == "0" || strings.ToLower(grpcInsecure) == "f" {
secureOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))
} else {
secureOption = grpc.WithTransportCredentials(insecure.NewCredentials())
}
conn, err := grpc.DialContext(ctx, *targetPtr, grpc.WithBlock(), secureOption, grpc.WithTimeout(time.Duration(5)*time.Second))
if err != nil {
log.Fatal(err)
}
tracer, logger, otlpSync, err := setup(ctx, conn)
if err != nil {
log.Fatal(err)
}
// close the syncer before shutdown
defer otlpSync.Close()
var span trace.Span
ctx, span = tracer.Start(ctx, "main")
defer span.End()
app := NewApp(tracer, logger)
for i := 0; i < 1; i++ {
time.Sleep(1 * time.Second)
app.Hello(ctx, strconv.Itoa(i)+"user: xyz")
}
logger.Sync()
}