-
Notifications
You must be signed in to change notification settings - Fork 0
Phase 6 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Phase 6 #16
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62ed2ca
fix(observability): resolve OTel resource schema URL conflict using N…
gitcommitankit 448a13c
docs: add observability README with metrics, logging, and tracing guide
gitcommitankit c0e63be
removed doc for observibility
gitcommitankit 2de702e
feat: add insecure flag to OTLP tracer configuration and remove hardc…
gitcommitankit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package observability | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "log/slog" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| // NewJSONLogger returns a [*slog.Logger] that writes structured JSON records to | ||
| // w. Pass os.Stdout in production; pass a [*bytes.Buffer] in tests. | ||
| func NewJSONLogger(w io.Writer) *slog.Logger { | ||
| return slog.New(slog.NewJSONHandler(w, &slog.HandlerOptions{ | ||
| Level: slog.LevelInfo, | ||
| })) | ||
| } | ||
|
|
||
| // WithTraceContext returns a child logger pre-populated with "trace_id" and | ||
| // "span_id" fields extracted from the active OpenTelemetry span in ctx. If ctx | ||
| // carries no valid span the original logger is returned unchanged. | ||
| func WithTraceContext(ctx context.Context, logger *slog.Logger) *slog.Logger { | ||
| span := trace.SpanFromContext(ctx) | ||
| sc := span.SpanContext() | ||
| if !sc.IsValid() { | ||
| return logger | ||
| } | ||
| return logger.With( | ||
| "trace_id", sc.TraceID().String(), | ||
| "span_id", sc.SpanID().String(), | ||
| ) | ||
| } | ||
|
|
||
| // NewLogr wraps a [*slog.Logger] as a [logr.Logger] so it can be passed to | ||
| // controller-runtime's [ctrl.SetLogger]. All controller-runtime log output | ||
| // (including reconcile errors) then flows through the slog JSON handler. | ||
| func NewLogr(logger *slog.Logger) logr.Logger { | ||
| return logr.FromSlogHandler(logger.Handler()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Package observability provides OpenTelemetry tracing initialization for the Agentrax operator. | ||
| // | ||
| // Use [InitTracerProvider] in main() to configure the global OTel tracer. When | ||
| // endpoint is empty the function installs a no-op provider so callers need not | ||
| // guard on whether tracing is enabled. The returned Shutdown function must be | ||
| // deferred in main() to flush buffered spans before process exit. | ||
| package observability | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
| "go.opentelemetry.io/otel/propagation" | ||
| "go.opentelemetry.io/otel/sdk/resource" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| semconv "go.opentelemetry.io/otel/semconv/v1.26.0" | ||
| "go.opentelemetry.io/otel/trace" | ||
| "go.opentelemetry.io/otel/trace/noop" | ||
| ) | ||
|
|
||
| // TracerName is the instrumentation library name used for all Agentrax OTel spans. | ||
| const TracerName = "agentrax.io/controller" | ||
|
|
||
| // Tracer is the package-level tracer used by controller code. It is set by | ||
| // [InitTracerProvider] and defaults to the global no-op tracer so it is always | ||
| // safe to call, even when tracing is disabled. | ||
| var Tracer trace.Tracer = noop.NewTracerProvider().Tracer(TracerName) | ||
|
|
||
| // InitTracerProvider configures the global OpenTelemetry TracerProvider and | ||
| // installs a W3C TraceContext propagator. When endpoint is empty it installs a | ||
| // no-op provider (tracing disabled). TLS is used by default for exporter connections; | ||
| // set insecure to true only for local development with plaintext collectors. | ||
| // The returned Shutdown function flushes and stops the exporter; it must be | ||
| // deferred in main(). | ||
| func InitTracerProvider(ctx context.Context, endpoint string, insecure bool) (func(context.Context) error, error) { | ||
| if endpoint == "" { | ||
| // No-op: tracing disabled. Global tracer stays as the default no-op. | ||
| return func(context.Context) error { return nil }, nil | ||
| } | ||
|
|
||
| opts := []otlptracegrpc.Option{ | ||
| otlptracegrpc.WithEndpoint(endpoint), | ||
| } | ||
| if insecure { | ||
| opts = append(opts, otlptracegrpc.WithInsecure()) | ||
| } | ||
|
|
||
| exp, err := otlptracegrpc.New(ctx, opts...) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating OTLP gRPC exporter: %w", err) | ||
| } | ||
|
|
||
| res, err := resource.Merge( | ||
| resource.Default(), | ||
| resource.NewSchemaless( | ||
| semconv.ServiceName("agentrax-operator"), | ||
| ), | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating OTel resource: %w", err) | ||
| } | ||
|
|
||
| tp := sdktrace.NewTracerProvider( | ||
| sdktrace.WithBatcher(exp), | ||
| sdktrace.WithResource(res), | ||
| ) | ||
|
|
||
| otel.SetTracerProvider(tp) | ||
| // W3C TraceContext + Baggage propagation — required for cross-service correlation. | ||
| otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( | ||
| propagation.TraceContext{}, | ||
| propagation.Baggage{}, | ||
| )) | ||
|
|
||
| // Update the package-level tracer to use the real provider. | ||
| Tracer = tp.Tracer(TracerName) | ||
|
|
||
| return tp.Shutdown, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.