Skip to content

Commit 983e3ef

Browse files
Fix OpenTelemetry error handling & update README
1 parent 5c63623 commit 983e3ef

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ response = requests.get("http://example.com", proxies=proxies, headers={"X-Upgra
5656
```
5757
5858
## Telemetry
59-
Chaperone is fully instrumented for OpenTelemetry traces & logs. Although disabled by default, instrumentation can enabled via the [standard OpenTelemetry environment variables](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration).
59+
Chaperone is fully instrumented for OpenTelemetry traces & logs. Although disabled by default, instrumentation can enabled by setting `OTEL_ENABLED` to true and is configured via the [standard OpenTelemetry environment variables](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration).
6060

61-
Logs are always emmitted to stderr, in addition to
61+
Logs are always emmitted to stderr, in addition to the optional OpenTelemetry exporter.

pkg/telemetry/log/logger.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,8 @@ func (l *Logger) With(fields ...string) *Logger {
264264
Writer: l.Writer,
265265
}
266266
}
267+
268+
func (l *Logger) Output(calldepth int, s string) error {
269+
l.Writer.Write([]byte(s))
270+
return nil
271+
}

pkg/telemetry/mod.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,37 @@ package telemetry
33
import (
44
"context"
55

6+
"github.com/KillianMeersman/chaperone/pkg/config"
67
"github.com/KillianMeersman/chaperone/pkg/telemetry/log"
78
"github.com/KillianMeersman/chaperone/pkg/telemetry/trace"
9+
"github.com/go-logr/stdr"
10+
"go.opentelemetry.io/otel"
811
"go.opentelemetry.io/otel/sdk/resource"
912
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
1013
)
1114

1215
const SERVICE_NAME = "chaperone"
1316
const SERVICE_VERSION = "0.0.3"
1417

18+
type telemetryErrorHandler struct{}
19+
20+
func (h *telemetryErrorHandler) Handle(err error) {
21+
log.Error(context.Background(), err)
22+
}
23+
1524
// Initialize telemetry components.
1625
// This includes tracing, logging, and metrics.
1726
// It is recommended to call this function at the start of your application.
1827
// The context should be cancelled when the application is shutting down to ensure proper cleanup.
1928
func InitTelemetry(ctx context.Context, name, version string) {
29+
if !config.GetBool("OTEL_ENABLED", false, false) {
30+
log.Info(ctx, "Telemetry is disabled")
31+
return
32+
}
33+
34+
otel.SetLogger(stdr.New(log.DefaultLogger))
35+
otel.SetErrorHandler(&telemetryErrorHandler{})
36+
2037
resource := getResource(name, version)
2138
log.Info(ctx, "Initializing telemetry", "name", name, "version", version)
2239
log.Init(ctx, resource)

0 commit comments

Comments
 (0)