Skip to content

GH-1608: Add observability support to Stability AI image model - #5876

Open
Gaurav1112 wants to merge 1 commit into
spring-projects:mainfrom
Gaurav1112:GH-1608
Open

GH-1608: Add observability support to Stability AI image model#5876
Gaurav1112 wants to merge 1 commit into
spring-projects:mainfrom
Gaurav1112:GH-1608

Conversation

@Gaurav1112

Copy link
Copy Markdown

GH-1608: Add observability support to Stability AI image model

Fixes #1608

Problem

StabilityAiImageModel was the last remaining ImageModel in the project without observability. Calls to imageModel.call(...) for Stability AI produced no gen_ai.client.operation observation, so they were invisible in the Actuator metrics endpoint and in any attached tracer — while equivalent calls against OpenAiImageModel have been fully instrumented for some time.

What this PR does

Instruments StabilityAiImageModel with Micrometer observations using the portable ImageModelObservationConvention infrastructure, mirroring OpenAiImageModel so the resulting tags are consistent across providers.

Production code

  • AiProvider — added STABILITY_AI("stability_ai") enum value (placed alphabetically, inside the existing @formatter:off region).
  • StabilityAiImageModel:
    • New 3-arg constructor StabilityAiImageModel(StabilityAiApi, StabilityAiImageOptions, ObservationRegistry). A null registry is defensively swapped for ObservationRegistry.NOOP.
    • Existing 1-arg and 2-arg constructors delegate to the new one with ObservationRegistry.NOOP, preserving source and binary compatibility.
    • call(ImagePrompt) is wrapped with ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION.observation(...).observe(...) using the shared DefaultImageModelObservationConvention. The raw ImagePrompt is passed to the observation context (matching OpenAiImageModel) so observation tags reflect what the user actually sent rather than post-merge defaults.
    • New setObservationConvention(ImageModelObservationConvention) accessor; guards against null.
  • StabilityAiImageAutoConfiguration — accepts ObjectProvider<ObservationRegistry> and ObjectProvider<ImageModelObservationConvention> and wires them into the model bean, identical to OpenAiImageAutoConfiguration.

Tests

All mocked — no network access and no API keys required.

  • StabilityAiImageModelObservationTests (7 cases):
    1. Happy path: verifies gen_ai.client.operation name, contextual name, provider = stability_ai, model tag, size/style high-cardinality tags.
    2. ImagePrompt with no options — observation still emitted with provider + operation type.
    3. Error path — when the API throws, the observation is still started and stopped (no instrumentation lost).
    4. Custom ImageModelObservationConvention is honoured (custom name + custom tags).
    5. setObservationConvention(null)IllegalArgumentException.
    6. Null ObservationRegistry constructor arg falls back to ObservationRegistry.NOOP.
    7. Pre-existing 1-arg and 2-arg constructors do not leak observations into unrelated registries.
  • StabilityAiImageAutoConfigurationObservabilityTests (3 cases):
    1. With no user-provided registry, the bean is still created (NOOP fallback).
    2. A user-provided ObservationRegistry bean is picked up via getIfUnique.
    3. A user-provided ImageModelObservationConvention bean is applied to the model.

Documentation

spring-ai-docs/.../observability/index.adoc — the Image Model note previously said "supported only for … OpenAI". Updated to list both "OpenAI, Stability AI".

How I verified

./mvnw -pl models/spring-ai-stability-ai,auto-configurations/models/spring-ai-autoconfigure-model-stability-ai -am test
  • spring-ai-stability-ai: 10 tests, 0 failures (3 existing options tests + 7 new observation tests).
  • spring-ai-autoconfigure-model-stability-ai: 5 tests, 0 failures (2 existing properties tests + 3 new observability wiring tests).
  • Reactor build: SUCCESS.

Compatibility

  • Binary / source compat: the existing 1-arg and 2-arg public constructors are unchanged and continue to work. The new 3-arg constructor is purely additive.
  • Behavioural: default runtime behaviour is unchanged — users who do not configure a Micrometer ObservationRegistry bean get ObservationRegistry.NOOP and see no new overhead. Users who do configure one now get a gen_ai.client.operation observation per call(...) automatically.

OTel semantic conventions

The STABILITY_AI provider value (stability_ai) follows the snake-case convention used by the other provider enum values (e.g. mistral_ai, oci_genai, openai_sdk) and is compatible with OpenTelemetry's gen_ai.system attribute as documented in the OTel GenAI semantic conventions.

Checklist

  • Branch named GH-1608 per CONTRIBUTING.adoc.
  • Commit signed off (DCO).
  • Single squashed commit.
  • Apache 2.0 headers on new files.
  • @author/@since tags added.
  • Unit tests cover happy path and error path and convention override and null-guard.
  • Autoconfig wiring tested.
  • Reference documentation updated.

…ge model

Instrument StabilityAiImageModel with Micrometer observations using the
portable ImageModelObservationConvention, matching the pattern already in
place for OpenAiImageModel and GoogleGenAiImageModel.

- Add STABILITY_AI ("stability_ai") to the AiProvider enum.
- Add a 3-arg StabilityAiImageModel constructor accepting an
  ObservationRegistry; existing constructors default to
  ObservationRegistry.NOOP, so behaviour is unchanged for current callers.
- Wrap call() with IMAGE_MODEL_OPERATION.observation(...).observe(...)
  using DefaultImageModelObservationConvention. The raw ImagePrompt is
  passed to the observation context for consistency with OpenAiImageModel.
- Expose setObservationConvention(...) for custom conventions.
- Wire ObjectProvider<ObservationRegistry> and
  ObjectProvider<ImageModelObservationConvention> in
  StabilityAiImageAutoConfiguration.
- Add unit tests (happy path, no-options path, API-error path, custom
  convention, null-convention guard, null-registry fallback and the
  backwards-compatible constructors) plus auto-configuration wiring tests.
- List Stability AI alongside OpenAI in the observability reference docs.

No public API is removed or renamed; the change is additive.

Fixes spring-projectsGH-1608

Signed-off-by: Kumar Gaurav <kgauravis016@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stability AI does not produce observability metrics

2 participants