{"record":{"id":"a22c53bb8acab479","repo":"apache/kafka","slug":"clientinstanceid-not-set","errorCode":null,"errorMessage":"clientInstanceId not set","messagePattern":"clientInstanceId not set","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java","lineNumber":571,"sourceCode":"        this.injectTimeoutExceptionCounter = injectTimeoutExceptionCounter;\n    }\n\n    /**\n     * Sets the client instance ID for this mock consumer.\n     *\n     * @param instanceId the client instance ID\n     */\n    public void setClientInstanceId(final Uuid instanceId) {\n        clientInstanceId = instanceId;\n    }\n\n    @Override\n    public Uuid clientInstanceId(Duration timeout) {\n        if (telemetryDisabled) {\n            throw new IllegalStateException();\n        }\n        if (clientInstanceId == null) {\n            throw new UnsupportedOperationException(\"clientInstanceId not set\");\n        }\n        if (injectTimeoutExceptionCounter != 0) {\n            // -1 is used as \"infinite\"\n            if (injectTimeoutExceptionCounter > 0) {\n                --injectTimeoutExceptionCounter;\n            }\n            throw new TimeoutException();\n        }\n\n        return clientInstanceId;\n    }\n\n    @Override\n    public synchronized Map<MetricName, ? extends Metric> metrics() {\n        ensureNotClosed();\n        return Collections.emptyMap();\n    }\n","sourceCodeStart":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java#L553-L589","documentation":"Thrown by MockConsumer.clientInstanceId(Duration) when clientInstanceId was never set via setClientInstanceId(Uuid) and telemetry is not disabled. The mock does not auto-generate an instance id (unlike the real consumer); tests that exercise telemetry must explicitly inject one, or disable telemetry, otherwise the call is unsupported.","triggerScenarios":"Calling clientInstanceId on a MockConsumer without prior setClientInstanceId; telemetry not disabled via setTelemetryDisabled(true); test code copied from the real consumer without mock setup.","commonSituations":"Telemetry/metrics tests using MockConsumer; framework code that calls clientInstanceId unconditionally on any Consumer; integration glue tested with mocks.","solutions":["Call mock.setClientInstanceId(Uuid.randomUuid()) before exercising clientInstanceId().","If the test does not cover telemetry, call mock.setTelemetryDisabled(true) to short-circuit the path.","Guard production telemetry code behind a feature flag so mocks that lack an instance id do not invoke it."],"exampleFix":"// before\nMockConsumer<String,String> mock = new MockConsumer<>(OffsetResetStrategy.EARLIEST);\nUuid id = mock.clientInstanceId(Duration.ofSeconds(5)); // throws\n\n// after\nmock.setClientInstanceId(Uuid.MIGRATED_CLIENT_INSTANCE_ID); // or Uuid.randomUuid()\nUuid id = mock.clientInstanceId(Duration.ofSeconds(5));","handlingStrategy":"validation","validationCode":"if (mockClientInstanceId == null) {\n    // either inject one or disable telemetry for this test\n    mockConsumer.setTelemetryDisabled(true);\n}\nUuid id = mockConsumer.clientInstanceId(Duration.ofSeconds(5));","typeGuard":"static boolean mockHasInstanceId(MockConsumer<?,?> m) {\n    try { m.clientInstanceId(Duration.ZERO); return true; }\n    catch (UnsupportedOperationException | IllegalStateException e) { return false; }\n}","tryCatchPattern":"try {\n    return mockConsumer.clientInstanceId(timeout);\n} catch (UnsupportedOperationException e) {\n    if (\"clientInstanceId not set\".equals(e.getMessage())) {\n        return Optional.empty(); // or skip telemetry\n    }\n    throw e;\n}","preventionTips":["Call setClientInstanceId(Uuid.randomUuid()) in @BeforeEach for telemetry tests.","Call setTelemetryDisabled(true) for tests that do not exercise telemetry.","Guard production telemetry code so mocks without an id do not invoke it."],"tags":["consumer","mock-consumer","test","telemetry","unsupported-operation"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}