public static Counter allocate(
        final Aeron aeron,
        final UnsafeBuffer tempBuffer,
        final long archiveId,
        final long recordingId,
        final int sessionId,
        final int streamId,
        final String strippedChannel,
        final String sourceIdentity)
    {
        tempBuffer.putLong(RECORDING_ID_OFFSET, recordingId);
        tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);

        final int sourceIdentityLength = Math.min(
            sourceIdentity.length(), MAX_KEY_LENGTH - SOURCE_IDENTITY_OFFSET - SIZE_OF_LONG);
        tempBuffer.putInt(SOURCE_IDENTITY_LENGTH_OFFSET, sourceIdentityLength);
        tempBuffer.putStringWithoutLengthAscii(SOURCE_IDENTITY_OFFSET, sourceIdentity, 0, sourceIdentityLength);
        final int archiveIdOffset = SOURCE_IDENTITY_OFFSET + sourceIdentityLength;
        tempBuffer.putLong(archiveIdOffset, archiveId);
        final int keyLength = archiveIdOffset + SIZE_OF_LONG;

        final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
        int labelLength = 0;
        labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset, NAME + ": ");
        labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, recordingId);
        labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
        labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId);
        labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
        labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId);
        labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
        labelLength += tempBuffer.putStringWithoutLengthAscii(
            labelOffset + labelLength,
            strippedChannel,
            0,
            MAX_LABEL_LENGTH - labelLength - ArchiveCounters.lengthOfArchiveIdLabel(archiveId));
        labelLength += ArchiveCounters.appendArchiveIdLabel(tempBuffer, labelOffset + labelLength, archiveId);

        return aeron.addCounter(
            RECORDING_POSITION_TYPE_ID, tempBuffer, 0, keyLength, tempBuffer, labelOffset, labelLength);
    }
@Test
    void allocateMaxKeyAndLabel()
    {
        final long archiveId = Long.MIN_VALUE;
        final long recordingId = 54311;
        final int sessionId = 42;
        final int streamId = -13;
        final String strippedChannel = generateStringWithSuffix("stripped channel", ".", 1000);
        final String sourceIdentity = generateStringWithSuffix("source identity", "X", 5000);
        final UnsafeBuffer tempBuffer = new UnsafeBuffer(new byte[METADATA_LENGTH]);
        final Counter counter = mock(Counter.class);
        final Aeron aeron = mock(Aeron.class);
        when(aeron.addCounter(
            eq(RECORDING_POSITION_TYPE_ID),
            eq(tempBuffer),
            eq(0),
            eq(MAX_KEY_LENGTH),
            eq(tempBuffer),
            eq(MAX_KEY_LENGTH),
            eq(MAX_LABEL_LENGTH)))
            .thenReturn(counter);

        final Counter result = RecordingPos.allocate(
            aeron,
            tempBuffer,
            archiveId,
            recordingId,
            sessionId,
            streamId,
            strippedChannel,
            sourceIdentity);

        assertSame(counter, result);
        int offset = 0;
        assertEquals(recordingId, tempBuffer.getLong(offset));
        offset += SIZE_OF_LONG;
        assertEquals(sessionId, tempBuffer.getInt(offset));
        offset += SIZE_OF_INT;
        final int expectedSourceIdentityLength = MAX_KEY_LENGTH - offset - SIZE_OF_INT - SIZE_OF_LONG;
        assertEquals(expectedSourceIdentityLength, tempBuffer.getInt(offset));
        assertTrue(expectedSourceIdentityLength < sourceIdentity.length());
        offset += SIZE_OF_INT;
        assertEquals(
            sourceIdentity.substring(0, expectedSourceIdentityLength),
            tempBuffer.getStringWithoutLengthAscii(offset, expectedSourceIdentityLength));
        offset += expectedSourceIdentityLength;
        assertEquals(archiveId, tempBuffer.getLong(offset));
        offset += SIZE_OF_LONG;

        offset = BitUtil.align(offset, SIZE_OF_INT);
        final String expectedPrefix = "rec-pos: 54311 42 -13 ";
        assertEquals(expectedPrefix, tempBuffer.getStringWithoutLengthAscii(offset, expectedPrefix.length()));
        offset += expectedPrefix.length();
        final int expectedStrippedChannelLength =
            MAX_LABEL_LENGTH - expectedPrefix.length() - ArchiveCounters.lengthOfArchiveIdLabel(archiveId);
        assertTrue(expectedStrippedChannelLength < strippedChannel.length());
        assertEquals(
            strippedChannel.substring(0, expectedStrippedChannelLength),
            tempBuffer.getStringWithoutLengthAscii(offset, expectedStrippedChannelLength));
        offset += expectedStrippedChannelLength;
        assertEquals(
            " - archiveId=-9223372036854775808",
            tempBuffer.getStringWithoutLengthAscii(offset, ArchiveCounters.lengthOfArchiveIdLabel(archiveId)));
    }