Files
coder/aibridge/tracing/tracing.go
T
Paweł Banaszewski e00e85765b chore: move aibridge library code into coder repo (#24190)
This PR merges code from `coder/aibridge` repository into `coder/coder`.
It was split into 4 PRs for easier review but stacked PRs will need to
be merged into this PR so all checks pass.

* https://github.com/coder/coder/pull/24190 -> raw code copy (this PR,
before merging PRs on top of it, it was just 1 commit:
https://github.com/coder/coder/commit/70d33f33200c7e77df910957595715f81f9bec24)
* https://github.com/coder/coder/pull/24570 -> update imports in
`coder/coder` to use copied code
* https://github.com/coder/coder/pull/24586 -> linter fixes and CI
integration (also added README.md)
* https://github.com/coder/coder/pull/24571 -> added exclude to
scripts/check_emdash.sh check

Original PR message (before PR squash):
Moves coder/aibridge code into coder/coder repository.

Omitted files:

- `go.mod`, `go.sum`, `.gitignore`, `.github/workflows/ci.yml,`
`Makefile`, `LICENSE`, `README.md` (modified README.md is added later)
- `.github`, `example`, `buildinfo,` `scripts` directories

Simple verification script (will list omitted files)

```
tmp=$(mktemp -d)
echo "$tmp"
git clone --depth=1 https://github.com/coder/aibridge "$tmp/aibridge"
git clone --depth=1 --branch pb/aibridge-code-move https://github.com/coder/coder "$tmp/coder"
diff -rq --exclude=.git "$tmp/aibridge" "$tmp/coder/aibridge"
# rm -rf "$tmp"
```
2026-04-22 17:01:01 +02:00

88 lines
2.2 KiB
Go

package tracing
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type (
traceInterceptionAttrsContextKey struct{}
traceRequestBridgeAttrsContextKey struct{}
)
const (
// trace attribute key constants
RequestPath = "request_path"
InterceptionID = "interception_id"
InitiatorID = "user_id"
Provider = "provider"
Model = "model"
Streaming = "streaming"
IsBedrock = "aws_bedrock"
PassthroughURL = "passthrough_url"
PassthroughUpstreamURL = "passthrough_upstream_url"
PassthroughMethod = "passthrough_method"
MCPInput = "mcp_input"
MCPProxyName = "mcp_proxy_name"
MCPToolName = "mcp_tool_name"
MCPServerName = "mcp_server_name"
MCPServerURL = "mcp_server_url"
MCPToolCount = "mcp_tool_count"
APIKeyID = "api_key_id"
)
// EndSpanErr ends given span and sets Error status if error is not nil
// uses pointer to error because defer evaluates function arguments
// when defer statement is executed not when deferred function is called
//
// example usage:
//
// func Example() (result any, outErr error) {
// _, span := tracer.Start(...)
// defer tracing.EndSpanErr(span, &outErr)
//
// }
func EndSpanErr(span trace.Span, err *error) {
if span == nil {
return
}
if err != nil && *err != nil {
span.SetStatus(codes.Error, (*err).Error())
}
span.End()
}
func WithInterceptionAttributesInContext(ctx context.Context, traceAttrs []attribute.KeyValue) context.Context {
return context.WithValue(ctx, traceInterceptionAttrsContextKey{}, traceAttrs)
}
func InterceptionAttributesFromContext(ctx context.Context) []attribute.KeyValue {
attrs, ok := ctx.Value(traceInterceptionAttrsContextKey{}).([]attribute.KeyValue)
if !ok {
return nil
}
return attrs
}
func WithRequestBridgeAttributesInContext(ctx context.Context, traceAttrs []attribute.KeyValue) context.Context {
return context.WithValue(ctx, traceRequestBridgeAttrsContextKey{}, traceAttrs)
}
func RequestBridgeAttributesFromContext(ctx context.Context) []attribute.KeyValue {
attrs, ok := ctx.Value(traceRequestBridgeAttrsContextKey{}).([]attribute.KeyValue)
if !ok {
return nil
}
return attrs
}