-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (105 loc) · 3.2 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
// Copyright 2022 Tyler Yahn (MrAlias)
//
// Licensed 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"
"errors"
"flag"
"log"
"os"
"os/signal"
"time"
"github.com/MrAlias/otlpr"
"github.com/go-logr/logr"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/instrumentation"
"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"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
lib = "github.com/MrAlias/otlpr/example"
libVer = "v0.1.0"
)
var targetPtr = flag.String("target", "127.0.0.1:4317", "OTLP target")
type App struct {
tracer trace.Tracer
logger logr.Logger
}
func NewApp(tracer trace.Tracer, logger logr.Logger) App {
return App{tracer: tracer, logger: logger}
}
// Hello logs a greeting to user.
func (a App) Hello(ctx context.Context, user string) error {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "Hello")
defer span.End()
if user == "" {
span.SetStatus(codes.Error, "invalid user")
return errors.New("no user name provided")
}
otlpr.WithContext(a.logger, ctx).Info("Hello!", "user", user)
return nil
}
func setup(ctx context.Context, conn *grpc.ClientConn) (trace.Tracer, logr.Logger, error) {
exp, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
return nil, logr.Discard(), err
}
res := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("example application"),
)
// Use a syncer for demo purposes only.
tp := sdk.NewTracerProvider(sdk.WithSyncer(exp), sdk.WithResource(res))
tracer := tp.Tracer(lib, trace.WithInstrumentationVersion(libVer))
l := otlpr.NewWithOptions(conn, otlpr.Options{
LogCaller: otlpr.All,
LogCallerFunc: true,
Batcher: otlpr.Batcher{Messages: 2, Timeout: 3 * time.Second},
})
l = otlpr.WithResource(l, res)
scope := instrumentation.Scope{Name: lib, Version: libVer}
l = otlpr.WithScope(l, scope)
return tracer, l, nil
}
func main() {
flag.Parse()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
conn, err := grpc.NewClient(
*targetPtr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatal(err)
}
tracer, logger, err := setup(ctx, conn)
if err != nil {
log.Fatal(err)
}
var span trace.Span
ctx, span = tracer.Start(ctx, "main")
defer span.End()
app := NewApp(tracer, logger)
for _, user := range []string{"alice", ""} {
if err := app.Hello(ctx, user); err != nil {
logger.Error(err, "failed to say hello", "user", user, "testing", true)
}
}
}