Skip to content

The only tool you need to know what is happening and how to fix it.

6.2 KB
Raw Blame
package otelcontrollers

import (
	"compress/gzip"
	"errors"
	"fmt"
	"io"
	"net/http"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/tracewayapp/traceway/backend/app/middleware"
	collogspb "go.opentelemetry.io/proto/otlp/collector/logs/v1"
	colmetricspb "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
	colprofilespb "go.opentelemetry.io/proto/otlp/collector/profiles/v1development"
	coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
)

const maxBodySize = 10 * 1024 * 1024 // 10MB

var errBodyTooLarge = errors.New("request body exceeds the 10MB limit")

const (
	otlpBodyIdle  = 20 * time.Second
	otlpBodyTotal = 2 * time.Minute
)

func readBody(c *gin.Context) ([]byte, error) {
	middleware.GuardBodyRead(c, otlpBodyIdle, otlpBodyTotal)

	raw := http.MaxBytesReader(c.Writer, c.Request.Body, maxBodySize)

	var reader io.Reader = raw
	if strings.EqualFold(c.GetHeader("Content-Encoding"), "gzip") {
		gr, err := gzip.NewReader(raw)
		if err != nil {
			if isMaxBytes(err) {
				return nil, errBodyTooLarge
			}
			return nil, fmt.Errorf("failed to create gzip reader: %w", err)
		}
		defer gr.Close()
		reader = gr
	}
	body, err := io.ReadAll(io.LimitReader(reader, maxBodySize+1))
	if err != nil {
		if isMaxBytes(err) {
			return nil, errBodyTooLarge
		}
		return nil, err
	}
	if len(body) > maxBodySize {
		return nil, errBodyTooLarge
	}
	return body, nil
}

func isMaxBytes(err error) bool {
	var maxBytesErr *http.MaxBytesError
	return errors.As(err, &maxBytesErr)
}

func writeDecodeError(c *gin.Context, err error) {
	if errors.Is(err, errBodyTooLarge) {
		c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": err.Error()})
		return
	}
	middleware.RejectIngestBindError(c, err, err.Error())
}

func isProtobuf(c *gin.Context) bool {
	ct := c.GetHeader("Content-Type")
	return strings.Contains(ct, "application/x-protobuf") || strings.Contains(ct, "application/protobuf")
}

func decodeTraceRequest(c *gin.Context) (*coltracepb.ExportTraceServiceRequest, int, error) {
	body, err := readBody(c)
	if err != nil {
		return nil, 0, err
	}
	req := &coltracepb.ExportTraceServiceRequest{}
	if isProtobuf(c) {
		if err := proto.Unmarshal(body, req); err != nil {
			return nil, 0, fmt.Errorf("failed to unmarshal protobuf: %w", err)
		}
	} else {
		normalized, err := normalizeOTLPJSON(body, req.ProtoReflect().Descriptor())
		if err != nil {
			return nil, 0, fmt.Errorf("failed to decode OTLP JSON: %w", err)
		}
		if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(normalized, req); err != nil {
			return nil, 0, fmt.Errorf("failed to unmarshal JSON: %w", err)
		}
	}
	return req, len(body), nil
}

func decodeMetricsRequest(c *gin.Context) (*colmetricspb.ExportMetricsServiceRequest, int, error) {
	body, err := readBody(c)
	if err != nil {
		return nil, 0, err
	}
	req := &colmetricspb.ExportMetricsServiceRequest{}
	if isProtobuf(c) {
		if err := proto.Unmarshal(body, req); err != nil {
			return nil, 0, fmt.Errorf("failed to unmarshal protobuf: %w", err)
		}
	} else {
		if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(body, req); err != nil {
			return nil, 0, fmt.Errorf("failed to unmarshal JSON: %w", err)
		}
	}
	return req, len(body), nil
}

func writeTraceResponse(c *gin.Context, invalidIDs, notStored int64) {
	resp := &coltracepb.ExportTraceServiceResponse{}
	if invalidIDs+notStored > 0 {
		var reasons []string
		if invalidIDs > 0 {
			reasons = append(reasons, fmt.Sprintf("%d spans need valid non-zero trace and span IDs", invalidIDs))
		}
		if notStored > 0 {
			reasons = append(reasons, fmt.Sprintf("%d spans could not be stored", notStored))
		}
		resp.PartialSuccess = &coltracepb.ExportTracePartialSuccess{RejectedSpans: invalidIDs + notStored, ErrorMessage: strings.Join(reasons, "; ")}
	}
	if isProtobuf(c) {
		data, _ := proto.Marshal(resp)
		c.Data(http.StatusOK, "application/x-protobuf", data)
	} else {
		data, _ := protojson.Marshal(resp)
		c.Data(http.StatusOK, "application/json", data)
	}
}

func writeMetricsResponse(c *gin.Context) {
	resp := &colmetricspb.ExportMetricsServiceResponse{}
	if isProtobuf(c) {
		data, _ := proto.Marshal(resp)
		c.Data(http.StatusOK, "application/x-protobuf", data)
	} else {
		data, _ := protojson.Marshal(resp)
		c.Data(http.StatusOK, "application/json", data)
	}
}

func decodeProfilesPayload(c *gin.Context) ([]byte, int, error) {
	body, err := readBody(c)
	if err != nil {
		return nil, 0, err
	}
	if isProtobuf(c) {
		return body, len(body), nil
	}
	req := &colprofilespb.ExportProfilesServiceRequest{}
	if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(body, req); err != nil {
		return nil, 0, fmt.Errorf("failed to unmarshal JSON: %w", err)
	}
	binary, err := proto.Marshal(req)
	if err != nil {
		return nil, 0, fmt.Errorf("failed to re-encode profiles request: %w", err)
	}
	return binary, len(body), nil
}

func writeProfilesResponse(c *gin.Context) {
	resp := &colprofilespb.ExportProfilesServiceResponse{}
	if isProtobuf(c) {
		data, _ := proto.Marshal(resp)
		c.Data(http.StatusOK, "application/x-protobuf", data)
	} else {
		data, _ := protojson.Marshal(resp)
		c.Data(http.StatusOK, "application/json", data)
	}
}

func decodeLogsRequest(c *gin.Context) (*collogspb.ExportLogsServiceRequest, int, error) {
	body, err := readBody(c)
	if err != nil {
		return nil, 0, err
	}
	req := &collogspb.ExportLogsServiceRequest{}
	if isProtobuf(c) {
		if err := proto.Unmarshal(body, req); err != nil {
			return nil, 0, fmt.Errorf("failed to unmarshal protobuf: %w", err)
		}
	} else {
		normalized, err := normalizeOTLPJSON(body, req.ProtoReflect().Descriptor())
		if err != nil {
			return nil, 0, fmt.Errorf("failed to decode OTLP JSON: %w", err)
		}
		if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(normalized, req); err != nil {
			return nil, 0, fmt.Errorf("failed to unmarshal JSON: %w", err)
		}
	}
	return req, len(body), nil
}

func writeLogsResponse(c *gin.Context) {
	resp := &collogspb.ExportLogsServiceResponse{}
	if isProtobuf(c) {
		data, _ := proto.Marshal(resp)
		c.Data(http.StatusOK, "application/x-protobuf", data)
	} else {
		data, _ := protojson.Marshal(resp)
		c.Data(http.StatusOK, "application/json", data)
	}
}