add grpc support

This commit is contained in:
2025-03-07 11:44:50 -05:00
parent 5aa5dda111
commit 98fba4eac8
4 changed files with 57 additions and 11 deletions

View File

@ -2,8 +2,10 @@ package grpc
import (
"context"
"strings"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
@ -38,12 +40,18 @@ func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) error {
ctx, span := appGRPC.tracer.Start(ctx, "appgrpc.init")
defer span.End()
// Prepare grpc.Server for use
if err := appGRPC.prepGRPCServer(ctx); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to prepare GRPC Server")
return err
}
// Load span with server info
span.SetAttributes(appGRPC.getServerAttributes()...)
// Run, returning a shutdown func and an error chan
// TODO: Implement shutdown func and error chan
err := appGRPC.runGRPCServer(ctx)
if err != nil {
span.RecordError(err)
@ -54,3 +62,31 @@ func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) error {
span.SetStatus(codes.Ok, "")
return nil
}
// Convert grpc.ServiceInfo map to []attribute.KeyValue
func (a *appGRPCServer) getServerAttributes() []attribute.KeyValue {
var attrs []attribute.KeyValue
for serviceName, info := range a.server.GetServiceInfo() {
// Add the service name
attrs = append(attrs, attribute.String("grpc.service", serviceName))
// Convert methods into a comma-separated string
var methods []string
for _, method := range info.Methods {
methods = append(methods, method.Name)
}
// Add methods if present
if len(methods) > 0 {
attrs = append(attrs, attribute.String("grpc.service.methods", strings.Join(methods, ",")))
}
// If metadata is a string, store it
if metadata, ok := info.Metadata.(string); ok && metadata != "" {
attrs = append(attrs, attribute.String("grpc.service.metadata", metadata))
}
}
return attrs
}