Skip to content

Commit

Permalink
Merge pull request #1207 from Permify/otel
Browse files Browse the repository at this point in the history
feat: otel headers option and cache hit span
  • Loading branch information
tolgaOzen committed Apr 25, 2024
2 parents 68a08a9 + acb97c4 commit e29be50
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/api-reference/apidocs.swagger.json
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Permify API",
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
"version": "v0.8.0",
"version": "v0.8.1",
"contact": {
"name": "API Support",
"url": "https://github.com/Permify/permify/issues",
Expand Down
24 changes: 14 additions & 10 deletions internal/config/config.go
Expand Up @@ -89,20 +89,22 @@ type (

// Tracer contains configuration for distributed tracing.
Tracer struct {
Enabled bool `mapstructure:"enabled"` // Whether tracing collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for tracing data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the tracing exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the tracing exporter, if not defined /v1/trace will be used
Enabled bool `mapstructure:"enabled"` // Whether tracing collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for tracing data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the tracing exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the tracing exporter, if not defined /v1/trace will be used
Headers map[string]string `mapstructure:"headers"`
}

// Meter contains configuration for metrics collection and reporting.
Meter struct {
Enabled bool `mapstructure:"enabled"` // Whether metrics collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for metrics data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the metrics exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the metrics exporter, if not defined /v1/metrics will be used
Enabled bool `mapstructure:"enabled"` // Whether metrics collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for metrics data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the metrics exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the metrics exporter, if not defined /v1/metrics will be used
Headers map[string]string `mapstructure:"headers"`
}

// Service contains configuration for various service-level features.
Expand Down Expand Up @@ -275,11 +277,13 @@ func DefaultConfig() *Config {
},
Tracer: Tracer{
Enabled: false,
Headers: map[string]string{},
},
Meter: Meter{
Enabled: true,
Exporter: "otlp",
Endpoint: "telemetry.permify.co",
Headers: map[string]string{},
},
Service: Service{
CircuitBreaker: false,
Expand Down
6 changes: 6 additions & 0 deletions internal/engines/cache/cache.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"

"go.opentelemetry.io/otel"
api "go.opentelemetry.io/otel/metric"

"github.com/cespare/xxhash/v2"
Expand All @@ -15,6 +16,8 @@ import (
base "github.com/Permify/permify/pkg/pb/base/v1"
)

var tracer = otel.Tracer("check-cache")

// CheckEngineWithCache is a struct that holds an instance of a cache.Cache for managing engine cache.
type CheckEngineWithCache struct {
// schemaReader is responsible for reading schema information
Expand Down Expand Up @@ -69,6 +72,9 @@ func (c *CheckEngineWithCache) Check(ctx context.Context, request *base.Permissi

// If a cached result is found, handle exclusion and return the result.
if found {
ctx, span := tracer.Start(ctx, "hit")
defer span.End()

// Increase the check count in the metrics.
c.cacheCounter.Add(ctx, 1)
// If the request doesn't have the exclusion flag set, return the cached result.
Expand Down
2 changes: 1 addition & 1 deletion internal/info.go
Expand Up @@ -23,7 +23,7 @@ var Identifier = ""
*/
const (
// Version is the last release of the Permify (e.g. v0.1.0)
Version = "v0.8.0"
Version = "v0.8.1"
)

// Function to create a single line of the ASCII art with centered content and color
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/serve.go
Expand Up @@ -10,8 +10,6 @@ import (
"syscall"
"time"

"github.com/davecgh/go-spew/spew"

"github.com/sony/gobreaker"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -158,8 +156,6 @@ func serve() func(cmd *cobra.Command, args []string) error {
}
}

spew.Dump(cfg)

// Print banner and initialize logger
internal.PrintBanner()

Expand Down Expand Up @@ -232,6 +228,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Tracer.Endpoint,
cfg.Tracer.Insecure,
cfg.Tracer.URLPath,
cfg.Tracer.Headers,
)
if err != nil {
slog.Error(err.Error())
Expand Down Expand Up @@ -274,6 +271,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Meter.Endpoint,
cfg.Meter.Insecure,
cfg.Meter.URLPath,
cfg.Meter.Headers,
)
if err != nil {
slog.Error(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion pkg/pb/base/v1/openapi.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/telemetry/meterexporters/factory.go
Expand Up @@ -7,12 +7,12 @@ import (
)

// ExporterFactory - Create meter exporter according to given params
func ExporterFactory(name, endpoint string, insecure bool, urlpath string) (metric.Exporter, error) {
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
switch name {
case "otlp", "otlp-http":
return NewOTLP(endpoint, insecure, urlpath)
return NewOTLP(endpoint, insecure, urlpath, headers)
case "otlp-grpc":
return NewOTLPGRPC(endpoint, insecure)
return NewOTLPGrpc(endpoint, insecure, headers)
default:
return nil, fmt.Errorf("%s meter exporter is unsupported", name)
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/meterexporters/otlp.go
Expand Up @@ -8,12 +8,16 @@ import (
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string) (metric.Exporter, error) {
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
options := []otlpmetrichttp.Option{
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
otlpmetrichttp.WithEndpoint(endpoint),
}

if len(headers) > 0 {
options = append(options, otlpmetrichttp.WithHeaders(headers))
}

if urlpath != "" {
options = append(options, otlpmetrichttp.WithURLPath(urlpath))
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/telemetry/meterexporters/otlp_grpc.go
Expand Up @@ -2,24 +2,29 @@ package meterexporters

import (
"context"

"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/sdk/metric"
"google.golang.org/grpc/credentials"
)

// NewOTLPGrpc - Creates new OTLP exporter using GRPC protocol.
func NewOTLPGrpc(endpoint string, insecure bool) (metric.Exporter, error) {
func NewOTLPGrpc(endpoint string, insecure bool, headers map[string]string) (metric.Exporter, error) {
options := []otlpmetricgrpc.Option{
otlpmetricgrpc.WithEndpoint(endpoint),
otlpmetricgrpc.WithHeaders(headers),
}

if len(headers) > 0 {
options = append(options, otlpmetricgrpc.WithHeaders(headers))
}

if insecure {
options = append(options, otlpmetricgrpc.WithInsecure())
} else {
options = append(options, otlpmetricgrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")))
}

exporter, err := otlpmetricgrpc.New(context.Background(), options...)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/telemetry/tracerexporters/factory.go
Expand Up @@ -7,16 +7,16 @@ import (
)

// ExporterFactory - Create tracer exporter according to given params
func ExporterFactory(name, url string, insecure bool, urlpath string) (trace.SpanExporter, error) {
func ExporterFactory(name, url string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
switch name {
case "zipkin":
return NewZipkin(url)
case "jaeger":
return NewJaegar(url)
case "otlp", "otlp-http":
return NewOTLP(url, insecure, urlpath)
return NewOTLP(url, insecure, urlpath, headers)
case "otlp-grpc":
return NewOTLPGrpc(url, insecure)
return NewOTLPGrpc(url, insecure, headers)
case "signoz":
return NewSigNoz(url, insecure)
default:
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/tracerexporters/otlp.go
Expand Up @@ -8,14 +8,18 @@ import (
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string) (trace.SpanExporter, error) {
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
var exporter trace.SpanExporter
var err error

opts := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(endpoint),
}

if len(headers) > 0 {
opts = append(opts, otlptracehttp.WithHeaders(headers))
}

if urlpath != "" {
opts = append(opts, otlptracehttp.WithURLPath(urlpath))
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/tracerexporters/otlp_grpc.go
Expand Up @@ -9,14 +9,18 @@ import (
)

// NewOTLPGrpc - Creates new OTLP exporter using GRPC protocol.
func NewOTLPGrpc(endpoint string, insecure bool) (trace.SpanExporter, error) {
func NewOTLPGrpc(endpoint string, insecure bool, headers map[string]string) (trace.SpanExporter, error) {
var exporter trace.SpanExporter
var err error

opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(endpoint),
}

if len(headers) > 0 {
opts = append(opts, otlptracegrpc.WithHeaders(headers))
}

if insecure {
opts = append(opts, otlptracegrpc.WithInsecure())
} else {
Expand Down
2 changes: 1 addition & 1 deletion proto/base/v1/openapi.proto
Expand Up @@ -9,7 +9,7 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
info: {
title: "Permify API";
description: "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.";
version: "v0.8.0";
version: "v0.8.1";
contact: {
name: "API Support";
url: "https://github.com/Permify/permify/issues";
Expand Down

0 comments on commit e29be50

Please sign in to comment.