Skip to content
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

Add support for logs #123

Closed
wants to merge 2 commits into from
Closed
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
49 changes: 26 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
module github.com/krzko/otelgen

go 1.19
go 1.21

require (
github.com/fatih/color v1.15.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/urfave/cli/v2 v2.25.3
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.37.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.37.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.14.0
go.opentelemetry.io/otel/metric v0.37.0
go.opentelemetry.io/otel/sdk v1.14.0
go.opentelemetry.io/otel/sdk/metric v0.37.0
go.opentelemetry.io/otel/trace v1.14.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240722072124-4c7f2bf5e845
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.0.0-20240722072124-4c7f2bf5e845
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
go.opentelemetry.io/otel/log v0.4.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/log v0.4.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
go.uber.org/atomic v1.10.0
go.uber.org/zap v1.24.0
golang.org/x/time v0.3.0
google.golang.org/grpc v1.54.0
google.golang.org/grpc v1.65.0
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.37.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
435 changes: 64 additions & 371 deletions go.sum

Large diffs are not rendered by default.

120 changes: 117 additions & 3 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,138 @@
package cli

import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/log/global"
sdklog "go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"google.golang.org/grpc"

"github.com/krzko/otelgen/internal/logs"

"github.com/urfave/cli/v2"
"go.uber.org/zap"
)

