Skip to content

Commit

Permalink
chore: consistently use otelx.End()
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Sep 11, 2024
1 parent 554238b commit 0864d49
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 218 deletions.
68 changes: 27 additions & 41 deletions hsm/manager_hsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/go-jose/go-jose/v3"
"github.com/go-jose/go-jose/v3/cryptosigner"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

const tracingComponent = "github.com/ory/hydra/hsm"
Expand All @@ -58,23 +60,21 @@ func NewKeyManager(hsm Context, config *config.DefaultProvider) *KeyManager {
}
}

func (m *KeyManager) GenerateAndPersistKeySet(ctx context.Context, set, kid, alg, use string) (*jose.JSONWebKeySet, error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GenerateAndPersistKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
"kid": kid,
"alg": alg,
"use": use,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m *KeyManager) GenerateAndPersistKeySet(ctx context.Context, set, kid, alg, use string) (_ *jose.JSONWebKeySet, err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GenerateAndPersistKeySet",
trace.WithAttributes(
attribute.String("set", set),
attribute.String("kid", kid),
attribute.String("alg", alg),
attribute.String("use", use)))
defer otelx.End(span, &err)

m.Lock()
defer m.Unlock()

set = m.prefixKeySet(set)

err := m.deleteExistingKeySet(set)
err = m.deleteExistingKeySet(set)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,14 +119,10 @@ func (m *KeyManager) GenerateAndPersistKeySet(ctx context.Context, set, kid, alg
}
}

func (m *KeyManager) GetKey(ctx context.Context, set, kid string) (*jose.JSONWebKeySet, error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GetKey")
defer span.End()
attrs := map[string]string{
"set": set,
"kid": kid,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m *KeyManager) GetKey(ctx context.Context, set, kid string) (_ *jose.JSONWebKeySet, err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GetKey",
trace.WithAttributes(attribute.String("set", set), attribute.String("kid", kid)))
defer otelx.End(span, &err)

m.RLock()
defer m.RUnlock()
Expand All @@ -150,13 +146,9 @@ func (m *KeyManager) GetKey(ctx context.Context, set, kid string) (*jose.JSONWeb
return createKeySet(keyPair, id, alg, use)
}

func (m *KeyManager) GetKeySet(ctx context.Context, set string) (*jose.JSONWebKeySet, error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GetKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m *KeyManager) GetKeySet(ctx context.Context, set string) (_ *jose.JSONWebKeySet, err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.GetKeySet", trace.WithAttributes(attribute.String("set", set)))
otelx.End(span, &err)

m.RLock()
defer m.RUnlock()
Expand Down Expand Up @@ -186,14 +178,12 @@ func (m *KeyManager) GetKeySet(ctx context.Context, set string) (*jose.JSONWebKe
}, nil
}

func (m *KeyManager) DeleteKey(ctx context.Context, set, kid string) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.DeleteKey")
defer span.End()
attrs := map[string]string{
"set": set,
"kid": kid,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m *KeyManager) DeleteKey(ctx context.Context, set, kid string) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.DeleteKey",
trace.WithAttributes(
attribute.String("set", set),
attribute.String("kid", kid)))
defer otelx.End(span, &err)

m.Lock()
defer m.Unlock()
Expand All @@ -216,13 +206,9 @@ func (m *KeyManager) DeleteKey(ctx context.Context, set, kid string) error {
return nil
}

func (m *KeyManager) DeleteKeySet(ctx context.Context, set string) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.DeleteKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m *KeyManager) DeleteKeySet(ctx context.Context, set string) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "hsm.DeleteKeySet", trace.WithAttributes(attribute.String("set", set)))
defer otelx.End(span, &err)

m.Lock()
defer m.Unlock()
Expand Down
112 changes: 42 additions & 70 deletions jwk/manager_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/go-jose/go-jose/v3"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/ory/hydra/v2/x"
"github.com/ory/x/otelx"
Expand All @@ -28,72 +30,52 @@ func NewManagerStrategy(hardwareKeyManager Manager, softwareKeyManager Manager)
}
}

func (m ManagerStrategy) GenerateAndPersistKeySet(ctx context.Context, set, kid, alg, use string) (*jose.JSONWebKeySet, error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.GenerateAndPersistKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
"kid": kid,
"alg": alg,
"use": use,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) GenerateAndPersistKeySet(ctx context.Context, set, kid, alg, use string) (_ *jose.JSONWebKeySet, err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.GenerateAndPersistKeySet",
trace.WithAttributes(
attribute.String("set", set),
attribute.String("kid", kid),
attribute.String("alg", alg),
attribute.String("use", use)))
defer otelx.End(span, &err)

return m.hardwareKeyManager.GenerateAndPersistKeySet(ctx, set, kid, alg, use)
}

