Skip to content

Commit

Permalink
Update OpenTelemetry configuration and observability package
Browse files Browse the repository at this point in the history
  • Loading branch information
mantzas committed Apr 30, 2024
1 parent 068f147 commit b283d71
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ services:
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
otelcol:
image: otel/opentelemetry-collector-contrib
restart: unless-stopped
command: [ "--config=/etc/otelcol-config.yml" ]
volumes:
- ./otelcol-config.yml:/etc/otelcol-config.yml
ports:
- 4317:4317
volumes:
rabbitmq_data:
driver: local
Expand Down
10 changes: 9 additions & 1 deletion examples/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/beatlabs/patron"
"github.com/beatlabs/patron/examples"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const (
Expand Down Expand Up @@ -35,7 +37,13 @@ func init() {
func main() {
ctx := context.Background()

service, err := patron.New(name, version)
conn, err := grpc.NewClient("localhost:4317", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
slog.Error("failed to create grpc observability client", slog.Any("error", err))
os.Exit(1)
}

service, err := patron.New(name, version, conn)
if err != nil {
slog.Error("failed to set up service", slog.Any("error", err))
os.Exit(1)
Expand Down
44 changes: 22 additions & 22 deletions observability/observability.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package observability provides functionality for initializing OpenTelemetry's traces and metrics.
// It is based on OpenTelemetry and includes methods for setting up and shutting down the observability components.
// It includes methods for setting up and shutting down the observability components.
package observability

import (
Expand All @@ -19,27 +19,6 @@ type Provider struct {
tp *trace.TracerProvider
}

// Shutdown flushes and shuts down the metrics and traces.
// It forces a flush of metrics and traces, logs any errors encountered during flushing,
// and shuts down the metric and trace providers.
func (p *Provider) Shutdown(ctx context.Context) error {
err := p.mp.ForceFlush(ctx)
if err != nil {
slog.Error("failed to flush metrics", slog.Any("error", err))
}
err = p.mp.Shutdown(ctx)
if err != nil {
return err
}

err = p.tp.ForceFlush(ctx)
if err != nil {
slog.Error("failed to flush traces", slog.Any("error", err))
}

return p.tp.Shutdown(ctx)
}

// Setup initializes OpenTelemetry's traces and metrics.
// It creates a resource with the given name and version, sets up the metric and trace providers,
// and returns a Provider containing the initialized providers.
Expand All @@ -63,6 +42,27 @@ func Setup(ctx context.Context, name, version string, conn *grpc.ClientConn) (*P
}, nil
}

// Shutdown flushes and shuts down the metrics and traces.
// It forces a flush of metrics and traces, logs any errors encountered during flushing,
// and shuts down the metric and trace providers.
func (p *Provider) Shutdown(ctx context.Context) error {
err := p.mp.ForceFlush(ctx)
if err != nil {
slog.Error("failed to flush metrics", slog.Any("error", err))
}
err = p.mp.Shutdown(ctx)
if err != nil {
return err
}

err = p.tp.ForceFlush(ctx)
if err != nil {
slog.Error("failed to flush traces", slog.Any("error", err))
}

return p.tp.Shutdown(ctx)
}

func createResource(name, version string) (*resource.Resource, error) {
return resource.Merge(resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL,
Expand Down
20 changes: 20 additions & 0 deletions otelcol-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

receivers:
otlp:
protocols:
grpc:

exporters:
debug:

processors:
batch:

service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [debug]
25 changes: 19 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (

patronErrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/observability"
"github.com/beatlabs/patron/trace"
"github.com/uber/jaeger-client-go"
"google.golang.org/grpc"
)

const (
Expand All @@ -32,21 +34,24 @@ type Component interface {
// Service is responsible for managing and setting up everything.
// The Service will start by default an HTTP component in order to host management endpoint.
type Service struct {
name string
version string
termSig chan os.Signal
sighupHandler func()
logConfig logConfig
name string
version string
termSig chan os.Signal
sighupHandler func()
logConfig logConfig
observabilityProvider *observability.Provider
}

func New(name, version string, options ...OptionFunc) (*Service, error) {
func New(name, version string, observabilityConn *grpc.ClientConn, options ...OptionFunc) (*Service, error) {
if name == "" {
return nil, errors.New("name is required")
}
if version == "" {
version = "dev"
}

ctx := context.Background()

s := &Service{
name: name,
version: version,
Expand Down Expand Up @@ -78,6 +83,11 @@ func New(name, version string, options ...OptionFunc) (*Service, error) {
return nil, patronErrors.Aggregate(optionErrors...)
}

s.observabilityProvider, err = observability.Setup(ctx, name, version, observabilityConn)
if err != nil {
return nil, err
}

setupLogging(s.logConfig)
s.setupOSSignal()

Expand Down Expand Up @@ -117,6 +127,9 @@ func (s *Service) Run(ctx context.Context, components ...Component) error {
for err := range chErr {
ee = append(ee, err)
}

ee = append(ee, s.observabilityProvider.Shutdown(ctx))

return patronErrors.Aggregate(ee...)
}

Expand Down

0 comments on commit b283d71

Please sign in to comment.