func genLogsCommand() *cli.Command {
return &cli.Command{
Name: "logs",
Usage: "Generate logs",
Aliases: []string{"l"},
Hidden: true,
Subcommands: []*cli.Command{
{
Name: "single",
Usage: "generate a single log entry",
Aliases: []string{"s"},
Action: func(c *cli.Context) error {
if c.String("otel-exporter-otlp-endpoint") == "" {
return errors.New("'otel-exporter-otlp-endpoint' must be set")
}

logsCfg := &logs.Config{
Endpoint: c.String("otel-exporter-otlp-endpoint"),
NumLogs: 1,
// WorkerCount: 1,
ServiceName: c.String("service-name"),
}

if c.String("log-level") == "debug" {
grpcZap.ReplaceGrpcLoggerV2(logger.WithOptions(
zap.AddCallerSkip(3),
))
}

grpcExpOpt := []otlploggrpc.Option{
otlploggrpc.WithEndpoint(logsCfg.Endpoint),
otlploggrpc.WithDialOption(
grpc.WithBlock(),
),
}

httpExpOpt := []otlploghttp.Option{
otlploghttp.WithEndpoint(logsCfg.Endpoint),
}

if c.Bool("insecure") {
grpcExpOpt = append(grpcExpOpt, otlploggrpc.WithInsecure())
httpExpOpt = append(httpExpOpt, otlploghttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlploggrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlploghttp.WithHeaders(headers))
}

var blp *sdklog.BatchProcessor
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
exp, err := otlploghttp.New(context.Background(), httpExpOpt...)
if err != nil {
logger.Error("failed to obtain OTLP exporter", zap.Error(err))
return err
}
defer func() {
logger.Info("stopping the exporter")
if err = exp.Shutdown(context.Background()); err != nil {
logger.Error("failed to stop the exporter", zap.Error(err))
return
}
}()
blp = sdklog.NewBatchProcessor(exp, sdklog.WithExportTimeout(time.Second))
} else {
logger.Info("starting gRPC exporter")
exp, err := otlploggrpc.New(context.Background(), grpcExpOpt...)
if err != nil {
logger.Error("failed to obtain OTLP exporter", zap.Error(err))
return err
}
defer func() {
logger.Info("stopping the exporter")
if err = exp.Shutdown(context.Background()); err != nil {
logger.Error("failed to stop the exporter", zap.Error(err))
return
}
}()
blp = sdklog.NewBatchProcessor(exp, sdklog.WithExportTimeout(time.Second))
}

defer func() {
logger.Info("stop the batch log processor")
if err := blp.Shutdown(context.Background()); err != nil {
logger.Error("failed to stop the batch log processor", zap.Error(err))
return
}
}()

loggerProvider := sdklog.NewLoggerProvider(
sdklog.WithResource(resource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceNameKey.String(logsCfg.ServiceName))),
sdklog.WithProcessor(blp),
)

global.SetLoggerProvider(loggerProvider)

logger.Info("Not yet implemented")
logger.Info("https://opentelemetry.io/docs/instrumentation/go/#status-and-releases")
if err := logs.Run(logsCfg); err != nil {
logger.Error("failed to stop the exporter", zap.Error(err))
}

return nil
},
Expand Down
17 changes: 4 additions & 13 deletions internal/cli/metrics_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ func genMetricsCommand() *cli.Command {
}
}

// MetricExporter is an interface that abstracts the functionality of both
// otlpmetricgrpc and otlpmetrichttp exporters.
type MetricExporter interface {
metric.Exporter
}

// NewMetricExporter creates a new MetricExporter based on the provided protocol.
// It accepts either otlpmetricgrpc.Option or otlpmetrichttp.Option as an argument.
// It returns an implementation of the MetricExporter interface.
func NewMetricExporter(ctx context.Context, protocol string, options interface{}) (MetricExporter, error) {
func NewMetricExporter(ctx context.Context, protocol string, options interface{}) (metric.Exporter, error) {
switch protocol {
case "grpc":
grpcOptions, ok := options.([]otlpmetricgrpc.Option)
Expand Down Expand Up @@ -69,8 +60,8 @@ func configureLogging(c *cli.Context) {
}

// createExporter creates a new exporter based on the command line flags
func createExporter(ctx context.Context, c *cli.Context, grpcExpOpt []otlpmetricgrpc.Option, httpExpOpt []otlpmetrichttp.Option) (MetricExporter, error) {
var exp MetricExporter
func createExporter(ctx context.Context, c *cli.Context, grpcExpOpt []otlpmetricgrpc.Option, httpExpOpt []otlpmetrichttp.Option) (metric.Exporter, error) {
var exp metric.Exporter
var err error

if c.String("protocol") == "http" {
Expand Down Expand Up @@ -187,7 +178,7 @@ func preferCumulativeTemporalitySelector(kind metric.InstrumentKind) metricdata.
}

// shutdownExporter shuts down the exporter
func shutdownExporter(exp MetricExporter) {
func shutdownExporter(exp metric.Exporter) {
defer func() {
logger.Info("stopping the exporter")
if err := exp.Shutdown(context.Background()); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/logs/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package logs

import (
"flag"
"fmt"
"strings"
)

type Config struct {
NumLogs int
ServiceName string

// OTLP config
Endpoint string
Insecure bool
UseHTTP bool
Headers HeaderValue
}

type HeaderValue map[string]string

var _ flag.Value = (*HeaderValue)(nil)

func (v *HeaderValue) String() string {
return ""
}

func (v *HeaderValue) Set(s string) error {
kv := strings.SplitN(s, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
(*v)[kv[0]] = kv[1]
return nil
}
33 changes: 31 additions & 2 deletions internal/logs/logs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package logs

// TODO: Implement this package when logs > Not yet implemented
// https://opentelemetry.io/docs/instrumentation/go/#status-and-releases
import (
"context"
"fmt"
"time"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
)

// Run executes the test scenario
func Run(c *Config) error {
if c.NumLogs <= 0 {
return fmt.Errorf("number of logs must be greater than zero")
}

for i := 0; i < c.NumLogs; i++ {
record := log.Record{}

record.SetBody(log.StringValue(fmt.Sprintf("Test log %d", i+1)))

now := time.Now()
record.SetObservedTimestamp(now)
record.SetTimestamp(now)

record.SetSeverity(log.SeverityInfo)

global.Logger(c.ServiceName).Emit(context.Background(), record)
}

return nil
}
12 changes: 6 additions & 6 deletions internal/metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"time"

"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/metric"
apiMetric "go.opentelemetry.io/otel/metric"
sdkMetric "go.opentelemetry.io/otel/sdk/metric"
"go.uber.org/zap"
)

// Counter demonstrates how to measure non-decreasing int64s
func SimulateCounter(mp *metric.MeterProvider, conf *Config, logger *zap.Logger) {
func SimulateCounter(mp *sdkMetric.MeterProvider, conf *Config, logger *zap.Logger) {
c := *conf
err := run(conf, logger, counter(mp, c, logger))
if err != nil {
Expand All @@ -20,14 +20,14 @@ func SimulateCounter(mp *metric.MeterProvider, conf *Config, logger *zap.Logger)
}

// counter generates a counter metric
func counter(mp *metric.MeterProvider, c Config, logger *zap.Logger) WorkerFunc {
func counter(mp *sdkMetric.MeterProvider, c Config, logger *zap.Logger) WorkerFunc {
return func(ctx context.Context) {
name := fmt.Sprintf("%v.metrics.counter", c.ServiceName)
logger.Debug("generating counter", zap.String("name", name))
counter, _ := mp.Meter(c.ServiceName).Int64Counter(
name,
instrument.WithUnit("1"),
instrument.WithDescription("Counter demonstrates how to measure non-decreasing numbers"),
apiMetric.WithUnit("1"),
apiMetric.WithDescription("Counter demonstrates how to measure non-decreasing numbers"),
)

var i int64
Expand Down
12 changes: 6 additions & 6 deletions internal/metrics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"math/rand"
"time"

"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/metric"
apiMetric "go.opentelemetry.io/otel/metric"
sdkMetric "go.opentelemetry.io/otel/sdk/metric"
"go.uber.org/zap"
)

// Histogram demonstrates how to record a distribution of individual values
func SimulateHistogram(mp *metric.MeterProvider, conf *Config, logger *zap.Logger) {
func SimulateHistogram(mp *sdkMetric.MeterProvider, conf *Config, logger *zap.Logger) {
c := *conf
err := run(conf, logger, histogram(mp, c, logger))
if err != nil {
Expand All @@ -21,13 +21,13 @@ func SimulateHistogram(mp *metric.MeterProvider, conf *Config, logger *zap.Logge
}

// histogram generates a histogram metric
func histogram(mp *metric.MeterProvider, c Config, logger *zap.Logger) WorkerFunc {
func histogram(mp *sdkMetric.MeterProvider, c Config, logger *zap.Logger) WorkerFunc {
return func(ctx context.Context) {
name := fmt.Sprintf("%v.metrics.histogram", c.ServiceName)
durRecorder, _ := mp.Meter(c.ServiceName).Int64Histogram(
name,
instrument.WithUnit("microseconds"),
instrument.WithDescription("Histogram demonstrates how to record a distribution of individual values"),
apiMetric.WithUnit("microseconds"),
apiMetric.WithDescription("Histogram demonstrates how to record a distribution of individual values"),
)

if c.TotalDuration > 0 {
Expand Down
Loading
Loading