hs-opentelemetry-api
Copyright(c) Ian Duncan 2021-2026
LicenseBSD-3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

OpenTelemetry.Attributes

Description

Overview

Attributes are key-value pairs attached to spans, events, links, and resources. Keys are Text strings; values are one of the OpenTelemetry primitive types: Text, Bool, Int64, Double, or arrays thereof.

Quick example

import OpenTelemetry.Attributes

-- On a span:
addAttribute span "http.request.method" (toAttribute GET)
addAttribute span "http.response.status_code" (toAttribute (200 :: Int))
addAttributes span
  [ ("user.id", toAttribute "abc123")
  , ("user.role", toAttribute "admin")
  ]

-- Using the builder API:
let attrs = buildAttrs $
      attr "http.request.method" (GET :: Text)
      <> attr "http.response.status_code" (200 :: Int)

Typed attribute keys

For type-safe attribute access, use AttributeKey:

import OpenTelemetry.Attributes.Key (AttributeKey(..), unkey)
import qualified OpenTelemetry.SemanticConventions as SC

let attrs' = addAttributeByKey defaultAttributeLimits attrs SC.http_request_method GET
lookupAttributeByKey attrs' SC.http_response_statusCode  -- Maybe Int64

Attribute limits

The SDK enforces limits on attribute count and value length, configured via OTEL_ATTRIBUTE_COUNT_LIMIT and OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT environment variables. When limits are exceeded, attributes are dropped (tracked by getDropped).

Spec reference

https://opentelemetry.io/docs/specs/otel/common/#attribute

Synopsis

Documentation

data Attributes Source #

Since: 0.0.1.0

Instances

Instances details
Generic Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

Associated Types

type Rep Attributes 
Instance details

Defined in OpenTelemetry.Attributes

type Rep Attributes = D1 ('MetaData "Attributes" "OpenTelemetry.Attributes" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "Attributes" 'PrefixI 'True) (S1 ('MetaSel ('Just "attributeMap") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 AttributeMap) :*: (S1 ('MetaSel ('Just "attributesCount") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "attributesDropped") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Int))))
Show Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

Eq Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

Ord Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

Hashable Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

Lift Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

Methods

lift :: Quote m => Attributes -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => Attributes -> Code m Attributes #

type Rep Attributes Source # 
Instance details

Defined in OpenTelemetry.Attributes

type Rep Attributes = D1 ('MetaData "Attributes" "OpenTelemetry.Attributes" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "Attributes" 'PrefixI 'True) (S1 ('MetaSel ('Just "attributeMap") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 AttributeMap) :*: (S1 ('MetaSel ('Just "attributesCount") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "attributesDropped") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Int))))

unsafeAttributesFromMap :: AttributeLimits -> AttributeMap -> Attributes Source #

Build Attributes directly from a pre-built AttributeMap, applying count and length limits. Faster than addAttributes limits emptyAttributes map because it skips the per-key membership check against an empty base map.

Since: 0.4.0.0

addAttributesFromBuilder :: AttributeLimits -> Attributes -> AttrsBuilder -> Attributes Source #

Like addAttributes, but consumes an AttrsBuilder instead of a HashMap. Folds each attribute directly into the existing Attributes without allocating an intermediate collection.

Since: 0.4.0.0

getCount :: Attributes -> Int Source #

Since: 0.0.1.0

getDropped :: Attributes -> Int Source #

Since: 0.0.1.0

data Attribute #

Instances

Instances details
Data Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Attribute -> c Attribute #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Attribute #

toConstr :: Attribute -> Constr #

dataTypeOf :: Attribute -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Attribute) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Attribute) #

gmapT :: (forall b. Data b => b -> b) -> Attribute -> Attribute #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Attribute -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Attribute -> r #

gmapQ :: (forall d. Data d => d -> u) -> Attribute -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Attribute -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Attribute -> m Attribute #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Attribute -> m Attribute #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Attribute -> m Attribute #

IsString Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Generic Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Associated Types

type Rep Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep Attribute = D1 ('MetaData "Attribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AttributeValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 PrimitiveAttribute)) :+: C1 ('MetaCons "AttributeArray" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 [PrimitiveAttribute])))
Read Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Show Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Eq Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Ord Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Hashable Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

FromAttribute Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

ToAttribute Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Lift Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Methods

lift :: Quote m => Attribute -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => Attribute -> Code m Attribute #

type Rep Attribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep Attribute = D1 ('MetaData "Attribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AttributeValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 PrimitiveAttribute)) :+: C1 ('MetaCons "AttributeArray" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 [PrimitiveAttribute])))

