-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
511 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
package autologs | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-faster/errors" | ||
"go.opentelemetry.io/collector/pdata/plog/plogotlp" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/go-faster/sdk/zapotel" | ||
"github.com/go-faster/sdk/zctx" | ||
) | ||
|
||
// Setup OTLP log exporter if configured. | ||
func Setup(ctx context.Context, res *resource.Resource) (context.Context, error) { | ||
if os.Getenv("OTEL_LOGS_EXPORTER") != "otlp" { | ||
return ctx, nil | ||
} | ||
endpoint := os.Getenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT") | ||
if endpoint == "" { | ||
endpoint = os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") | ||
} | ||
if endpoint == "" { | ||
endpoint = "localhost:4317" | ||
} | ||
endpoint = strings.TrimPrefix(endpoint, "http://") | ||
conn, err := grpc.DialContext(ctx, endpoint, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
) | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "dial logs endpoint") | ||
} | ||
lg := zctx.From(ctx) | ||
otelCore := zapotel.New(lg.Level(), res, plogotlp.NewGRPCClient(conn)) | ||
// Update logger down the stack. | ||
lg.Info("Setting up OTLP log exporter") | ||
lg = lg.WithOptions( | ||
zap.WrapCore(func(core zapcore.Core) zapcore.Core { | ||
return zapcore.NewTee(core, otelCore) | ||
}), | ||
) | ||
return zctx.Base(ctx, lg), nil | ||
} |
This file contains 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 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
Oops, something went wrong.