shield360

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 42 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Shield360 Go SDK

OpenTelemetry-native observability SDK for LLM applications in Go. Monitor your AI applications with automatic instrumentation for popular LLM providers.

Go Reference Go Report Card

Features

  • 🚀 One-line initialization - Get started with minimal code
  • 📊 OpenTelemetry-native - Built on OpenTelemetry standards
  • 💰 Cost tracking - Automatic cost calculation for LLM requests
  • 📈 Metrics collection - Token usage, latency, and performance metrics
  • 🔄 Streaming support - Full support for streaming responses
  • 🔌 Flexible integration - Works with multiple Go client libraries

Supported Integrations

  • ✅ OpenAI - Chat completions, embeddings, images, audio
  • ✅ Anthropic - Messages API with tool calling support
  • ✅ vLLM - OpenAI-compatible chat completions (local / self-hosted)

Installation

go get github.com/Shield360-AI/shield360-go

Quick Start

1. Initialize Shield360
package main

import (
    "context"
    "log"
    
    "github.com/Shield360-AI/shield360-go"
)

func main() {
    // Initialize Shield360
    err := shield360.Init(shield360.Config{
        OtlpEndpoint:    "http://127.0.0.1:4318",
        Environment:     "production",
        ApplicationName: "my-go-app",
    })
    if err != nil {
        log.Fatalf("Failed to initialize Shield360: %v", err)
    }
    defer shield360.Shutdown(context.Background())
    
    // Your application code here
}
2. Instrument OpenAI
import (
    "github.com/Shield360-AI/shield360-go/instrumentation/openai"
    openai_sdk "github.com/sashabaranov/go-openai"
)

// Create and instrument OpenAI client
client := openai_sdk.NewClient("your-api-key")
instrumentedClient := openai.Instrument(client)

// Use as normal - automatically traced!
resp, err := instrumentedClient.CreateChatCompletion(ctx, openai_sdk.ChatCompletionRequest{
    Model: openai_sdk.GPT4,
    Messages: []openai_sdk.ChatCompletionMessage{
        {
            Role:    openai_sdk.ChatMessageRoleUser,
            Content: "Hello!",
        },
    },
})
3. Instrument Anthropic
import (
    "github.com/Shield360-AI/shield360-go/instrumentation/anthropic"
)

// Create and instrument Anthropic client
client := anthropic.NewClient("your-api-key")
instrumentedClient := anthropic.Instrument(client)

// Use as normal - automatically traced!
resp, err := instrumentedClient.CreateMessage(ctx, anthropic.MessageRequest{
    Model: "claude-3-5-sonnet-20241022",
    Messages: []anthropic.Message{
        {
            Role:    "user",
            Content: "Hello!",
        },
    },
    MaxTokens: 1024,
})
4. Instrument vLLM

