-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.go
More file actions
64 lines (56 loc) · 2.15 KB
/
start.go
File metadata and controls
64 lines (56 loc) · 2.15 KB
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
package telemetry
import (
"context"
"errors"
"log"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
)
type Config struct {
EnableLogger bool
PackageName string
}
func Start(ctx context.Context, cfg Config) func() {
resources, err := DetectResources(ctx)
if err != nil {
log.Fatalf("Error initializing OTEL resources: %s\n", err)
}
otel.SetTextMapPropagator(DetectPropagator(ctx))
shutdownTracer := StartTracer(ctx, resources)
shutdownMetrics := StartMetrics(ctx, resources)
shutdownLogger := StartLogger(ctx, resources, cfg)
return func() {
shutdownLogger(ctx)
shutdownMetrics(ctx)
shutdownTracer(ctx)
}
}
func DetectResources(ctx context.Context) (*resource.Resource, error) {
res, err := resource.New(
ctx,
resource.WithDetectors(NullstoneResourceDetector{}), // Nullstone resource detection
resource.WithFromEnv(), // Discover and provide attributes from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables.
resource.WithTelemetrySDK(), // Discover and provide information about the OpenTelemetry SDK used.
resource.WithProcess(), // Discover and provide process information.
resource.WithOS(), // Discover and provide OS information.
resource.WithContainer(), // Discover and provide container information.
resource.WithHost(), // Discover and provide host information.
//resource.WithAttributes(attribute.String("foo", "bar")), // Add custom resource attributes.
)
if errors.Is(err, resource.ErrPartialResource) || errors.Is(err, resource.ErrSchemaURLConflict) {
// Log non-fatal issues
log.Printf("Error initializing detecting OTEL resources: %s\n", err)
return res, nil
} else if err != nil {
// This is a fatal issue and should fail the main program
return nil, err
}
return res, nil
}
func DetectPropagator(ctx context.Context) propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
)
}