class ToAttribute a where #

Minimal complete definition

Nothing

Methods

toAttribute :: a -> Attribute #

data PrimitiveAttribute #

Instances

Instances details
Data PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> PrimitiveAttribute -> c PrimitiveAttribute #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c PrimitiveAttribute #

toConstr :: PrimitiveAttribute -> Constr #

dataTypeOf :: PrimitiveAttribute -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c PrimitiveAttribute) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrimitiveAttribute) #

gmapT :: (forall b. Data b => b -> b) -> PrimitiveAttribute -> PrimitiveAttribute #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> PrimitiveAttribute -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> PrimitiveAttribute -> r #

gmapQ :: (forall d. Data d => d -> u) -> PrimitiveAttribute -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> PrimitiveAttribute -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> PrimitiveAttribute -> m PrimitiveAttribute #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> PrimitiveAttribute -> m PrimitiveAttribute #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> PrimitiveAttribute -> m PrimitiveAttribute #

IsString PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Generic PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Associated Types

type Rep PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep PrimitiveAttribute = D1 ('MetaData "PrimitiveAttribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) ((C1 ('MetaCons "TextAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "BoolAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Bool))) :+: (C1 ('MetaCons "DoubleAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Double)) :+: C1 ('MetaCons "IntAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Int64))))
Read PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Show PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Eq PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Ord PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Hashable PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

FromAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

FromPrimitiveAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

ToAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

ToPrimitiveAttribute PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

Lift PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep PrimitiveAttribute 
Instance details

Defined in OpenTelemetry.Attributes.Attribute

type Rep PrimitiveAttribute = D1 ('MetaData "PrimitiveAttribute" "OpenTelemetry.Attributes.Attribute" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'False) ((C1 ('MetaCons "TextAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "BoolAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Bool))) :+: (C1 ('MetaCons "DoubleAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Double)) :+: C1 ('MetaCons "IntAttribute" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 Int64))))

newtype AttributeKey a #

Constructors

AttributeKey 

Fields

Instances

Instances details
IsString (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Generic (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Associated Types

type Rep (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

type Rep (AttributeKey a) = D1 ('MetaData "AttributeKey" "OpenTelemetry.Attributes.Key" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'True) (C1 ('MetaCons "AttributeKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unkey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

Methods

from :: AttributeKey a -> Rep (AttributeKey a) x #

to :: Rep (AttributeKey a) x -> AttributeKey a #

Show (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Eq (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Ord (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

type Rep (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

type Rep (AttributeKey a) = D1 ('MetaData "AttributeKey" "OpenTelemetry.Attributes.Key" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'True) (C1 ('MetaCons "AttributeKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unkey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

newtype AttributeKey a #

Constructors

AttributeKey 

Fields

Instances

Instances details
IsString (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Generic (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Associated Types

type Rep (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

type Rep (AttributeKey a) = D1 ('MetaData "AttributeKey" "OpenTelemetry.Attributes.Key" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'True) (C1 ('MetaCons "AttributeKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unkey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

Methods

from :: AttributeKey a -> Rep (AttributeKey a) x #

to :: Rep (AttributeKey a) x -> AttributeKey a #

Show (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Eq (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

Ord (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

type Rep (AttributeKey a) 
Instance details

Defined in OpenTelemetry.Attributes.Key

type Rep (AttributeKey a) = D1 ('MetaData "AttributeKey" "OpenTelemetry.Attributes.Key" "hs-opentelemetry-api-types-1.0.0.0-inplace" 'True) (C1 ('MetaCons "AttributeKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unkey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

Attribute builder

data AttrsBuilder Source #

Church-encoded left fold over attribute key-value pairs. Avoids allocating intermediate tuples, list spines, or HashMaps when adding multiple attributes to a span.

Construct individual entries with attr / .@ and combine with <>. GHC can inline and fuse static builder expressions, eliminating all intermediate allocation.

addAttributes' span $
    SC.http_request_method .@ method
 <> SC.url_full .@ url
 <> SC.server_port .@? mPort

Since: 0.4.0.0

attr :: ToAttribute a => Text -> a -> AttrsBuilder Source #

Build an attribute entry from a Text key. The value is converted to Attribute lazily; actual conversion is deferred until the exporter thread reads the attribute, keeping the instrumented thread fast.

Since: 0.4.0.0

optAttr :: ToAttribute a => Text -> Maybe a -> AttrsBuilder Source #

Build an optional attribute entry. Nothing contributes nothing to the builder (zero cost).

Since: 0.4.0.0

(.@) :: ToAttribute a => AttributeKey a -> a -> AttrsBuilder infixl 8 Source #

Build an attribute entry from a typed AttributeKey. Type-safe: the value type must match the key's phantom type.

SC.http_request_method .@ (GET :: Text)

Since: 0.4.0.0

(.@?) :: ToAttribute a => AttributeKey a -> Maybe a -> AttrsBuilder infixl 8 Source #

Build an optional attribute entry from a typed AttributeKey. Nothing contributes nothing to the builder.

Since: 0.4.0.0

buildAttrs :: AttrsBuilder -> AttributeMap Source #

Materialize a builder into an AttributeMap. Useful when a raw HashMap is needed (e.g. for NewEvent attributes or SpanArguments).

Since: 0.4.0.0

Attribute limits

data AttributeLimits Source #

It is possible when adding attributes that a programming error might cause too many attributes to be added to an event. Thus, Attributes use the limits set here as a safeguard against excessive memory consumption.

Since: 0.0.1.0

Constructors

AttributeLimits 

Fields

Instances

Instances details
Data AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> AttributeLimits -> c AttributeLimits #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c AttributeLimits #

toConstr :: AttributeLimits -> Constr #

dataTypeOf :: AttributeLimits -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c AttributeLimits) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AttributeLimits) #

gmapT :: (forall b. Data b => b -> b) -> AttributeLimits -> AttributeLimits #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> AttributeLimits -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> AttributeLimits -> r #

gmapQ :: (forall d. Data d => d -> u) -> AttributeLimits -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> AttributeLimits -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> AttributeLimits -> m AttributeLimits #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> AttributeLimits -> m AttributeLimits #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> AttributeLimits -> m AttributeLimits #

Generic AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

Associated Types

type Rep AttributeLimits 
Instance details

Defined in OpenTelemetry.Attributes

type Rep AttributeLimits = D1 ('MetaData "AttributeLimits" "OpenTelemetry.Attributes" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AttributeLimits" 'PrefixI 'True) (S1 ('MetaSel ('Just "attributeCountLimit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (Maybe Int)) :*: S1 ('MetaSel ('Just "attributeLengthLimit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (Maybe Int))))
Read AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

Show AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

Eq AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

Ord AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

Hashable AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

type Rep AttributeLimits Source # 
Instance details

Defined in OpenTelemetry.Attributes

type Rep AttributeLimits = D1 ('MetaData "AttributeLimits" "OpenTelemetry.Attributes" "hs-opentelemetry-api-1.0.0.0-inplace" 'False) (C1 ('MetaCons "AttributeLimits" 'PrefixI 'True) (S1 ('MetaSel ('Just "attributeCountLimit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (Maybe Int)) :*: S1 ('MetaSel ('Just "attributeLengthLimit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (Maybe Int))))

defaultAttributeLimits :: AttributeLimits Source #

Default attribute limits used in the global attribute limit configuration if no environment variables are set.

Values:

Since: 0.0.1.0

Unsafe utilities

unsafeAttributesFromMapIgnoringLimits :: HashMap Text Attribute -> Attributes Source #

Wrap a pre-built HashMap directly into Attributes with zero conversion. No limit enforcement. Used by filterAttributesByKeys to avoid a HashMap -> list -> HashMap roundtrip.

Since: 0.0.1.0