vLLM exposes an OpenAI-compatible HTTP API. Point the instrumented client at your vLLM server (default http://127.0.0.1:8000/v1). Spans use gen_ai.system=vllm so they are distinct from OpenAI.

import (
    "github.com/Shield360-AI/shield360-go/instrumentation/vllm"
)

client := vllm.NewClient("http://127.0.0.1:8000/v1") // optional: vllm.WithAPIKey("...")

resp, err := client.CreateChatCompletion(ctx, vllm.ChatCompletionRequest{
    Model: "meta-llama/Llama-3-8B-Instruct",
    Messages: []vllm.ChatMessage{
        {Role: "user", Content: "Hello!"},
    },
})

Configuration Options

config := shield360.Config{
    // Required
    OtlpEndpoint:    "http://127.0.0.1:4318",  // OTLP endpoint; default OTEL_EXPORTER_OTLP_ENDPOINT, else <Shield360URL>/api/ingest/otlp with the API key as bearer, else this
    
    // Optional
    Environment:     "production",              // Deployment environment
    ApplicationName: "my-go-app",              // Application name
    ServiceVersion:  "1.0.0",                  // Service version
    
    // OTLP Configuration
    OtlpHeaders:     map[string]string{},      // Custom OTLP headers
    
    // Feature Flags
    DisableTracing:  false,                    // Disable tracing
    DisableMetrics:  false,                    // Disable metrics
    DisableBatch:    false,                    // Disable batch processing
    CaptureMessageContent: false,              // Record prompt/completion text (off by default)
    CaptureLogs:     nil,                      // *bool: send slog records to Shield360 (off by default)
    LogLevel:        "",                       // Lowest level sent when CaptureLogs is on (info)
    
    // Timeouts
    TraceExporterTimeout:  10 * time.Second,   // Trace export timeout
    MetricExporterTimeout: 10 * time.Second,   // Metric export timeout
    MetricExportInterval:  30 * time.Second,   // Metric export interval
    
    // Pricing
    PricingEndpoint:    "",                    // Custom pricing endpoint
    DisablePricingFetch: false,                // Disable pricing fetch
    PricingInfo:        map[string]ModelPricing{}, // Custom pricing
}

Message content is off by default. Prompt and completion text is recorded only with CaptureMessageContent: true (or OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true); DisableCaptureMessageContent still turns it off. Even then, Shield360 stores it only if your organization's Data retention setting allows it. Before this release the default was on.

Streaming Support

OpenAI, Anthropic, and vLLM integrations support streaming responses:

OpenAI Streaming
stream, err := instrumentedClient.CreateChatCompletionStream(ctx, request)
if err != nil {
    log.Fatal(err)
}
defer stream.Close()

for {
    response, err := stream.Recv()
    if errors.Is(err, io.EOF) {
        break
    }
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf(response.Choices[0].Delta.Content)
}
Anthropic Streaming
stream, err := instrumentedClient.CreateMessageStream(ctx, request)
if err != nil {
    log.Fatal(err)
}
defer stream.Close()

for {
    event, err := stream.Recv()
    if errors.Is(err, io.EOF) {
        break
    }
    if err != nil {
        log.Fatal(err)
    }
    
    // Handle event
}
vLLM Streaming
stream, err := client.CreateChatCompletionStream(ctx, vllm.ChatCompletionRequest{
    Model:    "meta-llama/Llama-3-8B-Instruct",
    Messages: []vllm.ChatMessage{{Role: "user", Content: "Hello!"}},
})
if err != nil {
    log.Fatal(err)
}
defer stream.Close()

for {
    chunk, err := stream.Recv()
    if errors.Is(err, io.EOF) {
        break
    }
    if err != nil {
        log.Fatal(err)
    }
    if len(chunk.Choices) > 0 {
        fmt.Print(chunk.Choices[0].Delta.Content)
    }
}

Collected Telemetry

Traces
  • Operation name and type (chat, embedding, etc.)
  • Request and response models
  • Input/output messages
  • Token usage (input, output, total)
  • Cost calculations
  • Response times
  • Error details
Metrics
  • gen_ai.client.token.usage - Token usage counter
  • gen_ai.client.operation.duration - Operation duration histogram
  • gen_ai.server.time_to_first_token - TTFT for streaming
  • gen_ai.server.time_per_output_token - TBT for streaming
Attributes

All traces include standard OpenTelemetry semantic conventions:

  • gen_ai.operation.name
  • gen_ai.system
  • gen_ai.request.model
  • gen_ai.response.model
  • gen_ai.usage.input_tokens
  • gen_ai.usage.output_tokens
  • server.address
  • server.port
Code location and build attributes

Every GenAI span records where in your code the model call was made:

  • code.function.name: the fully qualified Go function, for example github.com/acme/api/billing.Summarise
  • code.file.path: the file, relative to your repository
  • code.line.number: the line of the call

Frames inside this SDK, OpenTelemetry, the Go runtime and standard library, and the provider client libraries are skipped. Only names, paths and line numbers are recorded, never argument values or source text.

File paths are made repo-relative in this order: Config.CodeRoot (or SHIELD360_CODE_ROOT), the main module path in -trimpath builds, then the working directory at Init. A home directory prefix is always removed, and so is a CI workspace prefix (such as /home/runner/work/<repo>/<repo>/) left in a binary built without -trimpath. Add your own wrapper packages to Config.CodeLocationIgnoreModules to report their callers instead.

Turn it off with SHIELD360_CAPTURE_CODE_LOCATION=false (or 0, no, off) or CaptureCodeLocation: &off in Config. The config field wins over the environment variable.

The same call site is also recorded on the app's outbound calls: database, HTTP, RPC and messaging client and producer spans. Shield360 uses it to price the costly code patterns shield360 scan finds, such as a query inside a loop. Turn just that off with SHIELD360_CAPTURE_CALL_LOCATION=false or CaptureCallLocation: &off; model calls keep their location.

Init also sets these resource attributes, so every span carries the release it came from:

Attribute Sources, highest precedence first
service.version Config.ServiceVersion, SHIELD360_SERVICE_VERSION, OTEL_SERVICE_VERSION, OTEL_RESOURCE_ATTRIBUTES
vcs.ref.head.revision SHIELD360_GIT_COMMIT, OTEL_RESOURCE_ATTRIBUTES, the vcs.revision that go build embeds, CI variables
vcs.repository.url.full SHIELD360_GIT_REPOSITORY_URL, OTEL_RESOURCE_ATTRIBUTES, CI variables
vcs.ref.head.name SHIELD360_GIT_BRANCH, OTEL_RESOURCE_ATTRIBUTES, CI variables

CI variables are read from GitHub Actions, GitLab CI, Vercel, Netlify, Render, Railway, Heroku, AWS CodeBuild, Buildkite, CircleCI, Bitbucket Pipelines, Azure Pipelines and Jenkins. An explicit OTEL_RESOURCE_ATTRIBUTES value beats CI auto-detection. Only the first provider with a commit set is used. Commits must be 7 to 64 hex characters. Repository URLs are normalized to https://host/owner/repo, with credentials, query strings and a trailing .git removed.

Advanced Usage

Custom Pricing

Provide custom pricing information for models:

config := shield360.Config{
    PricingInfo: map[string]shield360.ModelPricing{
        "gpt-4-custom": {
            InputCostPerToken:  0.00003,
            OutputCostPerToken: 0.00006,
        },
    },
}
Custom Headers

Add custom headers to OTLP exports:

config := shield360.Config{
    OtlpHeaders: map[string]string{
        "Authorization": "Bearer token",
        "X-Custom-Header": "value",
    },
}

Integration with Shield360 Dashboard

The Go SDK works seamlessly with the Shield360 Dashboard:

  1. Start Shield360 stack:
docker compose up -d
  1. Configure SDK to send data:
shield360.Init(shield360.Config{
    OtlpEndpoint: "http://localhost:4318",
})
  1. View your traces at http://localhost:3000

Examples

See the examples/ directory for complete working examples:

Rule Engine - shield360.EvaluateRule()

Evaluate trace attributes against the Shield360 Rule Engine to retrieve matching rules and associated entities (contexts, prompts, evaluation configurations). This function does not require shield360.Init() — it is a standalone HTTP call.

Parameters (EvaluateRuleOptions)
Field Type Description
URL string Shield360 dashboard URL. Falls back to SHIELD360_URL env, then http://127.0.0.1:3000.
APIKey string Bearer token. Falls back to SHIELD360_API_KEY env.
EntityType RuleEntityType RuleEntityContext, RuleEntityPrompt, or RuleEntityEvaluation.
Fields map[string]interface{} Trace attributes to evaluate against rules.
IncludeEntityData bool If true, include full entity data in response.
EntityInputs map[string]interface{} Optional inputs for entity resolution (e.g. prompt variables).
Timeout time.Duration HTTP timeout. Default: 30s.
Example
result, err := shield360.EvaluateRule(ctx, shield360.EvaluateRuleOptions{
    EntityType: shield360.RuleEntityContext,
    Fields: map[string]interface{}{
        "gen_ai.system":        "openai",
        "gen_ai.request.model": "gpt-4",
        "service.name":         "my-app",
    },
    IncludeEntityData: true,
})
if err != nil {
    log.Fatal(err)
}
fmt.Println("Matching rules:", result.MatchingRuleIDs)
fmt.Println("Entities:", result.Entities)

Request tracing

After shield360.Init, wrap the server, the HTTP client and the database once, and each request becomes a trace with its outgoing calls and SQL inside:

mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", getOrder)
http.ListenAndServe(":8080", shield360.Handler(mux, "orders-api"))

client := shield360.HTTPClient()            // or &http.Client{Transport: shield360.Transport(base)}
db, err := shield360.OpenDB("postgres", dsn) // instead of sql.Open
  • Requests are named by their ServeMux route (GET /orders/{id}), with http.route set.
  • Health probes (Kubernetes, load balancers, uptime checks, /healthz-style paths) are not traced, and neither are calls to SHIELD360_URL.
  • SQL is recorded as the statement text, never its arguments.

Caches, document stores, RPC, messaging and AWS each have a one-line hook in its own package, so an app compiles only the client libraries it uses:

Library Package Hook
go-redis v9 instrumentation/goredis goredis.Instrument(rdb)
MongoDB driver v2 instrumentation/mongodb options.Client().ApplyURI(uri).SetMonitor(mongodb.Monitor())
gRPC instrumentation/grpctrace grpc.NewClient(addr, grpctrace.DialOption()), grpc.NewServer(grpctrace.ServerOption())
segmentio/kafka-go instrumentation/kafkago kafkago.WriteMessages(ctx, writer, msgs...), ctx, span := kafkago.StartProcess(ctx, msg, group)
AWS SDK v2 instrumentation/awstrace awstrace.Instrument(&cfg) before building clients

Values stay out: a Redis command is recorded by name only (set, never its key or value), a MongoDB command without its document and a failure by error name only, gRPC and Kafka without messages, keys or payloads (the trace context travels in Kafka headers), and AWS calls without request or response bodies. gRPC health checks are not traced. Spans follow the OpenTelemetry database, RPC and messaging conventions. For the MongoDB driver v1, use otelmongo from OpenTelemetry contrib, which keeps commands out by default. Sarama has no maintained OpenTelemetry instrumentation and is not covered yet.

To trace a Go service without changing its code, use eBPF on the host (the Shield360 controller).

Application logs

Off by default. Set SHIELD360_CAPTURE_LOGS=true (or the init option, Config.CaptureLogs) to send the application's own log records to Shield360, on the same endpoint and key as traces. SHIELD360_LOG_LEVEL (or Config.LogLevel) sets the lowest level sent (info by default; trace, debug, info, warn, error, fatal). Records carry the active trace and span ids, service.name and deployment.environment; credentials and opaque tokens in messages and attributes are masked before they leave the process; at most 500 records a second per bridge are sent, in batches, and a failure to send never reaches the application.

The bridge is a log/slog handler. With capture on, Init makes slog.Default() (and through it the log package) forward as well, while its output still goes where it went before; Shutdown puts it back.

on := true
_ = shield360.Init(shield360.Config{ApplicationName: "checkout-api", CaptureLogs: &on})
defer shield360.Shutdown(context.Background())

slog.InfoContext(ctx, "order placed", "order_id", id) // linked to the span in ctx

// Your own slog.Logger: a no-op until application logs are on.
logger := slog.New(shield360.LogHandler("checkout"))
logger.Error("payment failed", "err", err) // exception.type, exception.message
  • Records: body is the message; severity TRACE (below Debug), DEBUG, INFO, WARN, ERROR, FATAL (above Error); the scope is the handler's name (app by default); attributes keep their keys, with groups as dotted keys; code.file.path, code.function.name and code.line.number give the call site; the first error value becomes exception.type and exception.message.
  • Masking: the contract every SDK shares (sdk/rasp/README.md, "Masking"; also used for RASP snippets). Credentials and opaque tokens are masked: credential pairs (password=…, "api_key": "…", Authorization: Bearer …), bearer credentials, and opaque runs such as keys, hashes, base64, JWTs and UUIDs. Readable identifiers survive: class names, package and file paths, URLs. An attribute named like a credential (password, x-api-key, user.sessionId) is masked whole, whatever its value, and string lists are masked item by item. code.* and exception.type are never masked. Masking runs before the cut: messages are cut to 16,384 characters and attributes to 4,096.
  • Never sent: records from handlers named shield360… or opentelemetry…, the SDK's own log lines, and anything logged while a record is being exported.
  • shield360.CaptureLogs(level) and shield360.StopCapturingLogs() turn it on and off after Init; shield360.NewLogHandler(shield360.LogHandlerOptions{...}) takes your own OpenTelemetry LoggerProvider, level and rate.

Security feature coverage

Kept level with the TypeScript SDK (the reference). Each row passes the shared corpus named.

Feature Go Shared corpus
RASP sinks, custom rules, passive IAST, deception, baseline yes sdk/rasp/testdata/*.json
Protection triangle (Edge context, tightening, verdict header) yes (rasp.Middleware) sdk/rasp/testdata/triangle.json
Request correlation (Edge's request id and the trace id on slog records and the server span) yes sdk/rasp/testdata/request-correlation.json
Defend (defend(), signed policy, decisions feed, shared rate limits, reporting, client type, honeypot crumbs, content check) yes (defend.New, Middleware) injection golden corpus, crumb spec vector, byte-identical injection-rules.json and client-type signatures
Upload protection (inspection, signed policy, deep scan, backdoor checks) yes (InspectUpload, InspectUploadFile, UploadOptionsFor, UploadSaved; ClamAV, reputation, own signatures; the file and exec drop-ins) sdk/rasp/testdata/uploads.json, byte-identical upload-signatures.json

Protection triangle. With Shield360 Edge in front of the app, rasp.Middleware verifies Edge's signed X-Shield360-Edge-Context header (keys from the signed config's edgeContext, cross-checked against Edge's own X-Request-Id). A client Edge marked (flagged, trap level 2 or more, or risk 70 or more, unless a verified crawler or agent) is blocked for injection-class attacks the app only monitors (shield360.rasp.tightened_by_edge; a rule's own action and the monitor-only switch still apply). Every event of such a request carries shield360.edge.request_id, and the first finding is answered with a signed X-Shield360-Verdict response header (set before the handler writes its response) so Edge stops the client on its next request.

Request correlation. Edge's request id (from its verified context, else an id-shaped X-Request-Id, which Edge always sets) goes on the request's server span (shield360.Handler, the RASP capture) and on every slog record logged with the request's context (slog.InfoContext(r.Context(), …), or a context derived from it) as shield360.edge.request_id, with the trace id of the request's traceparent as shield360.edge.trace_id when no span is active.

Defend - defend.New()

Decide in the app whether a request may go on: the deny, challenge and trust lists, rules (paths, methods, countries, ASNs, client types, prompt injection, rates), traps and honeypot crumbs, from a signed policy (GET /api/defend/policy, typ shield360-defend+jws, pinned like the RASP config). No network call on the request path unless SharedRateLimits is set. Fails open by default.

import "github.com/Shield360-AI/shield360-go/defend"

shield, err := defend.New(defend.Options{
	App:    "checkout",
	URL:    os.Getenv("SHIELD360_URL"),
	APIKey: os.Getenv("SHIELD360_API_KEY"),
	// ConfigPublicKey or SHIELD360_CONFIG_PUBLIC_KEY pins the policy key.
})
if err != nil {
	log.Fatal(err)
}
defer shield.Close()

http.ListenAndServe(":8080", shield.Middleware(mux)) // 403 / 401 / honeypot decoys

// Or decide yourself, with content to check:
r := shield.Defend(defend.RequestOf(req), defend.Call{Content: []string{prompt}})
if answer := defend.ResponseFor(r); answer != nil { /* answer.Status, Headers, Body */ }
  • Decision order: kill switch, deny list, agent policy (Options.Agents), trap hit, challenge list, rules (first match wins; allow stops, monitor goes on), then sensitive data. SHIELD360_DEFEND=monitor reports without denying; off allows everything.
  • Client type (clienttype.Classify): human, verified agent, unverified automation or suspected AI agent, from the shared signatures, Edge's verified context and how widely a client roams.
  • Decisions feed: a block made anywhere reaches every instance within about 5 s.
  • Traps and honeypots: TrapHit marks a client; with honeypots on, decoy files (/.env, /config.json, ...) and crumbs (Crumbs) byte for byte the ones Shield360 Edge serves.
  • Reporting: deny, challenge and trap decisions and FailedLogin go to /api/defend/report, batched and folded; never a header, body, user name or password.