func (m ManagerStrategy) AddKey(ctx context.Context, set string, key *jose.JSONWebKey) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.AddKey")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) AddKey(ctx context.Context, set string, key *jose.JSONWebKey) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.AddKey", trace.WithAttributes(attribute.String("set", set)))
defer otelx.End(span, &err)

return m.softwareKeyManager.AddKey(ctx, set, key)
}

func (m ManagerStrategy) AddKeySet(ctx context.Context, set string, keys *jose.JSONWebKeySet) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.AddKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) AddKeySet(ctx context.Context, set string, keys *jose.JSONWebKeySet) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.AddKeySet", trace.WithAttributes(attribute.String("set", set)))
otelx.End(span, &err)

return m.softwareKeyManager.AddKeySet(ctx, set, keys)
}

func (m ManagerStrategy) UpdateKey(ctx context.Context, set string, key *jose.JSONWebKey) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.UpdateKey")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) UpdateKey(ctx context.Context, set string, key *jose.JSONWebKey) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.UpdateKey", trace.WithAttributes(attribute.String("set", set)))
defer otelx.End(span, &err)

return m.softwareKeyManager.UpdateKey(ctx, set, key)
}

func (m ManagerStrategy) UpdateKeySet(ctx context.Context, set string, keys *jose.JSONWebKeySet) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.UpdateKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) UpdateKeySet(ctx context.Context, set string, keys *jose.JSONWebKeySet) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.UpdateKeySet", trace.WithAttributes(attribute.String("set", set)))
defer otelx.End(span, &err)

return m.softwareKeyManager.UpdateKeySet(ctx, set, keys)
}

func (m ManagerStrategy) GetKey(ctx context.Context, set, kid string) (*jose.JSONWebKeySet, error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.GetKey")
defer span.End()
attrs := map[string]string{
"set": set,
"kid": kid,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) GetKey(ctx context.Context, set, kid string) (_ *jose.JSONWebKeySet, err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.GetKey",
trace.WithAttributes(
attribute.String("set", set),
attribute.String("kid", kid)))
defer otelx.End(span, &err)

keySet, err := m.hardwareKeyManager.GetKey(ctx, set, kid)
if err != nil && !errors.Is(err, x.ErrNotFound) {
Expand All @@ -105,13 +87,9 @@ func (m ManagerStrategy) GetKey(ctx context.Context, set, kid string) (*jose.JSO
}
}

func (m ManagerStrategy) GetKeySet(ctx context.Context, set string) (*jose.JSONWebKeySet, error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.GetKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) GetKeySet(ctx context.Context, set string) (_ *jose.JSONWebKeySet, err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.GetKeySet", trace.WithAttributes(attribute.String("set", set)))
defer otelx.End(span, &err)

keySet, err := m.hardwareKeyManager.GetKeySet(ctx, set)
if err != nil && !errors.Is(err, x.ErrNotFound) {
Expand All @@ -123,16 +101,14 @@ func (m ManagerStrategy) GetKeySet(ctx context.Context, set string) (*jose.JSONW
}
}

func (m ManagerStrategy) DeleteKey(ctx context.Context, set, kid string) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.DeleteKey")
defer span.End()
attrs := map[string]string{
"set": set,
"kid": kid,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) DeleteKey(ctx context.Context, set, kid string) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.DeleteKey",
trace.WithAttributes(
attribute.String("set", set),
attribute.String("kid", kid)))
defer otelx.End(span, &err)

err := m.hardwareKeyManager.DeleteKey(ctx, set, kid)
err = m.hardwareKeyManager.DeleteKey(ctx, set, kid)
if err != nil && !errors.Is(err, x.ErrNotFound) {
return err
} else if errors.Is(err, x.ErrNotFound) {
Expand All @@ -142,15 +118,11 @@ func (m ManagerStrategy) DeleteKey(ctx context.Context, set, kid string) error {
}
}

func (m ManagerStrategy) DeleteKeySet(ctx context.Context, set string) error {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.DeleteKeySet")
defer span.End()
attrs := map[string]string{
"set": set,
}
span.SetAttributes(otelx.StringAttrs(attrs)...)
func (m ManagerStrategy) DeleteKeySet(ctx context.Context, set string) (err error) {
ctx, span := otel.GetTracerProvider().Tracer(tracingComponent).Start(ctx, "jwk.DeleteKeySet", trace.WithAttributes(attribute.String("set", set)))
defer otelx.End(span, &err)

err := m.hardwareKeyManager.DeleteKeySet(ctx, set)
err = m.hardwareKeyManager.DeleteKeySet(ctx, set)
if err != nil && !errors.Is(err, x.ErrNotFound) {
return err
} else if errors.Is(err, x.ErrNotFound) {
Expand Down
Loading

0 comments on commit 0864d49

Please sign in to comment.