snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped

This commit is contained in:
upstream-mirror
2026-09-16 01:08:34 +05:00
commit 9456a4e922
2560 changed files with 120911 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package semconv
import (
"context"
"net/http"
"go.opentelemetry.io/otel/attribute"
"github.com/ory/x/httpx"
)
type contextKey int
const contextKeyAttributes contextKey = iota
func ContextWithAttributes(ctx context.Context, attrs ...attribute.KeyValue) context.Context {
existing, _ := ctx.Value(contextKeyAttributes).([]attribute.KeyValue)
return context.WithValue(ctx, contextKeyAttributes, append(existing, attrs...))
}
func AttributesFromContext(ctx context.Context) []attribute.KeyValue {
fromCtx, _ := ctx.Value(contextKeyAttributes).([]attribute.KeyValue)
uniq := make(map[attribute.Key]struct{})
attrs := make([]attribute.KeyValue, 0)
for i := len(fromCtx) - 1; i >= 0; i-- {
if _, ok := uniq[fromCtx[i].Key]; !ok {
uniq[fromCtx[i].Key] = struct{}{}
attrs = append(attrs, fromCtx[i])
}
}
reverse(attrs)
return attrs
}
func Middleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
ctx := ContextWithAttributes(r.Context(),
append(
AttrGeoLocation(*httpx.ClientGeoLocation(r)),
AttrClientIP(httpx.ClientIP(r)),
)...,
)
next(rw, r.WithContext(ctx))
}
func reverse[S ~[]E, E any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}

View File

@@ -0,0 +1,38 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package semconv
import (
"context"
otelattr "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// NewDeprecatedFeatureUsedEvent creates a new event indicating that a deprecated feature was used.
// It returns the event name and a trace.EventOption that can be used to
// add the event to a span.
//
// span.AddEvent(NewDeprecatedFeatureUsedEvent(ctx, "deprecated-feature-id", otelattr.String("key", "value")))
func NewDeprecatedFeatureUsedEvent(ctx context.Context, deprecatedCodeFeatureID string, attrs ...otelattr.KeyValue) (string, trace.EventOption) {
return DeprecatedFeatureUsed.String(),
trace.WithAttributes(
append(
append(
attrs,
AttributesFromContext(ctx)...,
),
AttrDeprecatedFeatureID(deprecatedCodeFeatureID),
)...,
)
}
const (
AttributeKeyDeprecatedCodePathIDAttributeKey AttributeKey = "DeprecatedFeatureID"
DeprecatedFeatureUsed Event = "DeprecatedFeatureUsed"
)
func AttrDeprecatedFeatureID(id string) otelattr.KeyValue {
return otelattr.String(AttributeKeyDeprecatedCodePathIDAttributeKey.String(), id)
}

View File

@@ -0,0 +1,104 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
// Package semconv contains OpenTelemetry semantic convention constants.
package semconv
import (
"github.com/gofrs/uuid"
otelattr "go.opentelemetry.io/otel/attribute"
"github.com/ory/x/httpx"
)
type Event string
func (e Event) String() string {
return string(e)
}
type AttributeKey string
func (a AttributeKey) String() string {
return string(a)
}
const (
AttributeKeyIdentityID AttributeKey = "IdentityID"
AttributeKeyNID AttributeKey = "ProjectID"
AttributeKeyClientIP AttributeKey = "ClientIP"
AttributeKeyGeoLocationCity AttributeKey = "GeoLocationCity"
AttributeKeyGeoLocationRegion AttributeKey = "GeoLocationRegion"
AttributeKeyGeoLocationCountry AttributeKey = "GeoLocationCountry"
AttributeKeyGeoLocationLatitude AttributeKey = "GeoLocationLatitude"
AttributeKeyGeoLocationLongitude AttributeKey = "GeoLocationLongitude"
AttributeKeyWorkspace AttributeKey = "WorkspaceID"
AttributeKeySubscriptionID AttributeKey = "SubscriptionID"
AttributeKeyProjectEnvironment AttributeKey = "ProjectEnvironment"
AttributeKeyWorkspaceAPIKeyID AttributeKey = "WorkspaceAPIKeyID"
AttributeKeyProjectAPIKeyID AttributeKey = "ProjectAPIKeyID"
)
func AttrIdentityID[V string | uuid.UUID](val V) otelattr.KeyValue {
return otelattr.String(AttributeKeyIdentityID.String(), uuidOrString(val))
}
func AttrNID(val uuid.UUID) otelattr.KeyValue {
return otelattr.String(AttributeKeyNID.String(), val.String())
}
func AttrWorkspace(val uuid.UUID) otelattr.KeyValue {
return otelattr.String(AttributeKeyWorkspace.String(), val.String())
}
func AttrSubscription(val uuid.UUID) otelattr.KeyValue {
return otelattr.String(AttributeKeySubscriptionID.String(), val.String())
}
func AttrProjectEnvironment(val string) otelattr.KeyValue {
return otelattr.String(AttributeKeyProjectEnvironment.String(), val)
}
func AttrClientIP(val string) otelattr.KeyValue {
return otelattr.String(AttributeKeyClientIP.String(), val)
}
func AttrGeoLocation(val httpx.GeoLocation) []otelattr.KeyValue {
geoLocationAttributes := make([]otelattr.KeyValue, 0, 3)
if val.City != "" {
geoLocationAttributes = append(geoLocationAttributes, otelattr.String(AttributeKeyGeoLocationCity.String(), val.City))
}
if val.Country != "" {
geoLocationAttributes = append(geoLocationAttributes, otelattr.String(AttributeKeyGeoLocationCountry.String(), val.Country))
}
if val.Region != "" {
geoLocationAttributes = append(geoLocationAttributes, otelattr.String(AttributeKeyGeoLocationRegion.String(), val.Region))
}
if val.Latitude != nil {
geoLocationAttributes = append(geoLocationAttributes, otelattr.Float64(AttributeKeyGeoLocationLatitude.String(), *val.Latitude))
}
if val.Longitude != nil {
geoLocationAttributes = append(geoLocationAttributes, otelattr.Float64(AttributeKeyGeoLocationLongitude.String(), *val.Longitude))
}
return geoLocationAttributes
}
func AttrWorkspaceAPIKeyID[V string | uuid.UUID](val V) otelattr.KeyValue {
return otelattr.String(AttributeKeyWorkspaceAPIKeyID.String(), uuidOrString(val))
}
func AttrProjectAPIKeyID[V string | uuid.UUID](val V) otelattr.KeyValue {
return otelattr.String(AttributeKeyProjectAPIKeyID.String(), uuidOrString(val))
}
func uuidOrString[V string | uuid.UUID](val V) string {
switch val := any(val).(type) {
case string:
return val
case uuid.UUID:
return val.String()
}
panic("unreachable")
}

View File

@@ -0,0 +1,38 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package semconv
import (
"context"
otelattr "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// NewWarning creates a new warning event with the given ID and attributes.
// It returns the event name and a trace.EventOption that can be used to
// add the event to a span.
//
// span.AddEvent(NewWarning(ctx, "warning-id", otelattr.String("key", "value")))
func NewWarning(ctx context.Context, id string, attrs ...otelattr.KeyValue) (string, trace.EventOption) {
return Warning.String(),
trace.WithAttributes(
append(
append(
attrs,
AttributesFromContext(ctx)...,
),
otelattr.String(AttributeWarningID.String(), id),
)...,
)
}
const (
Warning Event = "Warning"
AttributeWarningID AttributeKey = "WarningID"
)
func AttrWarningID(id string) otelattr.KeyValue {
return otelattr.String(AttributeWarningID.String(), id)
}