Feature flags - shield360.Flags()

Fetch an environment's feature flags and evaluate them locally, with the same engine as the Shield360 server and the other SDKs (the flags package). The client polls GET /api/flags/rules every 30 seconds (with ±10% jitter, at least every 5 seconds if you set PollInterval, sending the last ETag so an unchanged environment costs a 304) and starts on first use. It never panics or returns errors: until rules arrive, for an unknown flag, for a flag scoped to another app, for a value of the wrong kind, or on any error, you get your default.

The destination is Config.Shield360URL and Config.Shield360APIKey from shield360.Init, then SHIELD360_URL and SHIELD360_API_KEY. A flag scoped to an app is served only when its app is Config.ApplicationName (service.name).

shield360.Init(shield360.Config{ApplicationName: "checkout-api", Shield360URL: "https://shield360.example.com", Shield360APIKey: apiKey})
ff := shield360.Flags()
ff.WaitUntilReady(2 * time.Second) // optional: block startup until the first fetch

user := map[string]any{"key": userID, "plan": "pro", "groups": []string{"beta"}}
if ff.IsEnabled(ctx, "new-checkout", user) { /* ... */ }
limit := ff.GetNumber(ctx, "daily-limit", user, 100)
detail := ff.GetFlagDetail(ctx, "new-checkout", user, false) // Value, Variation, Reason

// AI helpers
model := ff.Model(ctx, "model-choice", user, "openai/gpt-4o-mini")
if ff.KillSwitch(ctx, "ai-assistant", user) { return errAssistantOff } // engaged when the flag serves false
p := ff.Prompt(ctx, "support-prompt", user, flags.PromptRef{Name: "support-reply"},
    flags.PromptOptions{Compile: true, Variables: map[string]any{"name": "Ana"}})
