Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/cmd/get-wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/cloudnative-pg/klio/core/internal/cli"
"github.com/cloudnative-pg/klio/core/internal/client/klioclient"
"github.com/cloudnative-pg/klio/core/internal/client/klioclient/grpcclient"
"github.com/cloudnative-pg/klio/core/internal/opentelemetry"
"github.com/cloudnative-pg/klio/core/pkg/config"
)

Expand Down Expand Up @@ -89,6 +90,7 @@ var getWalCmd = &cobra.Command{
}

if address == "" {
opentelemetry.ShutdownLogs(cmd.Context())
os.Exit(4)
}

Expand All @@ -114,6 +116,7 @@ var getWalCmd = &cobra.Command{
case errors.Is(err, klioclient.ErrMissingWALFile):
if path.Ext(walName) != "" || !downloadPartial {
contextLogger.Debug("Missing WAL file, exiting with error code 4", "wal_name", walName)
opentelemetry.ShutdownLogs(cmd.Context())
os.Exit(4)
}

Expand All @@ -136,6 +139,7 @@ var getWalCmd = &cobra.Command{

case errors.Is(err, klioclient.ErrMissingWALFile):
contextLogger.Debug("Missing partial WAL file, exiting with error code 4", "wal_name", walName)
opentelemetry.ShutdownLogs(cmd.Context())
os.Exit(4)

case err != nil:
Expand Down
8 changes: 7 additions & 1 deletion core/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/cloudnative-pg/klio/core/cmd/retention"
"github.com/cloudnative-pg/klio/core/cmd/server"
"github.com/cloudnative-pg/klio/core/cmd/walplayer"
"github.com/cloudnative-pg/klio/core/internal/opentelemetry"

_ "net/http/pprof" //nolint:gosec
)
Expand Down Expand Up @@ -74,7 +75,11 @@ run directly by end users.`,
if debug {
log.SetLogLevel(log.DebugLevelString)
}
logFlags.ConfigureLogging()
loggingOptions, otelErr := opentelemetry.LoggingOptions(cmd.Context())
logFlags.ConfigureLogging(loggingOptions...)
if otelErr != nil {
log.Error(otelErr, "Cannot export the logs via OpenTelemetry")
}

if pprofServerAddress != "" {
go func() {
Expand All @@ -97,6 +102,7 @@ run directly by end users.`,
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(ctx context.Context) {
err := rootCmd.ExecuteContext(ctx)
opentelemetry.ShutdownLogs(ctx)
if err != nil {
os.Exit(1)
}
Expand Down
10 changes: 6 additions & 4 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/cloudnative-pg/cloudnative-pg v1.30.1
github.com/cloudnative-pg/cnpg-i v0.6.0
github.com/cloudnative-pg/cnpg-i-machinery v0.4.2
github.com/cloudnative-pg/machinery v0.6.1
github.com/cloudnative-pg/machinery v0.6.2-0.20261110171004-0f58adc95f80
github.com/fclairamb/afero-s3 v0.5.0
github.com/go-viper/mapstructure/v2 v2.5.0
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -56,6 +56,8 @@ require (
sigs.k8s.io/yaml v1.6.0 // indirect
)

require go.opentelemetry.io/otel/sdk/log v1.47.0

require (
cloud.google.com/go/compute/metadata v0.10.0 // indirect
filippo.io/hpke v0.4.0 // indirect
Expand Down Expand Up @@ -159,14 +161,15 @@ require (
github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/shirou/gopsutil/v4 v4.26.9 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/spf13/pflag v1.1.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/thoas/go-funk v0.9.3 // indirect
github.com/tklauser/go-sysconf v0.4.0 // indirect
github.com/tklauser/numcpus v0.12.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.21.0 // indirect
go.opentelemetry.io/contrib/detectors/aws/ec2/v2 v2.6.0 // indirect
go.opentelemetry.io/contrib/detectors/aws/ecs v1.47.0 // indirect
go.opentelemetry.io/contrib/detectors/aws/eks v1.47.0 // indirect
Expand All @@ -193,7 +196,6 @@ require (
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.47.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.47.0 // indirect
go.opentelemetry.io/otel/log v1.47.0 // indirect
go.opentelemetry.io/otel/sdk/log v1.47.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.28.0 // indirect
Expand All @@ -202,7 +204,7 @@ require (
golang.org/x/net v0.60.0 // indirect
golang.org/x/oauth2 v0.37.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/sys v0.50.0 // indirect
golang.org/x/term v0.47.0 // indirect
golang.org/x/text v0.43.0 // indirect
golang.org/x/time v0.16.0 // indirect
Expand Down
16 changes: 10 additions & 6 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ github.com/cloudnative-pg/cnpg-i v0.6.0 h1:LA//DLkFOLIjU0ASOpFkydZhGir9IAIDfgSsT
github.com/cloudnative-pg/cnpg-i v0.6.0/go.mod h1:4kcpLAj+feMTnh0TVadXRASdVnEhBytNSm/BvktLgmI=
github.com/cloudnative-pg/cnpg-i-machinery v0.4.2 h1:0reS9MtyLYINHXQ/MfxJ9jp39hhBf8e3Qdj+T5Nsq6I=
github.com/cloudnative-pg/cnpg-i-machinery v0.4.2/go.mod h1:gvrKabgxXq0zGthXGucemDdsxakLEQDMxn43M4HLW30=
github.com/cloudnative-pg/machinery v0.6.1 h1:qcFS4TIO2XH0yW2uwts5KdWGldBBwvTZw00qJNStqvs=
github.com/cloudnative-pg/machinery v0.6.1/go.mod h1:AUE7ifNhkD8BU/ebFY+dxrpvOaUlU4Fvl+yjiUkCpMY=
github.com/cloudnative-pg/machinery v0.6.2-0.20261110171004-0f58adc95f80 h1:hwVs5ASHLRXPXoUZABzQ0yWu/Vf9DxdgO8xySFQLUBc=
github.com/cloudnative-pg/machinery v0.6.2-0.20261110171004-0f58adc95f80/go.mod h1:o14J0IP2kO2izDQy8WYqnYvogtkNTuLzDkykpQcN/jE=
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
Expand Down Expand Up @@ -307,8 +307,8 @@ github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qq
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.1.0 h1:6W6MbVUrnMb8PEhjkqwsVLY7ryuzRfFHF4mQd7L94ag=
github.com/spf13/pflag v1.1.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -346,6 +346,8 @@ github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/bridges/otelzap v0.21.0 h1:p23nklcy3VVRcfZ2ce5AXQa6RHQPQvIMM+fW3GzXCU0=
go.opentelemetry.io/contrib/bridges/otelzap v0.21.0/go.mod h1:7NPD4VToURMipQu1xgz6seTz8aXmbCqQNZs7bQADEjU=
go.opentelemetry.io/contrib/bridges/prometheus v0.72.0 h1:OUIUu7ovHCEvG4IE5LQd2PNBH2eVYh50w/1ROwHEM7M=
go.opentelemetry.io/contrib/bridges/prometheus v0.72.0/go.mod h1:pCrnkC07AZ5+h+0A0Z/1GYyqB+xu3A8LaEEhxvv9s6o=
go.opentelemetry.io/contrib/detectors/autodetect v0.19.0 h1:EqEFSUPTVuB3YSgv/V8eWLj9kq2qcfH81gRWy9Lfx9c=
Expand Down Expand Up @@ -416,6 +418,8 @@ go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.47.0 h1:N3YQCxjxQ/bMjyc
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.47.0/go.mod h1:Mp8HOFqcaUyypCuGv9IhDdTHnJ56lSudSHMd+pVSCEA=
go.opentelemetry.io/otel/log v1.47.0 h1:cOTS1CcLbSQeZKanGJ+0JpF/+t4PELi3O3bbl2lqCcI=
go.opentelemetry.io/otel/log v1.47.0/go.mod h1:9byitSQ5pLC6PpqwGXjqdMKya6ZTswHRZh2vvXT33nw=
go.opentelemetry.io/otel/log/logtest v0.23.0 h1:XeI2D+/DjUMYzrfmMPXOxnTIkUTCXT/HwQ5QBJSEC/8=
go.opentelemetry.io/otel/log/logtest v0.23.0/go.mod h1:TOVBzNwEZG0vmQUuvE9UHaiKkUN7q3j1d+GeVGs/yQ8=
go.opentelemetry.io/otel/metric v1.47.0 h1:4PptaldXx3Eat1XjMZ68pPJEs5wrhlemctZE9a3UdWY=
go.opentelemetry.io/otel/metric v1.47.0/go.mod h1:ADGSXxRrXM6bjbvLo535EstVFlPpPYZm4LBKixjDHwU=
go.opentelemetry.io/otel/metric/x v0.69.0 h1:DjRLr15H83v+hCW7JA9NoJvOkYTtmq5YoDRbe9deYpM=
Expand Down Expand Up @@ -457,8 +461,8 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
Expand Down
3 changes: 3 additions & 0 deletions core/internal/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"github.com/cloudnative-pg/machinery/pkg/log"
"github.com/spf13/cobra"

"github.com/cloudnative-pg/klio/core/internal/opentelemetry"
)

// RunEWithExitCode wraps a cobra RunE so that an error implementing
Expand All @@ -49,6 +51,7 @@ func RunEWithExitCode(
// rather than printed as plain text. fn has already returned,
// so all of its defers have run.
log.FromContext(cmd.Context()).Error(err, "command failed", "command", cmd.CommandPath())
opentelemetry.ShutdownLogs(cmd.Context())
os.Exit(coded.ExitCode())
}

Expand Down
84 changes: 84 additions & 0 deletions core/internal/opentelemetry/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright © contributors to CloudNativePG, established as
CloudNativePG a Series of LF Projects, LLC.

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.

SPDX-License-Identifier: Apache-2.0
*/

package opentelemetry

import (
"context"
"fmt"
"time"

"github.com/cloudnative-pg/machinery/pkg/log"
"go.opentelemetry.io/contrib/exporters/autoexport"
sdklog "go.opentelemetry.io/otel/sdk/log"
)

// loggerProvider exports the log records, nil when the export is disabled.
//
//nolint:gochecknoglobals
var loggerProvider *sdklog.LoggerProvider

// LoggingOptions creates the provider exporting the log records, with the
// resource of the other signals and the exporter selected by
// OTEL_LOGS_EXPORTER, "otlp" when unset, and returns the options making
// ConfigureLogging use it. Like the other signals, it returns no option
// when OpenTelemetry is not configured or disabled, and when
// OTEL_LOGS_EXPORTER is "none". Call it before ConfigureLogging, so that
// every record is exported, and call ShutdownLogs before the process exits.
func LoggingOptions(ctx context.Context) ([]log.ConfigureOption, error) {
if !isOtelConfigPresent() {
return nil, nil
}

res, err := createResource(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

exp, err := autoexport.NewLogExporter(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create log exporter: %w", err)
}
if autoexport.IsNoneLogExporter(exp) {
return nil, nil
}

loggerProvider = sdklog.NewLoggerProvider(
sdklog.WithProcessor(sdklog.NewBatchProcessor(exp)),
sdklog.WithResource(res),
)

return []log.ConfigureOption{log.WithOpenTelemetryLoggerProvider(loggerProvider)}, nil
}

// ShutdownLogs exports the queued log records and stops their export,
// waiting at most five seconds for the collector, even when ctx is already
// cancelled. It is a no-op when the export is disabled.
func ShutdownLogs(ctx context.Context) {
if loggerProvider == nil {
return
}

ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()

if err := loggerProvider.Shutdown(ctx); err != nil {
log.Error(err, "failed to shutdown the OpenTelemetry logger provider")
}
}
Loading