// p.Name, p.Version, p.Variation, p.Prompt (Prompt Hub's data), p.Err
Method Returns
GetFlagDetail(ctx, key, evalCtx, def) flags.Detail{Value, Variation, Reason}
GetFlag / GetJSON any (decoded JSON)
GetBool, GetString, GetNumber bool, string, float64
IsEnabled(ctx, key, evalCtx) GetBool(..., false)
Model(ctx, key, evalCtx, def) a provider/model id from a string flag
KillSwitch(ctx, key, evalCtx) true when the switch is engaged (!GetBool(..., true)), so it stays off when rules are unavailable
Prompt(ctx, key, evalCtx, def, opts) the Prompt Hub prompt the flag names ({"name", "version"} or "name@version"), else def, fetched from /api/prompt/get-compiled

When a variation is served, shield360.flag.<key> is set to its key on the recording span in ctx, so cost, latency and evals can be compared per variant (Options.DisableSpanAttributes turns this off). Rules that name segments (in_segment, not_in_segment) use the environment's segments from the same feed.

For your own client, flags.New(flags.Options{URL, APIKey, ApplicationName, PollInterval, Timeout, HTTPClient}); Start, Refresh, WaitUntilReady and Close control polling. flags.Evaluate(flag, context, segments), flags.ValidateFlag and flags.ValidateSegment expose the engine, which is held to the shared vectors in sdk/flags-core/testdata/vectors.json.

Runtime packages

With a Shield360 URL and API key, the SDK reports the modules the binary runs so Shield360 can match them against vulnerability data (Security → Code, as a "runtime" target): the Go version and the modules the binary was built with (runtime/debug.ReadBuildInfo; a replaced module reports its replacement's version). Each is a name and a version; no path, file or other field leaves the process, and at most 500 are sent. The report goes to /api/guard/events with the app's API key, about 15 seconds after start and then once a day, off the call path; it never panics and a report that cannot be delivered is dropped. When runtime protection is on it uses this report instead of sending its own.

Turn it off with Config.DisableRuntimePackages: true or SHIELD360_RUNTIME_PACKAGES=off.

Runtime protection (RASP)

Runtime application self-protection, opt-in, in the rasp package. It keeps the untrusted input of the request being served (and the model output the LLM instrumentation sees) in the request's context.Context and checks it where it reaches a dangerous sink. It follows the shared contract in sdk/rasp/README.md, with the same detection algorithms, rule ids, events and signed config as the other SDKs, and it fails open: the only error it returns on purpose is the block error, rasp.ErrBlocked.

shield360.Init(shield360.Config{
    ApplicationName: "orders-api",
    Shield360URL:    "https://shield360.example.com", // events and the signed config
    Shield360APIKey: apiKey,
    Rasp: &rasp.Options{
        ConfigPublicKey: os.Getenv("SHIELD360_CONFIG_PUBLIC_KEY"), // from the app's Runtime protection settings
    },
})
http.ListenAndServe(":8080", rasp.Middleware(mux))

SHIELD360_RASP=monitor|block turns it on without Config.Rasp; SHIELD360_RASP=off always wins. Everything runs in monitor mode unless an attack type is set to block (Options.Mode, Options.Attacks, or the platform's per-app settings in the signed config). rasp.Disable() and rasp.Enable() are the runtime kill switch; the env switch and the signed config's killSwitch rank above them.

rasp.Middleware is a plain func(http.Handler) http.Handler, so it works with http.ServeMux, chi and gorilla/mux (r.Use(rasp.Middleware)), echo (e.Use(echo.WrapMiddleware(rasp.Middleware))) and gin through any standard-middleware adapter, with no framework dependency. It records query values, route parameters (from the ServeMux pattern, or rasp.RecordRoute for other routers), cookies, the user-agent, referer and x-forwarded-for headers, and the body of POST, PUT, PATCH and DELETE requests (buffered up to 1 MiB and replayed to the handler in full). It runs the virtual patches before the handler and answers a blocking one with the contract's 403 JSON. Where a sink returns the block error, if rasp.WriteBlocked(w, err) { return } writes the same 403.

Deception (decoy paths): when the app's Runtime protection settings on the platform list decoy paths (none by default), rasp.Middleware answers a request to one with http.NotFound, never calling the handler, and marks the client's peer address (never X-Forwarded-For) suspicious for the configured time. Later requests from that address are reported as deception.suspicious, and get the contract's 403 when the action is block (capped by attacks.deception, and at monitor without a pinned key). Marks live in memory in this process only, at most 10,000 addresses (oldest dropped first), so each instance keeps its own. Matching follows sdk/rasp/README.md "Deception"; any error lets the request through.

Model output from the OpenAI, Anthropic and vLLM instrumentation (completion text, tool-call arguments) joins the request's inputs. A worker that acts on a model's answer outside a request wraps its job in ctx = rasp.WithScope(ctx).

The signed config is used only under a pinned key: Options.ConfigPublicKey or SHIELD360_CONFIG_PUBLIC_KEY (a JWK, a JWKS for rotation, or the bare base64url Ed25519 x). Without a pin the config is not fetched and RASP runs its built-in defaults in monitor mode, logging one warning. Trust on first use (Options.AllowUnpinnedConfig or SHIELD360_CONFIG_TOFU=dev) is for development only and is ignored when the environment is production. The last verified config is cached in os.TempDir()/shield360-rasp and verified again when loaded; polling never blocks start-up.

Events (shield360.rasp.event, batched, at most 100 a minute, repeats folded) and a components report (the Go runtime and the binary's modules, from runtime/debug.ReadBuildInfo) go to POST /api/guard/events. Events name where an input was found (query.id), never its value.

Sinks

Go has no runtime patching, so the sinks are opt-in: the app calls RASP's drop-in for each dangerous call. Each one checks before the underlying call, returns the block error instead of making it (in block mode), reports once and lets the call through in monitor mode, and fails open on anything else. Pass the request's context.Context: that is where the inputs live.

// database/sql: a drop-in for sql.Open. Every Query, Exec and Prepare (DB, Conn and Tx)
// is checked once, with the dialect from the driver name.
db, err := rasp.OpenSQL("postgres", dsn)
rows, err := db.QueryContext(r.Context(), query) // use the Context variants

// os/exec: drop-ins for exec.Command / exec.CommandContext. A blocked command gets the
// block error in cmd.Err, so Run, Start and Output return it without starting anything.
out, err := rasp.CommandContext(r.Context(), "convert", name, "out.png").Output()

// HTTP client: SSRF and the outbound allow-list before sending, and the address the
// connection actually dials (DNS rebinding).
client := rasp.WrapClient(http.DefaultClient) // or &http.Client{Transport: rasp.NewTransport(nil)}
resp, err := client.Do(req.WithContext(r.Context()))

// Files: the os functions with a context first.
data, err := rasp.ReadFile(r.Context(), filepath.Join(uploads, name))

if rasp.WriteBlocked(w, err) { return } // the contract's 403
Sink API Rules
database/sql rasp.OpenSQL, rasp.OpenSQLDB, rasp.WrapDriver (for sql.Register), rasp.WrapConnector, rasp.SQLDialectOf sqli.*. A statement a driver declines with driver.ErrSkip itself (not an error wrapping it) is not checked again when database/sql prepares it. The connection has the driver's own optional interfaces, SessionResetter and Validator included, and no others. db.Query without a context has no request in scope.
SQL Server, Oracle, Db2 (through database/sql) rasp.OpenSQL("sqlserver", dsn), rasp.OpenSQL("godror", dsn), rasp.OpenSQL("go_ibm_db", dsn), or WrapDriver/WrapConnector with that name sqli.*, lexed per driver name: sqlserver, mssql, azuresql → mssql; godror, oracle (go-ora), oci8 → oracle; go_ibm_db and names containing db2 → db2. Events use the sink db.<driver name>.query.
MongoDB rasp.CheckMongo(ctx, "find", filter) before the driver call nosqli.operator, nosqli.where. Reads bson.D, bson.M, mongo.Pipeline, maps and slices without importing the driver.
Processes rasp.Command, rasp.CommandContext, rasp.CheckCommand(ctx, argv), rasp.CheckShellCommand(ctx, line) cmdi.* on argv and on sh -c / bash -c / cmd /c scripts, shell_spawn.* with allow.commands. rasp.Command has no request in scope, so only shell_spawn.download_exec applies; prefer CommandContext. Changes to cmd.Path or cmd.Args after the call are not checked.
HTTP client rasp.NewTransport(base), rasp.WrapClient(c) outbound.not_allowed (inside or outside requests; the Shield360 and OTLP hosts always allowed), then ssrf.*, on every redirect hop. Internationalized hosts are matched in their percent-encoded, Unicode and IDNA ASCII forms. A request whose host name comes from an input and that goes direct is sent through a clone of the base *http.Transport that resolves the name, checks every address, and connects to exactly those addresses (TLS keeps the URL's server name), without keep-alive. A custom TLS dialer (DialTLSContext, legacy DialTLS) keeps doing its own connecting: its name is resolved and checked first, and the address it actually connected to is checked before any byte of the request is written. Through a proxy (Proxy, HTTP_PROXY) the target is resolved locally and checked before sending: best effort, since the proxy resolves the name itself. Keep NewTransport directly around the *http.Transport (tracing outside it); around another RoundTripper only the checks before sending (with a local lookup of a tainted name) run.
Files rasp.Open, Create, OpenFile, ReadFile, WriteFile, Remove, RemoveAll, Rename (both paths), ReadDir, rasp.CheckPath(ctx, path, op) path.* with allow.fileRoots, file_write.script, file_write.ntfs. Outside a RASP context only file_write.ntfs applies.
Code rasp.CheckCode(ctx, sink, src) before an expression or script engine evaluates text code.eval
Templates rasp.ParseTextTemplate(ctx, t, src) and rasp.ParseHTMLTemplate(ctx, t, src) for text/template and html/template (*Template).Parse (sinks text/template.Parse, html/template.Parse), rasp.CheckTemplate(ctx, sink, engine, src) for any engine of the shared table (rasp.TemplateEngines) code.template_injection on the template source only; data passed to Execute is never checked. An unknown engine name is not checked.
Deserialization rasp.CheckDeserialization(ctx, sink, format, data) before a third-party decoder of one of the contract's formats deser.*
XML rasp.UnmarshalXML(ctx, data, v), rasp.CheckXML(ctx, sink, data, resolvesExternal) xxe.*. encoding/xml never fetches external entities, so its findings are xxe.doctype.
HTML responses rasp.CheckResponses(handler) inside rasp.Middleware, or rasp.WrapResponseWriter(w, r) xss.reflected, monitor only: the response is never changed or delayed; a copy of at most 1 MiB is judged in the background after the handler returns (at most 8 at once; a response that finds every slot busy is not checked). The wrapper has exactly the writer's Flusher, Hijacker and Pusher, and Unwrap for http.ResponseController.

Event sinks name the call (db.postgres.query, exec.CommandContext, http.Transport, os.ReadFile, http.response). The contract's sink rules are for Node and Python; Go ignores them.

Uploads: rasp.Middleware reads the file names of multipart/form-data uploads from the part headers of the body it buffers, with mime/multipart and the request's boundary, so the names are those the handler's parser sees (at most 32 names, from the first 1 MiB; part bodies are skipped and the handler still reads the whole body). rasp.RecordRequestBody takes a parsed *multipart.Form, also after the middleware buffered the body, and adds any names not seen yet; each name is checked once. Detection reads the whole name, although the recorded input is cut at 4096 characters. The names are request input (body.<field>.filename), and an upload named like a script (.php, .jsp, .sh, .exe…) is reported at sink request.upload (file_write.upload; blocks when file_write blocks). A later rasp.WriteFile or rasp.Create named after the upload is caught by file_write.script.

Upload protection (sdk/rasp/README.md, "Uploads"), four layers:

  • Inspection: rasp.InspectUpload(ctx, part, name, contentType, rasp.UploadOptionsFor(route)) for each file part (or rasp.InspectUploadFile for a file already on disk) reads the real type, archives, macros and web-shell signatures in bounded memory.
  • Signed policy: the signed config's uploads block (allow, maxBytes, servedDirs, trusted, rules, deepScan, routes), else Options.Uploads (the same block as JSON, plus ScanBytes and QuarantineDir).
  • Deep scan: with deepScan.mode monitor or block, the customer's own clamd (zINSTREAM over tcp: or unix:, streamed as the part is read), the hash-only reputation lookup (only the SHA-256 leaves the app) and the organization's own signatures. scan: sync holds the upload until the engines answer; scan: async scans after rasp.UploadSaved and moves a file the mode blocks to quarantine. failMode: closed raises upload.scan_unavailable. Engine status goes to Shield360 as shield360.rasp.scanner records.
  • Backdoor checks: rasp.UploadSaved(ctx, path, verdict, opts) remembers where an upload was saved (as do InspectUploadFile, and writes during the request whose data is an upload's bytes or whose name is an upload's name). rasp.WriteFile, Create, OpenFile and Rename refuse saving one under a server-script name or into a served folder; rasp.Chmod refuses an execute bit; CommandContext, CheckCommand and CheckShellCommand refuse running one (directly, by an interpreter, or in sh -c); rasp.CheckLoad and rasp.CheckExtract cover plugin loads and archive extraction.

Security baseline: once per process, in the background after Init, RASP reports risky set-up as baseline events (report only, never blocks): InsecureSkipVerify on http.DefaultTransport, net/http/pprof on http.DefaultServeMux in production, GIN_MODE=debug under a production environment, running as root, an unpinned config key in production, and a Go release line past its end of life. See "Security baseline" in sdk/rasp/README.md.

code.include (loading a module named by input) does not apply to Go: a Go binary has no runtime module loading beyond plugin.Open.

Custom rules

The organization's own rules, written in the platform's rules editor, arrive in the signed config and are applied by the Go SDK as the contract's "Custom rules" section says (sdk/rasp/README.md). Each expression is parsed and type-checked once when the config arrives and evaluated as data, never as code; a rule that fails validation is skipped with one log line and the rest of the config applies. The language, its limits and error codes are those of the TypeScript SDK and the platform, and the regex subset is matched by a port of the same Pike VM (linear time, with the same 4,000,000-step budget per request), so testdata/custom-rules.json passes unchanged.

  • Request rules run with the virtual patches in two phases. rasp.Middleware judges the head (query, cookies, input headers, request.ip) before it reads the body, then the body phase once the body is buffered (rules that read request.body, and input.* rules that did not match at head). A request without a body (a GET, an empty POST) has a known, empty body, so the body phase still runs; a body the app still hands to rasp.RecordRequestBody becomes input for the built-in checks, without running the body phase again. A body the middleware does not read (a GET or other non-POST/PUT/PATCH/DELETE request that has one) stays unknown until rasp.RecordRequestBody adds it. A blocking match answers with the contract's 403.
  • Finding rules (those that read sink.* or attack.*) run whenever a built-in check reports a finding, and can block a call the built-in attack type only monitors.
  • request.ip is the peer address from http.Request.RemoteAddr; X-Forwarded-For is only request.headers["x-forwarded-for"]. request.route is the ServeMux pattern or the template given to rasp.RecordRoute.
  • A rule's action is capped by the custom_rule mode (Options.Attacks[rasp.CustomRuleAttack] or the platform's per-app setting): it blocks only when both say block. Events report custom.<id>.

rasp.CompileRuleExpr and rasp.EvaluateRuleExpr expose the evaluator for tests of your own rules.

Passive IAST

Off by default. Options.Iast (or SHIELD360_IAST=on, or the platform's per-app switch) turns on vulnerability reporting: where a request input reaches a sink verbatim and unparameterized during normal traffic, even when nothing attacked it (GET /api/orders/:id builds SQL from query.email). It never blocks, runs whatever the attack modes say, and adds work only to sink checks that already run with request input in scope. SHIELD360_IAST=off always wins. Checks are sampled: SHIELD360_IAST_SAMPLE_RATE, the signed config's rate, then Options.IastSampleRate, else every check (1 in 10 in production).

The rules reuse the tokens the attack checks already lexed, at the drop-in sinks above: database/sql (vuln.sql_concat), CommandContext / CheckCommand / CheckShellCommand (vuln.cmd_shell, vuln.cmd_arg), the file functions (vuln.path_from_input), the HTTP client's check before sending (vuln.ssrf_host), CheckCode, CheckDeserialization and CheckXML with resolvesExternal (vuln.code_eval, vuln.deser_input, vuln.xml_external). vuln.include_from_input has no Go sink. Each finding is reported as shield360.rasp.vulnerability at most once per process per hour per fingerprint (route, sink, input key, rule), with the count seen since, and never carries the input value. Its code location is the first stack frame (runtime.Callers) outside this SDK, the standard library and the module cache, relative to the working directory. Without a route template, path segments that are not short lower-case words become :id.

Requirements

  • Go 1.21 or higher
  • OpenTelemetry Collector or compatible backend

Contributing

We welcome contributions! See CONTRIBUTING.md for details.

License

Apache License 2.0 - see LICENSE

Support

Documentation

Overview

Package shield360 provides OpenTelemetry-native observability for LLM applications in Go. It offers automatic instrumentation for popular LLM providers like OpenAI and Anthropic.

Index

Constants

View Source
const (
	// DefaultLogLevel is the lowest level sent when application logs are on.
	DefaultLogLevel = "info"
	// DefaultLoggerName is the instrumentation scope of records from LogHandler("").
	DefaultLoggerName = "app"
	// DefaultLogsPerSecond is the most records a handler sends a second.
	DefaultLogsPerSecond = 500
)
View Source
const (
	RuntimePackagesStartDelay = 15 * time.Second
	RuntimePackagesEvery      = 24 * time.Hour
)

Runtime package report timing.

View Source
const IngestPath = "/api/ingest/otlp"

IngestPath is the platform's authenticated OTLP ingest route.

View Source
const Version = "1.2.0"

Version is the current version of the Shield360 Go SDK.

This is the single source of truth. Releases go through the Release / Packages workflow, which tags this repository `sdk-go-<Version>`; the mirror sync then tags Shield360-AI/shield360-go `v<Version>`, which is the tag Go itself resolves, because a module's import path is its repository URL and this repository is private.

Variables

This section is empty.

Functions

func CaptureLogs

func CaptureLogs(level string) bool

CaptureLogs sends slog records at level ("info" by default) and above to Shield360: the handlers LogHandler returns start sending, and slog.Default (and with it the log package) also forwards, its output still going where it went before. Calling it again changes the level. Call it after Init; it returns false when Init has not set up a LoggerProvider.

func Flags

func Flags() *flags.Client

Flags is the SDK's feature-flags client. It fetches the environment's flags from Config.Shield360URL with Config.Shield360APIKey (or SHIELD360_URL and SHIELD360_API_KEY) and evaluates them locally; flags scoped to an app are served only when their app is Config.ApplicationName. Polling starts on first use. It never panics: until rules arrive, or on any error, callers get their default.

if shield360.Flags().IsEnabled(ctx, "new-checkout", map[string]any{"key": userID}) { ... }

func HTTPClient

func HTTPClient() *http.Client

HTTPClient is an http.Client whose requests are traced.

func Handler

func Handler(h http.Handler, operation string) http.Handler

Handler traces each request h serves. operation names requests no route matched.

func Init

func Init(cfg Config) error

Init initializes the Shield360 SDK with the provided configuration. This should be called once at application startup.

Example:

err := shield360.Init(shield360.Config{
    OtlpEndpoint:    "http://127.0.0.1:4318",
    Environment:     "production",
    ApplicationName: "my-go-app",
})
if err != nil {
    log.Fatalf("Failed to initialize Shield360: %v", err)
}
defer shield360.Shutdown(context.Background())

func IsInitialized

func IsInitialized() bool

IsInitialized returns whether Shield360 has been initialized.

func LogHandler

func LogHandler(name string) slog.Handler

LogHandler returns a slog handler sending to Shield360 through the SDK's LoggerProvider under scope name ("app" when empty); see logHandler.

func MaskLogText

func MaskLogText(s string, max int) string

MaskLogText is at most max characters of s, masked by the contract every SDK shares (sdk/rasp/README.md, "Masking") before it is cut: credential pairs, bearer credentials and opaque tokens are masked; readable identifiers such as class names and file paths survive.

func NewCodeLocationProcessor

func NewCodeLocationProcessor(cfg Config) sdktrace.SpanProcessor

NewCodeLocationProcessor returns the span processor that records the application call site on GenAI spans, or nil when capture is disabled by Config.CaptureCodeLocation or SHIELD360_CAPTURE_CODE_LOCATION. Init registers it automatically; use it directly only with your own TracerProvider.

func NewLogHandler

func NewLogHandler(opts LogHandlerOptions) slog.Handler

NewLogHandler returns a slog handler sending to Shield360 with these options.

func OpenDB

func OpenDB(driverName, dataSourceName string) (*sql.DB, error)

OpenDB opens a database whose queries are traced (statement text, never the arguments), as sql.Open would.

func ReportRuntimePackages

func ReportRuntimePackages(ctx context.Context, o RuntimePackagesOptions) (ok bool)

ReportRuntimePackages sends one report now and reports whether every batch was accepted. Never panics.

func ResolveLogLevel

func ResolveLogLevel(name string) otellog.Severity

ResolveLogLevel turns a level name ("info", "WARN", "critical") into the lowest severity sent; empty or unknown names are INFO.

func RuntimePackagesActive

func RuntimePackagesActive() bool

RuntimePackagesActive reports whether the report is running.

func RuntimePackagesEnabled

func RuntimePackagesEnabled(disabled bool, getenv func(string) string) bool

RuntimePackagesEnabled reports whether the report runs: Disabled, or SHIELD360_RUNTIME_PACKAGES=off|false|0|no, turns it off.

func Shutdown

func Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the Shield360 SDK. It should be called before application exit to ensure all telemetry data is flushed.

func StartRuntimePackages

func StartRuntimePackages(o RuntimePackagesOptions) bool

StartRuntimePackages starts the report: once after StartDelay, then every Every. It returns whether it started; a second call while it runs, a missing URL or key, or a turned-off report start nothing. While it runs, runtime protection leaves its components report to it.

func StopCapturingLogs

func StopCapturingLogs()

StopCapturingLogs stops sending application logs and puts slog.Default and the log package back as they were.

func StopRuntimePackages

func StopRuntimePackages()

StopRuntimePackages stops the report (Shutdown, tests).

func Transport

func Transport(base http.RoundTripper) http.RoundTripper

Transport traces each request base sends (http.DefaultTransport when nil), except calls to Shield360.

Types

type Config

type Config struct {
	// OtlpEndpoint is the OTLP endpoint for sending traces, metrics and logs.
	// Default: OTEL_EXPORTER_OTLP_ENDPOINT; else, with a Shield360 URL and API
	// key, <Shield360URL>/api/ingest/otlp with the key as bearer; else
	// "http://127.0.0.1:4318".
	OtlpEndpoint string

	// OtlpHeaders are additional headers to send with OTLP requests
	OtlpHeaders map[string]string

	// Environment identifies the deployment environment
	// Default: "default"
	Environment string

	// ApplicationName is the name of your application
	// Default: "default"
	ApplicationName string

	// SHIELD: Shield360URL is Shield360's base URL, for feature flags (Flags()) and
	// Prompt Hub. Default: SHIELD360_URL.
	Shield360URL string

	// Shield360APIKey is the environment's API key for Shield360URL.
	// Default: SHIELD360_API_KEY.
	Shield360APIKey string

	// TracerName is the name for the tracer provider
	// Default: "shield360"
	TracerName string

	// ServiceVersion is the version of your service
	ServiceVersion string

	// DisableTracing disables trace collection
	// Default: false
	DisableTracing bool

	// DisableMetrics disables metrics collection
	// Default: false
	DisableMetrics bool

	// DisableBatch disables batch processing for exports
	// Default: false
	DisableBatch bool

	// TraceExporterTimeout is the timeout for trace exports
	// Default: 10 seconds
	TraceExporterTimeout time.Duration

	// MetricExporterTimeout is the timeout for metric exports
	// Default: 10 seconds
	MetricExporterTimeout time.Duration

	// MetricExportInterval is the interval for metric exports
	// Default: 30 seconds
	MetricExportInterval time.Duration

	// PricingInfo contains custom pricing information for models
	PricingInfo map[string]ModelPricing

	// PricingEndpoint is the endpoint to fetch pricing information
	// Default: none; pricing is not fetched unless an endpoint is set
	PricingEndpoint string

	// DisablePricingFetch disables automatic pricing information fetching
	DisablePricingFetch bool

	// CaptureMessageContent records prompt/completion text as span attributes.
	// Default: false — no content is captured unless asked for, here or with
	// OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true (matching the
	// Python and TypeScript SDKs). A Shield360 organisation stores content only
	// when its Data retention setting allows it, whatever is sent.
	CaptureMessageContent bool

	// DisableCaptureMessageContent prevents prompt/completion text from being
	// recorded, and wins over CaptureMessageContent and the environment.
	// Kept for compatibility; content is off by default.
	DisableCaptureMessageContent bool

	// DetailedTracing enables detailed component-level tracing.
	// Default: false
	DetailedTracing bool

	// IDGenerator overrides the default OTel ID generator. Use this when
	// callers need deterministic trace/span IDs (e.g. the shield360 CLI's
	// coding-agent hook deriving a stable TraceId from a session id so
	// every span emitted across separate hook invocations lands in the
	// same trace). When nil, OTel's random generator is used.
	IDGenerator sdktrace.IDGenerator

	// Sampler overrides the default head sampler. When nil, the SDK
	// uses AlwaysSample — every span emitted reaches the collector.
	// Callers wanting a head-sampling strategy (e.g. drop noisy
	// `coding_agent.tool.requested` events) can pass a
	// ParentBasedSampler or a custom span-name aware sampler here.
	// See cli/internal/otlp/sampler.go for an example.
	Sampler sdktrace.Sampler

	// ExtraResourceAttributes are merged into the OTel Resource set on
	// every span/metric this SDK emits. Callers use this to attach
	// stable identity (e.g. `gen_ai.user.name`, `host.name`) once at
	// startup rather than tagging each span individually.
	ExtraResourceAttributes map[string]string

	// CaptureCodeLocation records the application call site on GenAI spans
	// as code.function.name, code.file.path and code.line.number. Nil means
	// on unless SHIELD360_CAPTURE_CODE_LOCATION is false/0/no/off; a value
	// set here wins over the environment.
	CaptureCodeLocation *bool

	// CaptureCallLocation also records the call site on the app's outbound
	// calls: database, HTTP, RPC and messaging client (and producer) spans,
	// so Shield360 can price the costly code patterns shield360 scan finds.
	// Nil means on unless SHIELD360_CAPTURE_CALL_LOCATION is false/0/no/off;
	// it has no effect when CaptureCodeLocation is off.
	CaptureCallLocation *bool

	// CodeRoot is stripped from code.file.path to make it repo-relative.
	// Default: SHIELD360_CODE_ROOT, then the main module path (-trimpath
	// builds) or the working directory.
	CodeRoot string

	// CodeLocationIgnoreModules lists extra module path prefixes whose
	// frames are never reported as the call site (e.g. your own wrapper
	// around the instrumented client).
	CodeLocationIgnoreModules []string

	// SHIELD: CaptureLogs sends the application's own log/slog records to
	// Shield360 as OpenTelemetry logs, on the same endpoint and headers as
	// traces (signal path /v1/logs). Nil means off unless
	// SHIELD360_CAPTURE_LOGS is true/1/yes/on; a value set here wins. See
	// LogHandler and CaptureLogs.
	CaptureLogs *bool

	// LogLevel is the lowest level sent when CaptureLogs is on: trace,
	// debug, info, warn, error or fatal. Default: SHIELD360_LOG_LEVEL, then info.
	LogLevel string

	// SHIELD: Rasp turns on runtime application self-protection (the rasp
	// package) unless Rasp.Disabled is set. Nil leaves it off unless
	// SHIELD360_RASP=monitor|block; SHIELD360_RASP=off always wins. Events
	// and the signed config use Shield360URL and Shield360APIKey.
	Rasp *rasp.Options

	// SHIELD: DisableRuntimePackages stops the runtime package report: the
	// names and versions of the modules the binary was built with (never
	// paths), sent to Shield360URL so it can match them against
	// vulnerability data. On by default with a Shield360 URL and API key;
	// SHIELD360_RUNTIME_PACKAGES=off also turns it off.
	DisableRuntimePackages bool
}

Config holds the configuration for Shield360 SDK initialization

func GetConfig

func GetConfig() *Config

GetConfig returns the current global configuration. Returns nil if Shield360 is not initialized.

type EvaluateRuleOptions

type EvaluateRuleOptions struct {
	// URL is the Shield360 dashboard base URL.
	// Falls back to SHIELD360_URL env var, then http://127.0.0.1:3000.
	URL string

	// APIKey is the Bearer token for authentication.
	// Falls back to SHIELD360_API_KEY env var.
	APIKey string

	// EntityType specifies which kind of entities to match ("context", "prompt", or "evaluation").
	EntityType RuleEntityType

	// Fields are the trace attributes to evaluate against rules.
	// Example: {"gen_ai.system": "openai", "gen_ai.request.model": "gpt-4"}
	Fields map[string]interface{}

	// IncludeEntityData requests full entity data in the response when true.
	IncludeEntityData bool

	// EntityInputs provides optional inputs for entity resolution (e.g. prompt variables).
	EntityInputs map[string]interface{}

	// Timeout for the HTTP request. Defaults to 30 seconds.
	Timeout time.Duration
}

EvaluateRuleOptions holds parameters for the EvaluateRule call.

type EvaluateRuleResult

type EvaluateRuleResult struct {
	MatchingRuleIDs []string               `json:"matchingRuleIds"`
	Entities        []RuleEntity           `json:"entities"`
	EntityData      map[string]interface{} `json:"entity_data,omitempty"`
}

EvaluateRuleResult is the parsed response from the rule engine evaluate endpoint.

func EvaluateRule

func EvaluateRule(ctx context.Context, opts EvaluateRuleOptions) (*EvaluateRuleResult, error)

EvaluateRule calls the Shield360 dashboard rule-engine evaluate endpoint. It sends the provided fields against configured rules and returns matching rule IDs, associated entities, and optionally the full entity data.

This function does NOT require shield360.Init() — it is a standalone HTTP call, consistent with get_prompt/get_secrets in the Python and TypeScript SDKs.

Example:

result, err := shield360.EvaluateRule(ctx, shield360.EvaluateRuleOptions{
    EntityType: shield360.RuleEntityContext,
    Fields: map[string]interface{}{
        "gen_ai.system":        "openai",
        "gen_ai.request.model": "gpt-4",
        "service.name":         "my-app",
    },
    IncludeEntityData: true,
})

type LogHandlerOptions

type LogHandlerOptions struct {
	// Name is the instrumentation scope; "app" by default.
	Name string
	// LoggerProvider receives the records. Nil follows the SDK: records go
	// through Init's LoggerProvider while application logs are on
	// (Config.CaptureLogs, SHIELD360_CAPTURE_LOGS or CaptureLogs) and
	// nowhere otherwise.
	LoggerProvider otellog.LoggerProvider
	// Level is the lowest level sent with an explicit LoggerProvider
	// ("info" by default). With the SDK's provider the SDK's level applies.
	Level string
	// MaxPerSecond caps the records sent a second (default 500); the rest
	// are dropped.
	MaxPerSecond int
}

LogHandlerOptions configures NewLogHandler.

type ModelPricing

type ModelPricing struct {
	InputCostPerToken  float64
	OutputCostPerToken float64
}

ModelPricing represents pricing information for a model

type RuleEntity

type RuleEntity struct {
	RuleID     string `json:"rule_id"`
	EntityType string `json:"entity_type"`
	EntityID   string `json:"entity_id"`
}

RuleEntity represents a single matched rule-entity association.

type RuleEntityType

type RuleEntityType string

RuleEntityType constrains the entity_type values accepted by the rule engine.

const (
	// RuleEntityContext matches rules that return context entities.
	RuleEntityContext RuleEntityType = "context"
	// RuleEntityPrompt matches rules that return prompt entities.
	RuleEntityPrompt RuleEntityType = "prompt"
	// RuleEntityEvaluation matches rules that return evaluation entities.
	RuleEntityEvaluation RuleEntityType = "evaluation"
)

type RuntimePackagesOptions

type RuntimePackagesOptions struct {
	URL             string
	APIKey          string
	ApplicationName string
	Environment     string
	SDKVersion      string
	// Disabled turns the report off (Config.DisableRuntimePackages).
	Disabled bool
	// Getenv reads the environment (tests); default os.Getenv.
	Getenv func(string) string
	// StartDelay and Every override the timing (tests).
	StartDelay time.Duration
	Every      time.Duration
	// Collect lists the components (tests); default rasp.CollectComponents.
	Collect    func() []rasp.Component
	HTTPClient *http.Client
	RetryDelay time.Duration
}

RuntimePackagesOptions configure StartRuntimePackages.

Directories

Path Synopsis
Package clienttype says who is calling, with the reasons, deterministic and explainable (docs/latest/shield360/client-type.mdx).
Package clienttype says who is calling, with the reasons, deterministic and explainable (docs/latest/shield360/client-type.mdx).
Package defend is Shield360 Defend: decide in the app whether a request may go on.
Package defend is Shield360 Defend: decide in the app whether a request may go on.
internal/crumb
Package crumb is Shield360's one crumb generator: stateless deception tokens (fake paths, credentials, headers, LLM tripwires, decoy files) derived from HMAC(environment secret, epoch, owner fingerprint) and verifiable on a hit with no storage.
Package crumb is Shield360's one crumb generator: stateless deception tokens (fake paths, credentials, headers, LLM tripwires, decoy files) derived from HMAC(environment secret, epoch, owner fingerprint) and verifiable on a hit with no storage.
internal/injection
Package injection finds prompt injection in text an AI agent is about to read: a developer's prompt (direct injection), and tool results, files, fetched pages and tool descriptions (indirect injection and tool poisoning), plus the URL beacons attackers use to get data back out.
Package injection finds prompt injection in text an AI agent is about to read: a developer's prompt (direct injection), and tool results, files, fetched pages and tool descriptions (indirect injection and tool poisoning), plus the URL beacons attackers use to get data back out.
Package flags is the Shield360 feature-flag engine and client for Go.
Package flags is the Shield360 feature-flag engine and client for Go.
instrumentation
awstrace
Package awstrace traces AWS SDK for Go v2 (github.com/aws/aws-sdk-go-v2) calls inside the request trace.
Package awstrace traces AWS SDK for Go v2 (github.com/aws/aws-sdk-go-v2) calls inside the request trace.
goredis
Package goredis traces go-redis (github.com/redis/go-redis/v9) commands inside the request trace, recorded by command name only.
Package goredis traces go-redis (github.com/redis/go-redis/v9) commands inside the request trace, recorded by command name only.
grpctrace
Package grpctrace traces gRPC (google.golang.org/grpc) clients and servers with the OpenTelemetry stats handlers, health checks left out.
Package grpctrace traces gRPC (google.golang.org/grpc) clients and servers with the OpenTelemetry stats handlers, health checks left out.
internal/spantest
Package spantest records spans for the instrumentation tests.
Package spantest records spans for the instrumentation tests.
kafkago
Package kafkago traces segmentio/kafka-go producers and consumers and carries the trace across Kafka in message headers.
Package kafkago traces segmentio/kafka-go producers and consumers and carries the trace across Kafka in message headers.
mongodb
Package mongodb traces MongoDB Go driver v2 (go.mongodb.org/mongo-driver/v2) commands inside the request trace.
Package mongodb traces MongoDB Go driver v2 (go.mongodb.org/mongo-driver/v2) commands inside the request trace.
internal
guardevents
Package guardevents is the transport to Shield360's /api/guard/events: the endpoint guard reports, heartbeats and runtime-protection (RASP) events share.
Package guardevents is the transport to Shield360's /api/guard/events: the endpoint guard reports, heartbeats and runtime-protection (RASP) events share.
masking
Package masking is the masking contract every Shield360 SDK shares (sdk/rasp/README.md, "Masking"), checked by sdk/rasp/testdata/masking.json: credential pairs, bearer credentials and opaque tokens are masked; readable identifiers (class names, package and file paths) survive; an attribute named like a credential is masked whole.
Package masking is the masking contract every Shield360 SDK shares (sdk/rasp/README.md, "Masking"), checked by sdk/rasp/testdata/masking.json: credential pairs, bearer credentials and opaque tokens are masked; readable identifiers (class names, package and file paths) survive; an attribute named like a credential is masked whole.
Package rasp is Shield360 runtime application self-protection (RASP) for Go, opt-in.
Package rasp is Shield360 runtime application self-protection (RASP) for Go, opt-in.
Package semconv provides semantic conventions for Gen AI observability.
Package semconv provides semantic conventions for Gen AI observability.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址